Question

In: Computer Science

Write a program that prompts the user to insert an item into a stack, delete an...

Write a program that prompts the user to insert an item into a stack, delete an item from the stack, or display all the items in the stack. Assume that the stack maximum capacity is 10. Here is sample run

. Stack operation menu:

1. Insert

2. Delete

3. Display

4. Quit

Solutions

Expert Solution

Stack.java

/*** The following implements functionalities for the user to insert
* an item into a stack, delete an item from the stack,
* or display all the items in the stack. */
import java.util.Scanner;
public class Stack {
/** maxCapacity specifies the maximum size of the stack array */
int maxCapacity;
/** stack[] is the integer array for storing the items of the stack */
int stack[];   
/** top always specifies the position of last element added to the stack
* and which is also the position of first item popped out of the stack */
int top;

/** Constructor to initialize the maxCapactiy of the stack. Also top is
* initialized with -1 to specify empty stack.
*/
public Stack(int size)
{
maxCapacity = size;
stack = new int[maxCapacity];
top = -1;
}
  
/** isFull() method checks if all the array positions got filled up with items.
* It is identified by checking the top position with the maxCapacity.
* If it is equal, true is returned otherwise
* false is returned.
*/
public boolean isFull()
{
if (top < maxCapacity )
return false;
else
return true;
}
  
/** isEmpty() checks if the stack is empty. This is done by checking it top=-1
*/
public boolean isEmpty()
{
if(top == -1)
return true;
else
return false;
}
  
/** Push method is used to insert an item into the stack. It calls isFull() method to
* check if stack is full. If so, it displays error message.
* If stack is not full, increment the top position by 1 and insert the new element in the
* postion of top.
*/
public void push(int item) {
if(isFull()==true) {
System.out.println("Stack Overflow !");
}
else
{
top++;
stack[top] = item;
System.out.println("Item " + item + " is pushed onto Stack !");
}   
}

/** pop method is used to take the item at the top location and decrement the value of top by 1
* Pop method should check for items in the stack. It does that by calling isEmpty() function.
* If isEmpty() returns false, it means that there are items in the stack and pop can be done
*/
public int pop() {
int item;
if (isEmpty() == false) {
item = stack[top];
top--;
return(item);
} else {
return -1;
}
}
  
/** The method is used to print the elements of the stack. A simple for loop is used to read
* teh elements of the array and print the stack contents. The contents are printed in the
* reverse order just to show the Last Inserted is Found First (LIFO) of stack.
*/
public void displayStack() {
if (isEmpty() == false) {
System.out.println("\nItems in stack are :");
for (int i = top; i >= 0; i--) {
System.out.println(stack[i]);
}
} else {
System.out.println("\nStack Underflow !No more items in the stack");
}
}

/** Driver method to create an object of Stack class and call all the methods of stack */
public static void main(String[] args) {
Stack stk = new Stack(10);
Scanner sc = new Scanner(System.in);
int item;
int choice;
  
do
{
System.out.println("1. Insert an item onto stack");
System.out.println("2. Delete an item from stack");
System.out.println("3. Display items of stack");
System.out.println("4. Exit");
  
System.out.print("Enter your choice :: ");
choice = sc.nextInt();
  
switch(choice)
{
case 1:
System.out.print("Type the item to push into the stack :: ");
item = sc.nextInt();
stk.push(item);
break;
case 2:
item = stk.pop();
if(item!=-1)
System.out.println("Item " + item + " is popped off from stack");
else
System.out.println("No more items in the stack");
break;
case 3:
System.out.println("\nContents of the stack");
stk.displayStack();
break;
case 4:
System.out.println("Thank you!!!");
break;
default:
System.out.println("Invalid Option");

}
}while(choice!=4);
}
}

==================================================================================

===========================================================================================

Sample output


Related Solutions

