Question

In: Computer Science

IN JAVA Step 1 Develop the following interface: Interface Name: ImprovedStackInterface Access Modifier: public Methods Name:...

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 */ } }

Solutions

Expert Solution

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);

    }
}

Related Solutions

Develop the following class: Class Name: ImprovedArrayBasedStack Access Modifier: public Implements: ImprovedStackInterface Instance variables Name: top...
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...
write program that develop a Java class Dictionary to support the following public methods of an...
write program that develop a Java class Dictionary to support the following public methods of an abstract data type: public class Dictionary { // insert a (key, value) pair into the Dictionary, key value must be unique, the value is associated with the key; only the last value inserted for a key will be kept public void insert(String key, String value); // return the value associated with the key value public String lookup(String key); // delete the (key, value) pair...
Finish the following java question: Consider the following interface: interface Duty { public String getDuty(); }...
Finish the following java question: Consider the following interface: interface Duty { public String getDuty(); } Write a class called Student which implements Duty. Class Student adds 1 data field, id, and 2 methods, getId and setId, along with a 1-argument constructor. The duty of a Student is to study 40 hours a week. Write a class called Professor which implements Duty. Class Professor adds 1 data field, name, and 2 methods, getName and setName, along with a 1-argument constructor....
JAVA Add static methods largest and smallest to the Measurable interface. The methods should return the...
JAVA Add static methods largest and smallest to the Measurable interface. The methods should return the object with the largest or smallest measure from an array of Measurable objects.
Consider the following interface: public interface Car{ public String getMake(); public void setMake(); public void honk();...
Consider the following interface: public interface Car{ public String getMake(); public void setMake(); public void honk(); public void crash(); public void drive(); } public interface Boat{ public String getMake (); public void setMake (); public void blast_horn(); public void sink(); public void move(); } 1. Create a concrete FamilyCar class from the Car interface.
java: Given the definitions: public interface ActionListener { public void actionPerformed(ActionEvent e); } public JTextField {...
java: Given the definitions: public interface ActionListener { public void actionPerformed(ActionEvent e); } public JTextField { public JTextField(){} public void setText(String text) {} public String getText() {} } Write the code to create a JButton on the South of the window and a JTextField on the North. The first time you click on the button, it should print out “hello!” on the JTextField. For the second time, should show “hello!hello!”. For the third time, should show “hello!hello!hello!”.
JAVA 1. Create an Interface Vehicle. Declare one public void method in it paint( ). 2....
JAVA 1. Create an Interface Vehicle. Declare one public void method in it paint( ). 2. Create a class Car. Implements Vehicle. It's paint() method prints "Car Painted". It's drive( ) method prints "Car Driven". 3. Create a class Bus. Implements Vehicle. It's paint() method prints "Bus Painted" . It's drive( ) method prints "Bus Driven".   4. Create a method AutoWork which accepts an object of type Vehicle. The method makes a call to the vehicle's paint( ) and drive()...
Using Java The given class SetInterface.java is : public interface SetInterface<T> {    /**    *...
Using Java The given class SetInterface.java is : public interface SetInterface<T> {    /**    * Gets the current number of entries in this set.    *    * @return The integer number of entries currently in the set.    */    public int getSize();    /**    * Sees whether this set is empty.    *    * @return True if the set is empty, or false if not.    */    public boolean isEmpty();    /**    *...
URGENT JAVA Develop a Java computer program to simulate access control to a system network. The...
URGENT JAVA Develop a Java computer program to simulate access control to a system network. The username and password authentication mechanism is used as an access control mechanism in this project. The program needs to authenticate the user with their username and hashed password stored in Access Manager User (AMU) file. To create an Access Manager User file, you need to develop a java program that takes username and password as input and then generates a file containing the username,...
from Big Java Early Objects 7th edition Question Declare an interface Filter as follows: public interface...
from Big Java Early Objects 7th edition Question Declare an interface Filter as follows: public interface Filter { boolean accept(Object x); } Modify the implementation of the Data class in Section 10.4 to use both a Measurer and a Filter object. Only objects that the filter accepts should be processed. Demonstrate your modification by processing a collection of bank accounts, filtering out all accounts with balances less than $1,000. Solve Exercise •• P10.6, using a lambda expression for the filter....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT