Question

ex3 Write a method public static boolean isPalindrome(String input) that uses one or more stacks to...

ex3

  1. Write a method public static boolean isPalindrome(String input) that uses one or more stacks to determine if a given string is a palindrome. [A palindrome is a string that reads the same forwards and backwards, for example ‘racecar’, ‘civic’]. Make sure that your method works correctly for special cases, if any.

  1. What is the big-O complexity of your method in terms of the list size n.

Supplementary Exercise for Programming (Coding)

  1. [Stacks] Download and unpack (unzip) the file Stacks.rar. Compile and execute the class StacksTest.java. Use this file structure for completing Exercise 3.

import java.util.*;

public class StacksTest {
   public static void main(String[] args) {
       Stack s = new Stack();
       s.push(new Integer(3));
       s.push(new Integer(5));
       s.push(new String("hi"));
       while(!s.isEmpty()) {
           System.out.print(s.pop() + " ");
       }
      
       s.clear(); //Empty the contents of the stack
      
       System.out.println("\nHere's how I reverse a string: ");
       Scanner k = new Scanner(System.in);
       System.out.print("Enter a string> ");
       String input = k.nextLine();
      
       for(int i = 0; i < input.length(); i++)
           s.push(input.charAt(i) + "");
          
       System.out.println("The reversed string is: ");
       while(!s.isEmpty()) {
           System.out.print(s.pop());
       }
      
       System.out.println();
      
   }
}

------------------------------------------------------------------------------------------------------------------------------

public class Stack {
private java.util.ArrayList pool = new java.util.ArrayList();
public Stack() {
}
  
public Stack(int n) {
pool.ensureCapacity(n);
}
  
public void clear() {
pool.clear();
}
  
public boolean isEmpty() {
return pool.isEmpty();
}
  
public Object topEl() {
if (isEmpty())
throw new java.util.EmptyStackException();
return pool.get(pool.size()-1);
}
  
public Object pop() {
if (isEmpty())
throw new java.util.EmptyStackException();
return pool.remove(pool.size()-1);
}
  
public void push(Object el) {
pool.add(el);
}
  
public String toString() {
return pool.toString();
}
}

Homework Answers

Answer #1

import java.util.*;

public class StacksTest {

        static class Stack {
                private ArrayList pool = new ArrayList();

                public Stack() {
                }

                public Stack(int n) {
                        pool.ensureCapacity(n);
                }

                public void clear() {
                        pool.clear();
                }

                public boolean isEmpty() {
                        return pool.isEmpty();
                }

                public Object topEl() {
                        if (isEmpty())
                                throw new java.util.EmptyStackException();
                        return pool.get(pool.size() - 1);
                }

                public Object pop() {
                        if (isEmpty())
                                throw new java.util.EmptyStackException();
                        return pool.remove(pool.size() - 1);
                }

                public void push(Object el) {
                        pool.add(el);
                }

                public String toString() {
                        return pool.toString();
                }
                public static Boolean isPalindrome(String input) {
                    Stack s = new Stack();

                    for (int i = 0; i < input.length(); i++)
                        s.push(input.charAt(i));

                    boolean palindrome = true;
                    int i = 0;
                    while(i < input.length()) {
                        if(input.charAt(i) != (char)s.pop()) {
                            palindrome = false;
                            break;
                        }
                        i++;
                    }
                    return palindrome;
                }
        }

        public static void main(String[] args) {
                Stack s = new Stack();
                s.push(new Integer(3));
                s.push(new Integer(5));
                s.push(new String("hi"));
                while (!s.isEmpty()) {
                        System.out.print(s.pop() + " ");
                }

                s.clear(); // Empty the contents of the stack

                System.out.println("\nHere's how I reverse a string: ");
                Scanner k = new Scanner(System.in);
                System.out.print("Enter a string> ");
                String input = k.nextLine();
                
                for (int i = 0; i < input.length(); i++)
                        s.push(input.charAt(i));

                boolean palindrome = isPalindrome(input);

                System.out.println();

        }
}
**************************************************

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a method public static boolean isPalindrome(String input) that uses one or more stacks to determine...
Write a method public static boolean isPalindrome(String input) that uses one or more stacks to determine if a given string is a palindrome. [A palindrome is a string that reads the same forwards and backwards, for example ‘racecar’, ‘civic’]. Make sure that your method works correctly for special cases, if any. (b) What is the big-O complexity of your method in terms of the list size n.
Please explain code 1 and code 2 for each lines code 1 public class MyQueue {...
Please explain code 1 and code 2 for each lines code 1 public class MyQueue {    public static final int DEFAULT_SIZE = 10;    private Object data[];    private int index; code 2 package test; import java.util.*; /* Class Node */ class Node { protected Object data; protected Node link; /* Constructor */ public Node() { link = null; data = 0; } /* Constructor */ public Node(Object d,Node n) { data = d; link = n; } /*...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {       ...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {        Stack<Integer> new_stack = new Stack<>();/* Start with the empty stack */        Scanner scan = new Scanner(System.in);        int num;        for (int i=0; i<10; i++){//Read values            num = scan.nextInt();            new_stack.push(num);        }        int new_k = scan.nextInt(); System.out.println(""+smallerK(new_stack, new_k));    }     public static int smallerK(Stack s, int k) {       ...
Fix the program: what if the input is three? import java.util.Scanner public class Test { public...
Fix the program: what if the input is three? import java.util.Scanner public class Test { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter an integer: "); int m = in.nextInt(); System.out.print("Enter another integer: "); int n = in.nextInt(); System.out.println(m + " " + n); } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new...
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); // Read the input string String input = sc.nextLine(); BalancedParentheses bp = new BalancedParentheses(); // Print whether the string has balanced parentheses System.out.println(bp.hasBalancedParentheses(input)); } } class BalancedParentheses { public boolean hasBalancedParentheses(String input) { // Remove this and implement code throw new UnsupportedOperationException(); } }
Create a class StacktwoPopArr using Arrays, Change the pop function so that two elements get popped...
Create a class StacktwoPopArr using Arrays, Change the pop function so that two elements get popped everytime instead of the one. If there is only one element in the stack, just pop the one element and report that the stack is now empty. If stack is empty, it should just report that stack is empty. All other functions for StackArray remain the same. using java StackArray.java: public class StackArray {       private final int size = 20; //Size of...
import java.util.Scanner; public class FindMinLength { public static int minLength(String[] array) { int minLength = array[0].length();...
import java.util.Scanner; public class FindMinLength { public static int minLength(String[] array) { int minLength = array[0].length(); for (int i = 0; i < array.length; i++) { if (array[i].length() < minLength) minLength = array[i].length(); } return minLength; } public static void main(String[] args) { Scanner in = new Scanner(System.in); String[] strings = new String[50]; for (int i = 0; i < strings.length; i++) { System.out.print("Enter string " + (i + 1) + ": "); strings[i] = in.nextLine(); } System.out.println("Length of smallest...
Consider a generic Stack class implemented using linked list with the following methods: public boolean isEmpty(),...
Consider a generic Stack class implemented using linked list with the following methods: public boolean isEmpty(), which returns true if and only if the stack is empty; public T pop() throws StackUnderflowException, which removes and returns the top element of the stack (if the stack is empty, it throws StackUnderflowException); public T peek() throws StackUnderflowException, which returns the top element of the stack (but does not remove it; it throws exception if stack is empty); public void push(T element), which...
Q1; Write a method in class SLL called public SLL reverse() that produces a new linked...
Q1; Write a method in class SLL called public SLL reverse() that produces a new linked list with the contents of the original list reversed. Make sure not to use any methods like addToHead() or addToTail(). In addition, consider any special cases that might arise. What is the big-O complexity of your method in terms of the list size n Supplementary Exercise for Programming (Coding) [Singly Linked Lists] Download and unpack (unzip) the file SinglyLinkedList.rar. Compile and execute the class...
Provide A UML for the Following CODE public class Employee{ public String strName, strSalary; public Employee(){...
Provide A UML for the Following CODE public class Employee{ public String strName, strSalary; public Employee(){    strName = " ";    strSalary = "$0";    } public Employee(String Name, String Salary){    strName = Name;    strSalary = Salary;    } public void setName(String Name){    strName = Name;    } public void setSalary(String Salary){    strSalary = Salary;    } public String getName(){    return strName;    } public String getSalary(){    return strSalary;    } public String...