write this program in C++ Write a program that prompts a user for three characters. The...
write this program in C++ Write a program that prompts a user for three characters. The program must make sure that the input is a number 10 - 100 inclusive. The program must re prompt the user until a correct input is entered. Finally output the largest and the lowest value. Example 1: Input : 10 Input : 20 Input : 30 The largest is 30. The lowest is 10. Example 2: Input : 100 Input : 50 Input :...
Please Write C++ PROGRAM : That will write a program that initially prompts the user for...
Please Write C++ PROGRAM : That will write a program that initially prompts the user for a file name. If the file is not found, an error message is output, and the program terminates. Otherwise, the program prints each token in the file, and the number of times it appeared, in a well formatted manner. To accomplish all this, do the following: - Open the file - the user must be prompted and a file name input. DO NOT hardcode...
Write a C++ Program Write a program that prompts the user to input a string. The...
Write a C++ Program Write a program that prompts the user to input a string. The program then uses the function substr to remove all the vowels from the string. For example, if str=”There”, then after removing all the vowels, str=”Thr”. After removing all the vowels, output the string. Your program must contain a function to remove all the vowels and a function to determine whether a character is a vowel. You must insert the following comments at the beginning...
JAVA Program Write a program that prompts the user for data until the user wishes to...
JAVA Program Write a program that prompts the user for data until the user wishes to stop (you must have a while loop) (You must read in at least 8 to 10 sets of voter data using dialog boxes) The data to read in is: The registration of the voter (Democrat, Republican or other) The gender of the voter The Presidential candidate the voter is choosing (Trump or Biden) Which candidate has done better to manage the economy? (Trump or...
Write a program that prompts the user to input a string. The program then uses the...
Write a program that prompts the user to input a string. The program then uses the function substr to remove all the vowels from the string. For example, if str=”There”, then after removing all the vowels, str=”Thr”. After removing all the vowels, output the string. Your program must contain a function to remove all the vowels and a function to determine whether a character is a vowel. You must insert the following comments at the beginning of your program and...
Write a C++ program that prompts the user for the radius of a circle and then...
Write a C++ program that prompts the user for the radius of a circle and then calls inline function circleArea to calculate the area of that circle. It should do it repeatedly until the user enters -1. Use the constant value 3.14159 for π Sample: Enter the radius of your circle (-1 to end): 1 Area of circle with radius 1 is 3.14159 Enter the radius of your circle (-1 to end): 2 Area of circle with radius 2 is...
Write a program in C that prompts the user for a number of seconds and then...
Write a program in C that prompts the user for a number of seconds and then converts it to h:m:s format. Example: 5000 seconds should display as 1:23:20 (1 hour, 23 minutes, 20 seconds.) Test with several values between about 100 seconds and 10,000 seconds. use unint and remainders for this and keep it as simple as possible.
Write a program which: Prompts the user for a positive integer >= 0 Validates the user...
Write a program which: Prompts the user for a positive integer >= 0 Validates the user input to ensure it is a positive integer >= 0 Allocate (dynamically) an array big enough for the data. Load the array with random numbers ranging in value from1 to 100 Display the elements of the array (unsorted) Display the elements of the array (sorted) Display the average Display the median Display the mode, if none, display appropriate message RESTRICTIONS No global variables No...
C++ code Inventory Item Stack You are writing an Inventory program that will allow the user...
C++ code Inventory Item Stack You are writing an Inventory program that will allow the user to enter a part into the inventory, take a part from the inventory, or quit. You are provided with the InvItem class (InvItem.h) and a partial Driver.cpp. You will be creating a DynamicStack class that should implement a Stack data structure. The DynamicClass should be implemented as a template class to allow any data type be added/removed from the stack. You will submit three...
Write a program that prompts the user to enter a positive integer and then computes the...
Write a program that prompts the user to enter a positive integer and then computes the equivalent binary number and outputs it. The program should consist of 3 files. dec2bin.c that has function dec2bin() implementation to return char array corresponding to binary number. dec2bin.h header file that has function prototype for dec2bin() function dec2binconv.c file with main function that calls dec2bin and print results. This is what i have so far. Im doing this in unix. All the files compiled...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT