.Implement a stack using exception handling and interfaces. In stack push(option 1) ,pop(option 2) and display(option 3) operation are done. Display the element specifying the index position.In display on trying to access an element out of bounds print the exception message.
Question
.Implement a stack using exception handling and interfaces. In stack push(option 1) ,pop(option 2) and display(option 3) operation are done. Display the element specifying the index position.In display on trying to access an element out of bounds print the exception message.
Solution
Here is a simple implementation of a stack in Java using exception handling and interfaces.
import java.util.Scanner;
interface StackOperations {
void push(int item);
int pop();
void display(int index);
}
class Stack implements StackOperations {
private int[] stackArray;
private int top;
public Stack(int size) {
stackArray = new int[size];
top = -1;
}
public void push(int item) {
try {
stackArray[++top] = item;
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Stack is full. Cannot push " + item);
top--;
}
}
public int pop() {
try {
return stackArray[top--];
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Stack is empty. Cannot pop");
top++;
return -1;
}
}
public void display(int index) {
try {
System.out.println("Element at index " + index + " is: " + stackArray[index]);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Invalid index " + index + ". " + e.getMessage());
}
}
}
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the size of the stack");
int size = scanner.nextInt();
Stack stack = new Stack(size);
while (true) {
System.out.println("Enter 1 to push, 2 to pop, 3 to display an element by index, 4 to quit");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.println("Enter the element to push");
int item = scanner.nextInt();
stack.push(item);
break;
case 2:
System.out.println("Popped element is: " + stack.pop());
break;
case 3:
System.out.println("Enter the index of the element to display");
int index = scanner.nextInt();
stack.display(index);
break;
case 4:
System.exit(0);
}
}
}
}
This program first asks for the size of the stack. Then it enters an infinite loop where it asks for the operation to perform. If the user enters 1, it asks for the element to push onto the stack. If the user enters 2, it pops an element from the stack. If the user enters 3, it asks for the index of the element to display. If the user enters 4, it quits the program. If the stack is full and the user tries to push an element, it catches the ArrayIndexOutOfBoundsException and prints a message. Similarly, if the stack is empty and the user tries to pop an element, or if the user tries to display an element at an invalid index, it catches the ArrayIndexOutOfBoundsException and prints a message.
Similar Questions
What happens when you try to pop an element from an empty stack? The program crashes. The top element is returned and removed. An error message is displayed. Nothing happens.
Problem Statement:Write a program that implements the Stack Data structure using ArraysThe operations performed on the Stack arePushPopPeekInput format:The first line of input contains the number of operations to be performed on the stackThe next lines contain the integers separated by space in which the first integer indicates the operation to be performed and the second integer contains the element to be pushed.1 ---> indicates the Push2 ---> indicates the Pop3 ---> indicates the PeekOutput format:Display every element after performing the peek operationDisplay the stack after performing all operations at the end.Sample Test Case:Input:61 21 31 4231 5Output:35 3 2Explanation:1 2 ---> 2 will be pushed1 3 ---> 3 will be pushed1 4 ---> 4 will be pushed2 ---> Last element(4) is popped3 ---> peek operation is performed which results in printing the top element of the stack i.e 31 5 ---> 5 will be pushed6 Operations are completedTherefore one peek operation is performed so the output is 3 and the final stack 5 3 2Note:If the stack is empty and when pop and peek are performed first, proceed to the next operation without displaying and modifying anything in the stack.If the stack is empty and nothing is pushed to the stack, Print Empty at the end.Sample Test CasesTest Case 1:Expected Output:61 21 31 4231 535·3·2
By throwing an exceptionBy not allowing the operationBy also popping the corresponding minimum element from the minimum stackBy replacing the popped element with the next minimum element
The retrieval of items in a stack is ........... operation. Options push pop access None of these
What error condition is triggered when you attempt to push an element onto a full stack?*1 pointa. Stack Overflowb. Stack Underflowc. Invalid Operationd. Stack Limit Exceeded
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.