Question

In: Computer Science

Implement our own stack class patterned after Java's Stack class. Start with a generic class that...

Implement our own stack class patterned after Java's Stack class. Start with a generic class that uses an ArrayList for storage of the elements:

public class StackBox<E>
{
ArrayList<E> stack = new ArrayList<E>();
}

Implement the following methods in StackBox:

boolean empty()
Tests if this stack is empty.

E push(E item)
Pushes an item onto the top of this stack. Returns item pushed.

E pop()
Removes the object at the top of this stack and returns that object as the value of this function. Throws EmptyStackException if empty.

E peek()
Looks at the object at the top of this stack without removing it from the stack. Throws EmptyStackException if empty.

Refer to my slides in class for exact specifications details for each of these methods. For full marks your StackBox must throw exceptions as specified above. Now create an instance of StackBox that holds Integers. Create a menu that looks like this to test your StackBox:

=[==[==[=[=================================]=]==]==]=

=[==[==[=[ Welcome to StackBox ]=]==]==]=

=[==[==[=[=================================]=]==]==]=

Choose...

1. Push a (Integer) to our stack box.

2. Pop a (Integer) off our stack box.

3. Peek at top of the stack.

4. Exit

Defining StackBox as a generic class. [ 1 mark ].  WARNING - your program will NOT be accepted if you don't create and use a generic class StackBox as defined for this assignment.

Implementing the four Stack methods: empty, push. pop, and peek as per their specification including throwing exceptions if specified. [ 2 marks ]

=[==[==[=[=================================]=]==]==]=

=[==[==[=[ Welcome to StackBox ]=]==]==]=

=[==[==[=[=================================]=]==]==]=

Choose...

1. Push a (Integer) to our stack box.

2. Pop a (Integer) off our stack box.

3. Peek at top of the stack.

4. Exit

>> 1

Enter an Integer you want to add to the StackBox >>1

=[==[==[=[=================================]=]==]==]=

=[==[==[=[ Welcome to StackBox ]=]==]==]=

=[==[==[=[=================================]=]==]==]=

Choose...

1. Push a (Integer) to our stack box.

2. Pop a (Integer) off our stack box.

3. Peek at top of the stack.

4. Exit

>> 1

Enter an Integer you want to add to the StackBox >>2

=[==[==[=[=================================]=]==]==]=

=[==[==[=[ Welcome to StackBox ]=]==]==]=

=[==[==[=[=================================]=]==]==]=

Choose...

1. Push a (Integer) to our stack box.

2. Pop a (Integer) off our stack box.

3. Peek at top of the stack.

4. Exit

>> 1

Enter an Integer you want to add to the StackBox >>3

=[==[==[=[=================================]=]==]==]=

=[==[==[=[ Welcome to StackBox ]=]==]==]=

=[==[==[=[=================================]=]==]==]=

Choose...

1. Push a (Integer) to our stack box.

2. Pop a (Integer) off our stack box.

3. Peek at top of the stack.

4. Exit

>> 3

The element currently at the top of the stack is 3   [ 2 mark for correct peek behavior ]

=[==[==[=[=================================]=]==]==]=

=[==[==[=[ Welcome to StackBox ]=]==]==]=

=[==[==[=[=================================]=]==]==]=

Choose...

1. Push a (Integer) to our stack box.

2. Pop a (Integer) off our stack box.

3. Peek at top of the stack.

4. Exit

>> 2

The element 3 was removed from the top of the stack box.

=[==[==[=[=================================]=]==]==]=

=[==[==[=[ Welcome to StackBox ]=]==]==]=

=[==[==[=[=================================]=]==]==]=

Choose...

1. Push a (Integer) to our stack box.

2. Pop a (Integer) off our stack box.

3. Peek at top of the stack.

4. Exit

>> 2

The element 2 was removed from the top of the stack box.

=[==[==[=[=================================]=]==]==]=

=[==[==[=[ Welcome to StackBox ]=]==]==]=

=[==[==[=[=================================]=]==]==]=

Choose...

1. Push a (Integer) to our stack box.

2. Pop a (Integer) off our stack box.

3. Peek at top of the stack.

4. Exit

>> 2

The element 1 was removed from the top of the stack box.

[ 7 MARKS for correct push/pop behavior ]

=[==[==[=[=================================]=]==]==]=

=[==[==[=[ Welcome to StackBox ]=]==]==]=

=[==[==[=[=================================]=]==]==]=

Choose...

1. Push a (Integer) to our stack box.

2. Pop a (Integer) off our stack box.

3. Peek at top of the stack.

4. Exit

>> 2

Stack is empty. Caught EmptyStackException. [ 1 MARK ]

=[==[==[=[=================================]=]==]==]=

=[==[==[=[ Welcome to StackBox ]=]==]==]=

=[==[==[=[=================================]=]==]==]=

Choose...

1. Push a (Integer) to our stack box.

2. Pop a (Integer) off our stack box.

3. Peek at top of the stack.

4. Exit

>> 3

Stack is empty. Caught EmptyStackException. [ 1 MARK ]

=[==[==[=[=================================]=]==]==]=

=[==[==[=[ Welcome to StackBox ]=]==]==]=

=[==[==[=[=================================]=]==]==]=

Choose...

1. Push a (Integer) to our stack box.

2. Pop a (Integer) off our stack box.

3. Peek at top of the stack.

4. Exit

>> 4

Solutions

Expert Solution

StackBox.java

import java.util.ArrayList;
import java.util.EmptyStackException;
import java.util.Scanner;

public class StackBox<E>
{
   ArrayList<E> stack = new ArrayList<E>();
  
   //Tests if this stack is empty.
   boolean empty()
   {
       boolean result = stack.isEmpty();
       return result;
   }
  
   //Pushes an item onto the top of this stack. Returns item pushed.
   E push(E item)
   {
       stack.add(item);
       return item;
   }
  
   //Removes the object at the top of this stack and returns that object as the value of this function.
   //Throws EmptyStackException if empty.
   E pop()
   {
       if(empty())
       {
           throw new EmptyStackException();
       }
       E last_element = stack.get(stack.size() - 1); // the most recently pushed element is at the last index. SO we try to access it.
       stack.remove(stack.size() - 1); // Now remove that element from stack
return last_element;
   }
  
   //Looks at the object at the top of this stack without removing it from the stack.
   //Throws EmptyStackException if empty.
   E peek()
   {
       if(empty())
       {
           throw new EmptyStackException();
       }
       return stack.get(stack.size() - 1);
   }
  
   public static void main(String args[])
   {
       //Now create an instance of StackBox that holds Integers.
       StackBox<Integer> s = new StackBox<Integer>();
      
       //to take input we use scanner object
       Scanner scan = new Scanner(System.in);
       int input;
      
       while(true)
       {  
           //Create a menu that looks like this to test your StackBox:
           System.out.println("=[==[==[=[=================================]=]==]==]=");
           System.out.println("=[==[==[=[ Welcome to StackBox ]=]==]==]=");
           System.out.println("=[==[==[=[=================================]=]==]==]=");
           System.out.println("Choose...");
           System.out.println("1. Push a (Integer) to our stack box.");
           System.out.println("2. Pop a (Integer) off our stack box.");
           System.out.println("3. Peek at top of the stack.");
           System.out.println("4. Exit");
          
           input = scan.nextInt();
          
           switch(input)
           {
               case 1:
                   System.out.println("Enter an Integer you want to add to the StackBox >> ");
                   input = scan.nextInt();
                   s.push(input);
                   break;
                  
               case 2:
                   try
                   {
                       System.out.println("The element "+ s.pop() +" was removed from the top of the stack box.");
                   }
                   catch(EmptyStackException e)
                   {
                       System.out.println("Stack is empty. Caught EmptyStackException. ");
                   }          
                   break;
                  
               case 3:
                   try
                   {
                   System.out.println("The element currently at the top of the stack is "+s.peek());
                   }
                   catch(EmptyStackException e)
                   {
                       System.out.println("Stack is empty. Caught EmptyStackException. ");
                   }
                   break;
                  
               case 4:
                   scan.close();
                   return;
                  
               default:
                   System.out.println("Invalid option. Try again");
                   break;
           }
       }  
   }
}

Sample I/O and output


Related Solutions

