In: Computer Science
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
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