Question

In: Computer Science

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 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"

Solutions

Expert Solution

The Implementation of the all classes are provided below:

// ImprovedStackInterface is an interface which contains all the methods used for stack implementation
public interface ImprovedStackInterface<T> {
        public  void push(T item ) throws StackFullException;
        public  void push(T item1, T item2) throws StackFullException;
        public void pop() throws StackEmptyException;
        public void doublePop() throws StackEmptyException;
        public T top() throws StackEmptyException;
}

//ImprovedArrayBasedStack class implements the ImprovedStackInterface as per the question
public class ImprovedArrayBasedStack<T> implements ImprovedStackInterface<T> {
        private int top;
        private T[] stack;
        
        public ImprovedArrayBasedStack(){
                top=-1;
                stack = (T[])new Object[100];           
        }       
        public ImprovedArrayBasedStack(int size){
                top=-1;
                stack = (T[])new Object[size];  
        }
        
        public void push(T item ) throws StackFullException{
                if(top<(stack.length-1)){
                        top=top+1;
                        stack[top]=item;
                }else{
                        throw new StackFullException("Not enough room for one item");
                }
        }
        public void push(T item1, T item2) throws StackFullException{
                if(top<(stack.length-2)){
                        top=top+1;
                        stack[top]=item1;
                        top=top+1;
                        stack[top]=item2;
                }else{
                        throw new StackFullException("Not enough room for two items");
                }
        }
        public void pop() throws StackEmptyException{
                if(top>-1){
                        stack[top]=null;
                        top=top-1;
                }else{
                        throw new StackEmptyException("No item to remove");
                }
                
        }
        public void doublePop() throws StackEmptyException{
                if(top>0){
                        stack[top]=null;
                        top=top-1;
                        stack[top]=null;
                        top=top-1;
                }else{
                        throw new StackEmptyException("There are less than two items in the stack");
                }
                
        }
        public T top() throws StackEmptyException{
                if(top>-1){
                        return stack[top];
                }else{
                        throw new StackException("Top attempted on an empty stack");
                }
        }               
}
// StackFullException class is used to define the custom exception when stack is full
public class StackFullException extends Exception {
        StackFullException(String s){
                super(s);
        }

}

//StackEmptyException is used to create a custom exception when stack is empty
public class StackEmptyException extends Exception {
        StackEmptyException(String s){
                super(s);
        }

}
//DriverClassForStackGenerics  is used to test whether all operations are working properly or not.

public class DriverClassForStackGenerics  {
  public static  <T> void main(String[] args) throws StackFullException, StackEmptyException{
          ImprovedArrayBasedStack st= new ImprovedArrayBasedStack(5);
          st.push(10);
          System.out.println("The Top elment in the stack after st.push(10) is "+st.top());
          st.push(11);
          System.out.println("The Top elment in the stack after st.push(11) is "+st.top());
          st.push(12);
          System.out.println("The Top elment in the stack after st.push(12) is "+st.top());
          st.push(13,14);
          System.out.println("The Top elment in the stack after st.push(12) is "+st.top());      
          st.pop();
          System.out.println("The Top elment in the stack after st.pop() is "+st.top());
          st.doublePop();
          System.out.println("The Top elment in the stack after st.doublePop(10) is "+st.top());
          st.pop();
          System.out.println("The Top elment in the stack after st.doublePop(10) is "+st.top());
                          
  }
}

The output of the above program is as below:

The sample output of all the exceptions handled are :


Related Solutions

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:...
Write a class "LinkedBag" which implements this interface. The LinkedBag class has two instance variables firstNode...
Write a class "LinkedBag" which implements this interface. The LinkedBag class has two instance variables firstNode and numberOfEntries. It has an inner class Node. BagInterface.java /** An interface that describes the operations of a bag of objects. @author Frank M. Carrano @author Timothy M. Henry @version 4.1*/public interface BagInterface<T>{ /** Gets the current number of entries in this bag. @return The integer number of entries currently in the bag. */ public int getCurrentSize(); /** Sees whether this bag is empty....
In java Implement the class Book. It has the following instance variables: name, subject, year, maximumLoanPeriod,...
In java Implement the class Book. It has the following instance variables: name, subject, year, maximumLoanPeriod, and loanPeoriod. The following methods should be included: • Constructor(s), Accessors and Mutators as needed. • public double computeFine() => calculates the fine due on this item The fine is calculated as follows: • If the loanPeriod <= maximumLoanPeriod, there is no fine on the book. • If loanPeriod > maximumLoanPeriod o If the subject of the book is "CS" the fine is 10.00...
public class OperationsBetween { // You must define the following: // 1.) Two private instance variables,...
public class OperationsBetween { // You must define the following: // 1.) Two private instance variables, min and max   // 2.) A constructor which takes initial values for // min and max // 3.) An instance method named sum, which sums the // values between min and max and returns the // result. For example, if min = 3 and max = 5, // then this should return 12 (3 + 4 + 5). If // min is greater than...
public class OperationsBetween { // You must define the following: // 1.) Two private instance variables,...
public class OperationsBetween { // You must define the following: // 1.) Two private instance variables, min and max // // 2.) A constructor which takes initial values for // min and max // // 3.) An instance method named sum, which sums the // values between min and max and returns the // result. For example, if min = 3 and max = 5, // then this should return 12 (3 + 4 + 5). If // min is...
in Java, Create a class called EMPLOYEE that includes three instance variables – a first name...
in Java, Create a class called EMPLOYEE that includes three instance variables – a first name (type String), a last name (type String) and a monthly salary (double). Provide a constructor that initializes the three instance variables. Provide a set and a get method for each instance variable. If the monthly salary is not positive, do not set its value. Write a test app names EmployeeTest that demonstrates class EMLOYEE’s capabilities. Create two EMPLOYEE objects and display each object’s yearly...
SalaryCalculator in Java. The SalaryCalculator class should have instance variables of: an employee's name, reportID that...
SalaryCalculator in Java. The SalaryCalculator class should have instance variables of: an employee's name, reportID that is unique and increments by 10, and an hourly wage. There should also be some constructors and mutators, and accessor methods.
Create a class called Student. Include the following instance variables: name, address, phone, gpa Create all...
Create a class called Student. Include the following instance variables: name, address, phone, gpa Create all of the methods required for a standard user defined class: constructors, accessors, mutators, toString, equals Create the client for testing the Student class Create another class called CourseSection Include instance variables for: course name, days and times course meets (String), description of course, student a, student b, student c (all of type Student) Create all of the methods required for a standard user defined...
PREVIOUS CODE: InventroryItems class InventoryItem implements Cloneable{ // instance variables protected String description; protected double price;...
PREVIOUS CODE: InventroryItems class InventoryItem implements Cloneable{ // instance variables protected String description; protected double price; protected int howMany; // constructor public InventoryItem(String description, double price, int howMany) { this.description = description; this.price = price; this.howMany = howMany; } // copy constructor public InventoryItem(InventoryItem obj) { this.description = obj.description; this.price = obj.price; howMany = 1; } // clone method @Override protected Object clone() throws CloneNotSupportedException { return super.clone(); } // toString method @Override public String toString() { return description +...
Code in Java Write a Student class which has two instance variables, ID and name. This...
Code in Java Write a Student class which has two instance variables, ID and name. This class should have a two-parameter constructor that will set the value of ID and name variables. Write setters and getters for both instance variables. The setter for ID should check if the length of ID lies between 6 to 8 and setter for name should check that the length of name should lie between 0 to 20. If the value could not be set,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT