Java ArrayList Inclass Exercise It is often the case that ne
Java ArrayList In-class Exercise
It is often the case that new data structures are written using other existing data structures as an underlying model. In this practice exercise we will be creating a Stack class using Java’s ArrayList as a basis for our new class.
A Stack data structure behaves similarly to a stack of papers. There are restrictions on adding or removing papers from the stack:
1. You may only add papers to the top of a stack, referred to as a “push” operation.
2. You may only remove papers from the top of a stack, referred to as a “pop” operation.
3. You may also view the paper on top of the stack, referred to as a “peek” operation. This does not remove the top paper.
4. You cannot access papers below the top paper on your stack
Because of these restrictions, a Stack data type is referred to as a LIFO structure (last-in-first-out).
Note: because we only allow access to elements at the top of a Stack, this removes the index-based access of structures such as Arrays and ArrayLists
Write a new Stack class using an ArrayList as the underlying storage structure (in other words, a private instance field). You can simulate the behavior described above by adding/removing items from one side of your ArrayList. Which side to use is up to you, but I would suggest analyzing which side of an ArrayList is best to use.
Use the following class diagram as a basis for your class:
Stack<T>
- data : ArrayList<T>
+ Stack()
+ pop() : T
+ push(newElement : T) : void
+ peek() : T
+ isEmpty() : boolean
+ size() : int
Note: your Stack<T> class should throw an appropriate exception when pop() is called on an empty Stack.
Write a driver class to show that your Stack class is working correctly.
Challenge: create a new class called BoundedStack that enforces a maximum number of elements in your stack.
1. Use the code from your Stack class as a starting point in your new BoundedStack class
2. Include only a parameterized constructor that takes a positive integer that specifies the maximum number of elements in your bounded stack
3. Throw an appropriate exception if you stack size exceeds the bounds given
Solution
Java Code for all the Stack Operations Mentioned above:-
The below Two cases also handled by this code:-
1. Stack class should throw an appropriate message when pop() is called on an empty Stack.
2. Showing appropriate Message or Exception if you stack size exceeds the bounds given
class BoundedStack {
private static int maxSize,i,j;
private static int[] stackArray;
private static int top;
//Constructor to initialize the Stack
public BoundedStack(int s) {
maxSize = s;
stackArray = new int[maxSize];
top = -1;
}
//Push an Element into the Stack and display the elements from Top to bottom
public static void push(int j) {
if(top == maxSize){
System.out.println(\"Stack is Full!! Can\'t push new items to this bounded Stack\");
}
else{
stackArray[++top] = j;
System.out.println(\"Element \"+ j + \" is pushed to the stack\");
printElements();
}
}
//Pop the top element in the Stack and display it in the console
public static void pop() {
if(top == -1){
System.out.println(\"The Stack is Empty !! We can\'t pop an Element in the Empty Stack\");
}
int elem = stackArray[top--];
System.out.println(\"Pop Operation Done!!\");
System.out.println(elem + \" is Popped!!\");
printElements();
}
//SHow the Top element in the Stack
public static void peek() {
if(top == -1){
System.out.println(\"The Stack is Empty !! We can\'t peek an Element in the Empty Stack\");
}
int elem = stackArray[top];
System.out.println(\"Peek is : \" + elem);
printElements();
}
//Check Whether the Stack is Empty
public static void isEmpty() {
System.out.print(\"Is the Stack Empty? \");
System.out.println(top == -1);
}
//Method to Print all the Elements in the STack from top to bottom
public static void printElements(){
System.out.println(\"Elements in the Stack are(Top ---> Bottom)\");
for(i = top ; i>=0 ; i-- ){
System.out.print(stackArray[i]+\" \");
}
System.out.println(\"\ \");
}
}
Driver class to test whether the program is working correctly
public class MyStack{
//Driver Main Method
public static void main(String[] args) {
//Create a Stack Object to test all the operation
BoundedStack theStack = new BoundedStack(10);
theStack.push(23);
theStack.push(2);
theStack.push(73);
theStack.push(21);
theStack.pop();
theStack.peek();
theStack.isEmpty();
System.out.println(\"\");
}
}
Output:-
Element 23 is pushed to the stack
Elements in the Stack are(Top ---> Bottom)
23
Element 2 is pushed to the stack
Elements in the Stack are(Top ---> Bottom)
2 23
Element 73 is pushed to the stack
Elements in the Stack are(Top ---> Bottom)
73 2 23
Element 21 is pushed to the stack
Elements in the Stack are(Top ---> Bottom)
21 73 2 23
Pop Operation Done!!
21 is Popped!!
Elements in the Stack are(Top ---> Bottom)
73 2 23
Peek is : 73
Elements in the Stack are(Top ---> Bottom)
73 2 23
Is the Stack Empty? false


