Program: 2
Develop a stack class to hold a maximum of 10 integers with suitable methods.
Develop a JAVA main method to illustrate Stack operations.
Source Code
import [Link];
class Stack {
private int[] elements;
private int top;
public Stack() {
elements = new int[10];
top = -1;
}
public boolean isEmpty() {
return top == -1;
}
public boolean isFull() {
return top == 9;
}
public void push(int element) {
if (isFull()) {
[Link]("Stack is full. Cannot push more elements.");
} else {
elements[++top] = element;
[Link]("Pushed: " + element);
}
}
public void pop() {
if (isEmpty()) {
[Link]("Stack is empty. Cannot pop elements.");
} else {
int poppedElement = elements[top--];
[Link]("Popped: " + poppedElement);
}
}
public void printStack() {
if (isEmpty()) {
[Link]("Stack is empty.");
} else {
[Link]("Stack: ");
for (int i = 0; i <= top; i++) {
[Link](elements[i] + " ");
}
[Link]();
}
}
}
public class Main {
public static void main(String[] args) {
Stack stack = new Stack();
while(true)
{
[Link]("Stack Operations");
[Link]("1. Push");
[Link]("2. Pop");
[Link]("3. Display");
[Link]("4. Exit");
Scanner scanner = new Scanner([Link]);
[Link]("Enter your Choice: ");
int choice = [Link]();
switch(choice)
{
case 1: [Link]("Enter Number to push: ");
int num = [Link]();
[Link](num);
break;
case 2:
[Link]();
break;
case 3: [Link]();
break;
case 4: [Link](0);
break;
default: [Link]("Invalid choice ");
}
}
}
}
Output:
java Main
Stack Operations
1. Push
2. Pop
3. Display
4. Exit
Enter your Choice: 1
Enter Number to push:
10
Pushed: 10
Stack Operations
1. Push
2. Pop
3. Display
4. Exit
Enter your Choice: 1
Enter Number to push:
20
Pushed: 20
Stack Operations
1. Push
2. Pop
3. Display
4. Exit
Enter your Choice: 2
Popped: 20
Stack Operations
1. Push
2. Pop
3. Display
4. Exit
Enter your Choice:
----jGRASP: Process ended by user.
----jGRASP exec: java Main
Stack Operations
1. Push
2. Pop
3. Display
4. Exit
Enter your Choice: 1
Enter Number to push:
10
Pushed: 10
Stack Operations
1. Push
2. Pop
3. Display
4. Exit
Enter your Choice: 1
Enter Number to push:
20
Pushed: 20
Stack Operations
1. Push
2. Pop
3. Display
4. Exit
Enter your Choice: 1
Enter Number to push:
30
Pushed: 30
Stack Operations
1. Push
2. Pop
3. Display
4. Exit
Enter your Choice: 1
Enter Number to push:
40
Pushed: 40
Stack Operations
1. Push
2. Pop
3. Display
4. Exit
Enter your Choice: 1
Enter Number to push:
50
Pushed: 50
Stack Operations
1. Push
2. Pop
3. Display
4. Exit
Enter your Choice: 1
Enter Number to push:
60
Pushed: 60
Stack Operations
1. Push
2. Pop
3. Display
4. Exit
Enter your Choice: 1
Enter Number to push:
70
Pushed: 70
Stack Operations
1. Push
2. Pop
3. Display
4. Exit
Enter your Choice: 1
Enter Number to push:
80
Pushed: 80
Stack Operations
1. Push
2. Pop
3. Display
4. Exit
Enter your Choice: 1
Enter Number to push:
90
Pushed: 90
Stack Operations
1. Push
2. Pop
3. Display
4. Exit
Enter your Choice: 1
Enter Number to push:
100
Pushed: 100
Stack Operations
1. Push
2. Pop
3. Display
4. Exit
Enter your Choice: 1
Enter Number to push:
110
Stack is full. Cannot push more elements.
Stack Operations
1. Push
2. Pop
3. Display
4. Exit
Enter your Choice: 2
Popped: 100
Stack Operations
1. Push
2. Pop
3. Display
4. Exit
Enter your Choice: 2
Popped: 90
Stack Operations
1. Push
2. Pop
3. Display
4. Exit
Enter your Choice: 2
Popped: 80
Stack Operations
1. Push
2. Pop
3. Display
4. Exit
Enter your Choice: 2
Popped: 70
Stack Operations
1. Push
2. Pop
3. Display
4. Exit
Enter your Choice: 3
Stack: 10 20 30 40 50 60