0% found this document useful (0 votes)
19 views3 pages

Java Stack Class Implementation Guide

The document provides a Java implementation of a stack class that can hold a maximum of 10 integers, including methods for pushing, popping, peeking, displaying contents, and checking if the stack is empty or full. It also includes a main method that presents a menu-driven interface for users to perform stack operations. The stack handles overflow and underflow conditions with appropriate messages.

Uploaded by

Swagath Nevagi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views3 pages

Java Stack Class Implementation Guide

The document provides a Java implementation of a stack class that can hold a maximum of 10 integers, including methods for pushing, popping, peeking, displaying contents, and checking if the stack is empty or full. It also includes a main method that presents a menu-driven interface for users to perform stack operations. The stack handles overflow and underflow conditions with appropriate messages.

Uploaded by

Swagath Nevagi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

2. Develop a stack class to hold a maximum of 10 integers with suitable methods.

Develop a JAVA main method to illustrate Stack operations.

import [Link];
public class Stack {
private static final int MAX_SIZE = 10;
private int[] stackArray;
private int top;
public Stack() {
stackArray = new int[MAX_SIZE];
top = -1;
}
public void push(int value) {
if (top < MAX_SIZE - 1) {
stackArray[++top] = value;
[Link]("Pushed: " + value);
} else {
[Link]("Stack Overflow! Cannot push " + value + ".");
}
}
public int pop() {
if (top >= 0) {
int poppedValue = stackArray[top--];
[Link]("Popped: " + poppedValue);
return poppedValue;
} else {
[Link]("Stack Underflow! Cannot pop from an empty stack.");
return -1; // Return a default value for simplicity
}
}
public int peek() {
if (top >= 0) {
[Link]("Peeked: " + stackArray[top]);
return stackArray[top];
} else {
[Link]("Stack is empty. Cannot peek.");
return -1; // Return a default value for simplicity
}
}
public void display() {
if (top >= 0) {
[Link]("Stack Contents: ");
for (int i = 0; i <= top; i++) {
[Link](stackArray[i] + " ");
}
[Link]();
} else {
[Link]("Stack is empty.");
}
}
public boolean isEmpty() {
return top == -1;
}
public boolean isFull() {
return top == MAX_SIZE - 1;
}

public static void main(String[] args) {


Stack stack = new Stack();
Scanner scanner = new Scanner([Link]);
int choice;
do {
[Link]("\nStack Menu:");
[Link]("1. Push");
[Link]("2. Pop");
[Link]("3. Peek");
[Link]("4. Display Stack Contents");
[Link]("5. Check if the stack is empty");
[Link]("6. Check if the stack is full");
[Link]("0. Exit");
[Link]("Enter your choice: ");
choice = [Link]();
switch (choice) {
case 1:
[Link]("Enter the value to push: ");
int valueToPush = [Link]();
[Link](valueToPush);
break;
case 2:
[Link]();
break;
case 3:
[Link]();
break;
case 4:
[Link]();
break;
case 5:
[Link]("Is the stack empty? " + [Link]());
break;
case 6:
[Link]("Is the stack full? " + [Link]());
break;
case 0:
[Link]("Exiting the program. Goodbye!");
break;
default:
[Link]("Invalid choice. Please try again.");
}
} while (choice != 0);
[Link]();
}

You might also like