Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that uses a stack to reverse its inputs. Your stack must be gene

ID: 2246739 • Letter: W

Question

Write a program that uses a stack to reverse its inputs. Your stack must be generic and you must demonstrate that it accepts both String and Integer types. Your stack must implement the following methods:

push,

pop,

isEmpty (returns true if the stack is empty and false otherwise), and

size (returns an integer value for the number of items in the stack).

You may use either an ArrayList or a LinkedList to implement your stack. Also, your pop method must throw an IndexOutOfBoundsException if the user attempts to “pop” something off an empty stack. Name your stack class “Stack” and put it in a file named Stack.java.

Create a StackTest.java file to demonstrate all of the methods of your stack. Instantiate two stacks; one for String and one for Integer. Push words/integers onto your stack. After you have pushed a series of words/integers on your stack use a while loop to pop and print each item. If your stack is working properly your inputs should print in reverse order. Be sure that you calls to pop are in a try statement and will catch the IndexOutOfBoundsException, if thrown.

Note: Java has a Stack class, but you may not use it. You must create your own Stack class and your class must have the following operations (all of which must be demonstrated): push, pop, isEmpty, and size.

Explanation / Answer

The Stack.java is the utility class which will perform all the Stack operations. I have provided the comments for each line.

public class Stack {

private LinkedList<Object> list;

public Stack(){

list = new LinkedList<Object>();

}

//this method check the size is empty or not

public boolean isEmpty(){

return (list.size() == 0);

}

//this method add an object

public void push(Object item){

list.add(item);

}

//this method remove an object and return

public Object pop(){

Object item = list.get(size()-1);

list.remove(size()-1);

return item;

}

//this method return the size

public int size(){

return list.size();

}

}

The file StackTest.java is test class to test all the util method (push, pop, isEmpty() and size())

public class StackTest {

public static void main(String[] args) {

//Creating String stack

Stack stringStack = new Stack();

stringStack.push("AAA");

stringStack.push("BBB");

stringStack.push("CCC");

stringStack.push("DDD");

//Creating Integer stack

Stack intStack = new Stack();

intStack.push(1111);

intStack.push(2222);

intStack.push(3333);

//test integer stack

testStack(intStack);

//test String stack

testStack(stringStack);

}

//Common method to test string and integer stack

public static void testStack(Stack stack){

//test for isEmpty(), size() and pop check for integer stack

try{

System.out.println("Size of stack is "+stack.size());

//Test to check the stack entries

while(stack.isEmpty()!=true){

System.out.println(stack.pop());

}

//If uncomment the below line it will throw the IndexOutOfBoundsException

//System.err.println(stack.pop());

}catch(Exception e){

e.printStackTrace();

}

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote