In: Computer Science
IN JAVA
Step 1 Develop the following interface: Interface Name: ImprovedStackInterface Access Modifier: public Methods Name: push Access modifier: public Parameters: item (data type T, parameterized type) Return type: void Throws: StackFullException Name: push Access modifier: public Parameters: item1 (data type T, parameterized type), item2 (data type T, parameterized type) Return type: void Throws: StackFullException Name: pop Access modifier: public Parameters: none Return type: void Throws: StackEmptyException Name: doublePop Access modifier: public Parameters: none Return type: void Throws: StackEmptyException Name: top Access modifier: public Parameters: none Return type: T (parameterized type) Throws: StackEmptyException Step 2 Develop the following class: Class Name: StackFullException Access Modifier: public Extends: Exception Constructors Name: StackFullException Access modifier: public Parameters: none (default constructor) Task: makes a call to the default constructor of its superclass Name: StackFullException Access modifier: public Parameters: message (datatype String) Task: makes a call to the constructor of its superclass by passing the parameter message to it Step 3 Develop the following class: Class Name: StackEmptyException Access Modifier: public Extends: Exception Constructors Name: StackEmptyException Access modifier: public Parameters: none (default constructor) Task: makes a call to the default constructor of its superclass Name: StackEmptyException Access modifier: public Parameters: message (datatype String) Task: makes a call to the constructor of its superclass by passing the parameter message to it Step 4 Develop the following class: Class Name: ImprovedArrayBasedStack Access Modifier: public Implements: ImprovedStackInterface Instance variables Name: top Access modifier: private Data type: int Name: stack Access modifier: private Data type: T[] (an array of parameterized type) Constructors Name: ImprovedArrayBasedStack Access modifier: public Parameters: none (default constructor) Task: sets the value of top to -1 sets the stack to refer to an array of Objects with 100 elements which are type cast to T[] Name: ImprovedArrayBasedStack Access modifier: public Parameters: size (data type int) Task: sets the value of top to -1 sets the stack to refer to an array of Objects with the number of elements equal to the size parameter which are type cast to T[] Methods Name: push Access modifier: public Parameters: item (data type T, parameterized type) Return type: void Throws: StackFullException Task: if the value of top is less than the length of the stack minus 1 then increase the value of top by 1 and place the item at the top of the stack, otherwise throw a StackFullException with the message "Not enough room for one item" Name: push Access modifier: public Parameters: item1 (data type T, parameterized type), item2 (data type T, parameterized type) Return type: void Throws: StackFullException Task: if the value of top is less than the length of the stack minus 2, then increase the value of top by 1 and place item1 at the top of the stack, then increase the value of top by 1 and place item2 at the top of the stack, otherwise throw a StackFullException with the message "Not enough room for two items" Name: pop Access modifier: public Parameters: none Return type: void Throws: StackEmptyException Task: if the value of top is greater than -1 then remove the item at the top of the stack by replacing it with null and decrease the value of top by 1, otherwise throw a StackEmptyException with the message "No item to remove" Name: doublePop Access modifier: public Parameters: none Return type: void Throws: StackEmptyException Task: if the value of top is greater than 0, then remove the item at the top of the stack by replacing it with null and decrease the value of top by 1, then remove the item at the top of the stack by replacing it with null and decrease the value of top by 1, otherwise throw a StackEmptyException with the message "There are less than two items in the stack" Name: top Access modifier: public Parameters: none Return type: T (parameterized type) Throws: StackEmptyException Task: if the value of top is greater than -1 then return the item at the top of the stack, otherwise throw a StackEmptyException with the message "Top attempted on an empty stack" Step 5 Develop a class with only a main method in it: import java.util.Scanner; public class ImprovedStackDemo { public static void main(String[] args) { /* Inside of this main method do the following: Create an object of the Scanner class that takes input from System.in and refer to this object as keyboard Create a reference to a ImprovedStackInterface called myImprovedStack and have it refer to a new object of the ImprovedArrayBasedStack type passing the value of 6 as an argument to the constructor Open a do/while loop Prompt the user to pick one of the following options: Press 1 to push one item onto the stack Press 2 to push two items onto the stack Press 3 to pop the top of stack Press 4 to pop the top of the stack twice Press 5 to look at the top of the stack Press 6 to end the program Save the user’s input into the option variable if the user picks option 1, prompt the user for what they would like to push onto the stack then save that in a variable called item Open a try block and inside that block call the push method by passing item as a parameter and then close the try block Open a catch block catching StackFullException e and inside this catch block print out the value of message stored in the exception else if the user picks option 2, prompt the user for what they would like to push onto the stack then save that in a variable called item1 Prompt the user for the second item that they would like to push onto the stack and save that in a variable called item2 Open a try block and inside that block call the push method by passing item1 and item 2 as parameters and then close the try block Open a catch block catching StackFullException e and inside this catch block print out the value of the message stored in the exception else if the user picks option 3, Open a try block and call the pop method and close the try block Open a catch block catching StackEmptyException e and inside this catch block print out the value of the message stored in the exception else if the user picks option 4, Open a try block and call the doublePop method and close the try block Open a catch block catching StackEmptyException e and inside this catch block print out the value of the message stored in the exception else if the user picks option 5, Open a try block and print the value returned by the top method and close the try block Open a catch block catching StackEmptyException e and inside this catch block print out the value of the message stored in the exception else if the user picks option 6, display Goodbye. else if the user picks any other option, display Error! close the do/while loop and make it so that it continues to run as long as the user does not pick option 6 */ } }
PLEASE GIVE IT A THUMBS UP, I SERIOUSLY NEED ONE, IF YOU NEED ANY MODIFICATION THEN LET ME KNOW, I WILL DO IT FOR YOU
ImprovedStackInterface.java
package com.Solution;
public interface ImprovedStackInterface<T> //our interface
{
public void push(T item) throws StackFullException; //method to push single item
public void push(T item1, T item2) throws StackFullException; //method to push two items
public void pop() throws StackEmptyException; //method to pop single item
public void doublePop() throws StackEmptyException; //method to pop double items
public T top() throws StackEmptyException; //method to get the top element of stack
}
StackEmptyException.java:
package com.Solution;
public class StackEmptyException extends Exception //class to handle Stack empty exception or underflow condition
{
public StackEmptyException() //default constructor
{
super();
}
public StackEmptyException(String message) //Constructor with the error message
{
super(message);
}
}
StackFullException.java:
package com.Solution;
public class StackFullException extends Exception //class to handle Stack full exception or overflow condition
{
public StackFullException() //default constructor
{
super();
}
public StackFullException(String message) //constructor with error message as parameter
{
super(message);
}
}
ImprovedArrayBasedStack.java:
package com.Solution;
public class ImprovedArrayBasedStack<T> implements ImprovedStackInterface<T> //class to implement our stack
{
private int top; //to store the index of top element of stack
private T stack[]; //object array that will act as stack
public ImprovedArrayBasedStack() //default constructor
{
top = -1;
stack = (T[])(new Object[100]);
}
public ImprovedArrayBasedStack(int size) //parameterized constructor that will create our object array with given size
{
top = -1;
stack = (T[])(new Object[size]);
}
public void push(T item) throws StackFullException //function to push an element in stack
{
if(top<stack.length-1)
{
top+=1;
stack[top] = item;
}
else
{
throw new StackFullException("Not enough room for one item");
}
}
public void push(T item1, T item2) throws StackFullException //function to push two elements in the stack
{
if(top<stack.length-2)
{
top+=1;
stack[top] = item1;
top+=1;
stack[top] = item2;
}
else
{
throw new StackFullException("Not enough room for two items");
}
}
public void pop() throws StackEmptyException //function to pop one element from the stack
{
if(top>-1)
{
stack[top] = null;
top -= 1;
}
else
{
throw new StackEmptyException("No item to remove");
}
}
public void doublePop() throws StackEmptyException //function to pop two elements from the stack
{
if(top>0)
{
stack[top] = null;
top -= 1;
stack[top] = null;
top -= 1;
}
else
{
throw new StackEmptyException("There are less than 2 items in the stack");
}
}
public T top() throws StackEmptyException //function to get the top element of stack
{
if(top>-1)
{
return stack[top];
}
else
{
throw new StackEmptyException("Top attempted on an empty stack");
}
}
}
ImprovedStackDemo.java:
package com.Solution;
import java.util.Scanner;
public class ImprovedStackDemo
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in); //to get user input
ImprovedStackInterface<String> myImprovedStack = new ImprovedArrayBasedStack<String>(6); //creating our stack with size 6
int quit = 0; //to set condition for loop to exit
do
{
System.out.println("Pick an option :\n" +
"Press 1 to push one item onto the stack\n" +
"Press 2 to push two items onto the stack\n" +
"Press 3 to pop the top of the stack\n" +
"Press 4 to pop the top of the stack twice\n" +
"Press 5 to look at the top of the stack\n" +
"Press 6 to end the program"); //printing choices to user
int option = scanner.nextInt(); //taking user input based on the choices offered
scanner.nextLine();
if(option == 1) //choice 1 means add an element
{
System.out.print("Please Enter the element : ");
String item = scanner.nextLine(); //take element from user
try //try adding element
{
myImprovedStack.push(item);
}
catch(StackFullException e) //if error occurs print the error message
{
System.out.println(e.getMessage());
}
}
else if(option == 2) //choice 2 means add two elements
{
System.out.print("Please Enter the first element : ");
String item1 = scanner.nextLine(); //taking input for element 1
System.out.print("Please Enter the second element : ");
String item2 = scanner.nextLine(); //taking input for element 2
try //try adding both elements
{
myImprovedStack.push(item1, item2);
}
catch(StackFullException e) //if error occurs print the error message
{
System.out.println(e.getMessage());
}
}
else if(option == 3) //choice 3 means remove the top element from stack
{
try //try removing the element
{
myImprovedStack.pop();
}
catch(StackEmptyException e) //if error occurs print the error message
{
System.out.println(e.getMessage());
}
}
else if(option == 4) //choice 4 means remove top two elements from stack
{
try //try removing top two elements from stack
{
myImprovedStack.doublePop();
}
catch(StackEmptyException e) //if error occurs print the error message
{
System.out.println(e.getMessage());
}
}
else if(option == 5) //choice 5 means print the top element of stack
{
try
{
System.out.println("Top is : " + myImprovedStack.top());
}
catch(StackEmptyException e) //if error occurs print the error message
{
System.out.println(e.getMessage());
}
}
else if(option == 6) //choice 6 means exit and here we will set the condition of loop to be false
{
System.out.println("Goodbye!");
quit = 1; //this will set condition of loop to be false
}
}while(quit!=1);
}
}