StackOfIntegers class Displaying the user inputs in reverse order using StackOfI
ID: 3767110 • Letter: S
Question
StackOfIntegers class
Displaying the user inputs in reverse order using StackOfIntegers class:
Write a program that asks user to enter ten integer numbers, use the StackOfIntegers class to store the numbers, and retrieve and display them in reverse order.
Please follow the sample code on our textbook to format and indent your code properly.
StackOfIntegers.java is attached below.
public class StackOfIntegers {
private int[] elements;
private int size;
public static final int DEFAULT_CAPACITY = 16;
/** Construct a stack with the default capacity 16 */
public StackOfIntegers() {
this(DEFAULT_CAPACITY);
}
/** Construct a stack with the specified maximum capacity */
public StackOfIntegers(int capacity) {
elements = new int[capacity];
}
/** Push a new integer into the top of the stack */
public void push(int value) {
if (size >= elements.length) {
int[] temp = new int[elements.length * 2];
System.arraycopy(elements, 0, temp, 0, elements.length);
elements = temp;
}
elements[size++] = value;
}
/** Return and remove the top element from the stack */
public int pop() {
return elements[--size];
}
/** Return the top element from the stack */
public int peek() {
return elements[size - 1];
}
/** Test whether the stack is empty */
public boolean empty() {
return size == 0;
}
/** Return the number of elements in the stack */
public int getSize() {
return size;
}
}
Explanation / Answer
Answer :
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.