Create a Stack class for integers. Within the stack class, store up to 10 intege
ID: 3572581 • Letter: C
Question
Create a Stack class for integers.
Within the stack class, store up to 10 integers in an array.
Include a push method that adds an integer to the stack.
Include a pop method that removes an integer from the stack.
When an integer is popped, use it's position in the stack for the next push.
Include an isFull method that checks to see if the stack is full.
Include an isEmpty method that checks to see if the stack is empty.
Include an output method to display the contents of the stack in LIFO order (the last number pushed should be the first number displayed). Include a message with the display stating what is being displayed. Display the stack on one line with spaces between the numbers.
Do not ask for any input in the Stack class. All input will be taken in the Demo class and then passed to the Queue class.
Create a Queue class for integers.
Within the queue class, store up to 10 integers in an array.
Include a push method that adds an integer to the queue.
Include a pop method that removes an integer from the queue.
When an integer is popped, reorder the queue so that the second integer is now the first, etc. and the last used position will be the next one pushed onto.
Include an isFull method that checks to see if the queue is full.
Include an isEmpty method that checks to see if the queue is empty.
Include an output method to display the contents of the queue in FIFO order (the first number pushed should be the first number displayed). Include a message with the display stating what is being displayed. Display the queue on one line with spaces between the numbers.
Do not ask for any input in the Queue class. All input will be taken in the Demo class and then passed to the Queue class.
Create a demo class that contains the main method.
Create a stack object and a queue object.
Inside a loop, create a menu for the user. The choices are:
Add a number to the stack.
Remove a number from the stack.
Display the stack.
Add a number to the queue.
Remove a number from the queue.
Display the queue.
Quit.
If the user chooses options 1 or 4:
Check to see if the chosen data structure is full. If it is, display a polite message stating so and display the contents of the chosen data structure.
If it is not full, push a random number between 1 and 100 (use Math.random()) onto the chosen data structure and display the number being pushed and the contents of the chosen data structure.
Loop back to the menu.
If the user chooses options 2 or 5:
Check to see if the chosen data structure is empty. If it is, display a polite message stating so.
If it is not empty, pop a number off of the chosen data structure and display the number being popped and the remaining contents of the chosen data structure.
Loop back to the menu.
If the user chooses options 3 or 6 then display the contents of the chosen data structure and loop back to the menu.
If the user chooses option 7 then exit the loop.
Explanation / Answer
1)
class Stack {
int stck[] = new int[10];
int tos;
// Initialize top-of-stack
Stack() {
tos = -1;
}
// Push an item onto the stack
void push(int item) {
if(tos==9)
System.out.println("Stack is full.");
else
stck[++tos] = item;
}
// Pop an item from the stack
int pop() {
if(tos < 0) {
System.out.println("Stack underflow.");
return 0;
}
else
2)
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class Queue_Ex1 {
Scanner scan;
Queue<Integer> queue;
int n;
void insert() {
scan = new Scanner(System.in);
queue = new LinkedList<Integer>();
System.out.println("Integer Queue - Insert & Delete");
System.out.println(" Enter 'n' value :");
n = scan.nextInt();
System.out.println("Enter the elements");
for(int i=0; i<n; i++) {
queue.add(scan.nextInt());
}
}
void delete() {
System.out.println(" The Queue");
while(!queue.isEmpty()) {
System.out.println(queue.poll());
}
}
}
class MainClass {
public static void main(String args[]) {
Queue_Ex1 obj = new Queue_Ex1();
obj.insert();
obj.delete();;
}
}
return stck[tos--];
}
}