/** * A stack provides last-in-first-out behavior. All access * operations on a
ID: 3617622 • Letter: #
Question
/**
* A stack provides last-in-first-out behavior. All access
* operations on a stack are done at its<code>top</code>.
*/
public interface Stack<E> {
/**
* Determine if the stack is empty.
* @return <code>true</code> if the stackis empty,
* otherwise return false
*/
public boolean isEmpty();
/**
* Return the top element of the stack without removingit.
* This operation does not modify the stack.
* @return topmost element of the stack
* @throws EmptyStackException if the stack isempty
*/
public E peek();
/**
* Pop the top element from the stack and returnit.
* @return topmost element of the stack
* @throws EmptyStackException if the stack isempty
*/
public E pop();
/**
* Push <code>element</code> on top of thestack.
* @param element the element to be pushed on thestack.
*/
public void push( E element );
/**
* Return the number of elements currently stored inthe stack.
* @return topmost element of the stack
*/
public int size();
}
Explanation / Answer
importjava.util.*; public class Palindrome { publicstatic void main(String[]args) { Scanner in =new Scanner(System.in); int values; System.out.print("Enter yournumber: "); values = in.nextInt(); String number =""+values; StacknumStack = new Stack(); for(char c: number.toCharArray()) { numStack.push(c); } String reversed =""; while(!numStack.isEmpty()) { reversed += numStack.pop(); } System.out.println(reversed); if(reversed.equals(number)) { System.out.println("Palindrome"); } } }Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.