Question: An implementation of a Stack ADT has been provided for this lab at the bottom of this description. It uses the List ADT we have

An implementation of a Stack ADT has been provided for this lab at the bottom of this description. It uses the List ADT we have discussed in lecture. You will need to copy and use the source code for the List ADT, and the source code for the Stack ADT that uses the List ADT.

Using a stack, you will take input values, place them on the stack, and take them off the stack. Once the input values are taken off the stack, they need to be shown the reverse order from how they were placed on the stack.

To do this, you will make a class called Lab3 which will have an object made from the Stack ADT. That object made from the Stack ADT you will use to perform the reversing of the input values.

Your Lab class needs to have a method you will write which corresponding to the following UML:

+reverse() : string

reverse prints output showing the items in the reverse order in which they were inputted.

Your Lab class need to have a main() method and is the program that is run at the command line. The program should perform a loop, ask for input, and display output in the following way (note that the output in bold is what the user inputs):

Input a list of items: 5 9 101 183 4893

The reverse order you inputted your items is: 4893 183 101 9 5

Do you want to continue (y/n): y

Input a list of items: m o m

The reverse order you inputted your items is: m o m

Do you want to continue (y/n): y

Input a list of items: radar civic wow noon racecar

The reverse order you inputted your items is: racecar noon wow civic radar

Do you want to continue (y/n): y

To submit for the lab

Remember to submit both your program listing(s), including both solutions, and captures of the program runs as shown above, for both solutions, on Canvas. You may submit captures of the program run as either a text file that has all of the output of the driver program or as an image file that has all of the output of the driver program.

public class StackException extends java.lang.RuntimeException {

public StackException(String s) {

super(s);

} // end constructor

} // end StackException

public interface StackInterface {

public boolean isEmpty();

// Determines whether the stack is empty.

// Precondition: None.

// Postcondition: Returns true if the stack is empty; otherwise returns false.

public void popAll();

// Removes all the items from the stack;

// Precondition: None.

// Postcondition: Stack is empty.

public void push(Object newItem) throws StackException;

// Adds an item to the top of a stack.

// Precondition: newItem is the item to be added.

// Postcondition: If insertion is successful, newItem is on the top of the stack.

// Exception: Some implementations may throw StackException when newItem cannot be place on the stack.

public Object pop() throws StackException;

// Removes the top of a stack;

// Precondition: None.

// Postcondition: If the stack is not empty, the item tat was added most recently is removed from the stack and returned.

// Exception: Throws StackException if the stack is empty.

public Object peek() throws StackException;

// Retrieves the top of a stack.

// Precondition: None.

// Postcondition: If the stack is not empty, the item that was added most recently is returned. The stack is unchanged.

// Exception: Throws StackException if the stack is empty.

} // end StackInterface

public class StackListBased implements StackInterface {

private ListInterface list;

public StackListBased() {

list = new ListReferencedBased();

} // end default constructor

public boolean isEmpty() {

return list.isEmpty();

} // end isEmpty

public void push(Object newItem) {

list.add(0, newItem);

} // end push

public Object pop() throws StackException {

if (!list.isEmpty()) {

Object temp = list.get(0);

list.remove(0);

return temp;

}

else {

throw new StackException("StackException on " + "pop: stack empty");

}

} // end pop

public void popAll() {

list.removeAll();

} // end popAll

public Object peek() throws StackException {

if (!isEmpty()) {

return list.get(0);

}

else {

throw new StackException("StackException on " + "peek: stack empty");

}

} // end peek

} // end StackListBased

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!