StackBox Implement our own stack class patterned after Java's Stack class. Start with a generic class...
StackBox Implement our own stack class patterned after Java's Stack class. Start with a generic class that uses an ArrayList for storage of the elements: public class StackBox<E> { ArrayList<E> stack = new ArrayList<E>(); } Implement the following methods in StackBox: boolean empty() Tests if this stack is empty. E push(E item) Pushes an item onto the top of this stack. Returns item pushed. E pop() Removes the object at the top of this stack and returns that object as...
Implement a generic MyStack class using templates. A stack allows elements to be added and removed...
Implement a generic MyStack class using templates. A stack allows elements to be added and removed in a last-in, first-out (LIFO) manner. Stacks have an operation called push to place elements at the top of the stack, and another operation called pop to remove and return the element at the top of the stack. The only element on the stack that may be referenced is the one on the top. This means that if two elements are pushed onto the...
Implement the stack ADT in a fully generic manner (through the use of templates) by means...
Implement the stack ADT in a fully generic manner (through the use of templates) by means of a singly linked list. (Give your implementation “from scratch,” without the use of any classes from the Standard Template Library ).
1. Implement the stack abstract data type. Write it as a separate class called Stack. For...
1. Implement the stack abstract data type. Write it as a separate class called Stack. For simplicity the data type to be stored in the stack is String but you can also use generic type. 2. Test your class implementation by calling push() and pop(). If the stack is empty (size equals zero) pop() should return null and print that “stack is empty”. If stack is full (size equals max) push() returns false and prints that “stack is full”. This...
To write a Generic Collection class for Stack<E>, using the generic Node<E> from Lab 5, and...
To write a Generic Collection class for Stack<E>, using the generic Node<E> from Lab 5, and test it using a stack of Car, Integer, and String Stack<E> For Programming Lab 6, you are to write your own Generic Collection for a Stack that will use your Node<E> from Lab 5 UML for Stack<E> Stack<E> - top : Node<E> - numElements : int + Stack( ) + push( element : E ) : void + pop( ) : E + size(...
Implement a class named stack pair that provides a pair of stacks. Make the class a...
Implement a class named stack pair that provides a pair of stacks. Make the class a template class. So, you will have two files: stack pair.h and stack pair.template, following the style of the text. The basic idea is that two stacks can share a single static array. This may be advantageous if only one of the stacks will be in heavy use at any one time. • The class should have various methods to manipulate the stack: T pop...
the topic is STack and Generic stack in java It's really urgent!! Generic Stack due 10/16/2020...
the topic is STack and Generic stack in java It's really urgent!! Generic Stack due 10/16/2020 Friday 2 pm Mountain Time Generic Stack Make It Generic! The first part of this lab is making your stack generic. Take the code from your working StringStack class - everything except the main method - and paste it into the GenericStack class. Change the name of the constructors to match the new name of the class, then modify the whole class so it...
b. Implement StackFromList, a templated stack class backed by the above singlylinked list. The stack should...
b. Implement StackFromList, a templated stack class backed by the above singlylinked list. The stack should have a private linked list member, and utilize the linked list methods to implement its functionality. The stack should include a constructor, a destructor, a push, a pop, and an isEmpty method (which returns a bool). c. Implement, QueueFromList, a templated queue class backed by the above singlylinked list. The queue should have a private linked list member, and utilize the linked list methods...
0. Introduction. In this assignment you will implement a stack as a Java class, using a...
0. Introduction. In this assignment you will implement a stack as a Java class, using a linked list of nodes. Unlike the stack discussed in the lectures, however, your stack will be designed to efficiently handle repeated pushes of the same element. This shows that there are often many different ways to design the same data structure, and that a data structure should be designed for an anticipated pattern of use. 1. Theory. The most obvious way to represent a...
Generic Stack due 10/16/2020 Friday 12 pm Mountain Time Generic Stack Make It Generic! The first...
Generic Stack due 10/16/2020 Friday 12 pm Mountain Time Generic Stack Make It Generic! The first part of this lab is making your stack generic. Take the code from your working StringStack class - everything except the main method - and paste it into the GenericStack class. Change the name of the constructors to match the new name of the class, then modify the whole class so it uses generics, and can store any type that a programmer asks for....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT