Question

In: Computer Science

Reverse Polish (HP) Style Calculator - Part 2 The purpose of this assignment is to incorporate...

Reverse Polish (HP) Style Calculator - Part 2

The purpose of this assignment is to incorporate an abstract class and inheritance and add it to the interface of the business calculator created in Topic 4.

• Implement a pure abstract stack class named AbstractStack that has no implementation. It will not have a private array of double, because that is an implementation detail of ArrayStack that would not be found in other implementations such as LinkedStack. Nor will it have the constructor public AbstractClass(int size), or methods public double peek(int n) or public int count(), since these methods are convenience features that are easily implemented for ArrayStack, but not other implementations of stack.  

• Include the method public double peek(), and add the method public void clear(), resulting in the following abstract methods.

o public abstract void push(double item) – Standard stack action; Throws an exception if the stack is full.

o public abstract double pop() – Standard stack action; Throws an exception if the stack is empty.

o public abstract boolean isEmpty() – Returns true if the stack is empty and false otherwise.

o public abstract double peek() – returns the value of the item located at the specified position on the stack; throws an exception if the array bounds are exceeded or if a nonexistent element is requested.

o public abstract void clear() – empties the stack if it is not already empty.

• Do not specify a constructor. Depend on the default constructor.

• Modify the existing ArrayStack.java file to include the phrase "extends AbstractStack."

• Add an implementation for the methods void clear() and double peek(), which overloads the existing double peek(int n). Provide a default constructor that creates an array that will hold three elements.

o public ArrayClass(int size) - constructor that creates an ArrayClass instance containing an array of the specified size. Remember that in Java, arrays are indexed from 0!

o public push(double item) – standard stack action; throws an exception if the array bounds are exceeded.

o public double pop() – standard stack action; throws an exception if the array bounds are exceeded.

o public boolean isEmpty() – returns true if the stack is empty and false otherwise.

o public double peek(int n) – returns the value of the item located at the specified position on the stack; throws an exception if the array bounds are exceeded or if a nonexistent element is requested; peek(0) will return the top element of the stack.

o public int count() - returns the number of items currently pushed onto the stack.

• Modify the existing TestArrayStack.java file to include tests for void clear() and double peek(). Note that it is easiest to do "incremental testing" in which you code a little then test a little.  

• Create an interface with additional methods.

• Create a new file called Forth.java. Make this a publicinterface file. Add the following methods to the interface.

o public add() – pops two values from the stack, adds them together, and pushes the result back onto the stack. Throws an exception if there are not at least 2 items on the stack.

o public sub() – pops two values from the stack, subtracts the second number popped from the first number popped, and pushes the result back onto the stack. Throws an exception if there are not at least 2 items on the stack.

o public mult() – pops two values from the stack, multiplies them together, and pushes the result back onto the stack. Throws an exception if there are not at least 2 items on the stack.

o public div() – pops two values from the stack, divides the second number popped by the first number popped, and pushes the result back onto the stack. Throws an exception if there are not at least 2 items on the stack.

o public dup() – peeks at the top value on the stack and pushes a copy of that value onto the stack. Throws an exception if the stack is empty or full.

o public twoDup() – peeks at the top two values on the stack and pushes a copy of both values onto the stack (in the same order). Throws an exception if the stack does not have at least 2 items or room for 2 additional items.

• Create another file called ForthStack.java. Make this file extend ArrayStack and implement Forth. Code concrete implementations of each of the methods in the interface.

• Test by making a copy of the file TestArrayStack.java and name it TestForth. Change the name of the class inside the file. Add tests for add, sub, mult, div, dup, and twoDup.

• After thoroughly testing the program, submit the AbstractStack.java, ArrayStack.java, Forth.java, ForthStack.java, and TestForthStack.java files to the instructor.

Previous Array Stack

public class ArrayStack
{

private double[] array;
private int size;
private int num;

   public ArrayStack(int a){
   array = new double[a];
   size = a;
   num = 0;
}
   public void push(double a){
   if (num < size){
   array[num] = a;
   num++;
   System.out.println("Success");
}
   else {
   System.out.println("Failure! Stack is full");
}
}
   public double pop(){
   if (num > 0){
   num--;
   return array[num];
}
   else {
   System.out.println("Stack is empty");
   return -1;
}
}
   public boolean isEmpty(){
   return (num == 0);
}
   public double peek(int n){
   try
{
   if (num > 0)
{
   if (n < 0 || n >= num)
   return -1;
   else
   return array[num-1-n];
}
   else
{
   System.out.println("Stack is empty");
   return -1;
}
}
   catch(Exception e ){
   e.printStackTrace();
}
   return 0;
}
   public int count(){
   return num;
}


}
  

  
Previous Test Array Stack

public class TestArrayStack{

public static void main(String [] args)
{
   int choice;
   int peek;
   double val,poped;
   boolean empty;
   Scanner sc =new Scanner(System.in);
   ArrayStack as = new ArrayStack(20);
  
   while(true){
       System.out.println("1. Enter a Value in stack");
       System.out.println("2. Pop a Value");
       System.out.println("3. Check If array is Empty");
       System.out.println("4. Peek Function");
       System.out.println("5. Exit\n");
   choice = sc.nextInt();
   switch(choice)
   {
  
   case 1:
       System.out.print("Enter a value To push : ");
   val = sc.nextDouble();
   as.push(val);
   break;
   case 2:
   poped = as.pop();
       System.out.println("Popped : "+poped);
   break;
   case 3:
   empty = as.isEmpty();
       System.out.println("Empty ? "+empty);
   break;
   case 4:
       System.out.print("Enter a index to peek : ");
   peek = sc.nextInt();
   poped = as.peek(peek);
   if(poped != -1)
       System.out.println("Peeked Value : "+poped);
  
   else
       System.out.println("Oops it was not a valid index this place is empty");
   break;
   case 5:
   System.exit(0);
}
}
}
}

What I need is AbstractStack.java, ArrayStack.java, Forth.java, ForthStack.java, and TestForthStack.java

Thank you

Solutions

Expert Solution

Ans)

Java Program

Program

AbstractStack.java

/*
* Pure abstract class without implementation
*/
public abstract class AbstractStack {
   public abstract void push(double item);
   public abstract double pop();
   public abstract boolean isEmpty();
   public abstract double peek();
   public abstract void clear();
}

ArrayStack.java

import java.util.Arrays;

public class ArrayStack extends AbstractStack {
   //Attributes
   private double[] array;
   private int size;
   private int num;
  
   //Default constructor
   public ArrayStack() {
       array = new double[3];
       size = 3;
       num = 0;
   }
  
   //Parametrized Constructor
   public ArrayStack(int a){
       array = new double[a];
       size = a;
       num = 0;
   }
  
   //Insert a new element
   public void push(double a){
       if (num < size) {
           array[num] = a;
           num++;
            System.out.println("Success");
       }
       else {
           throw new ArrayIndexOutOfBoundsException("Failure! Stack is full");
       }
   }
  
   //Take out last inserted value
   public double pop(){
       if (num > 0){
           num--;
           return array[num];
       }
       else {
           throw new ArrayIndexOutOfBoundsException("Stack is empty");
        }
    }
   //Check stack empty or not
   public boolean isEmpty(){
       return (num == 0);
   }
   //Get top element from stack
   public double peek() {
       return peek(num-1);
   }
   //Get specified index element
   public double peek(int n){
       try
       {
           if (num > 0){
               if (n < 0 || n >= num)
                   return -1;
               else
                   return array[n];
           }
           else{
               System.out.println("Stack is empty");
               return -1;
           }
       }catch(Exception e ){
           e.printStackTrace();
       }
       return 0;
   }
   //Number of elements in stack
   public int count(){
       return num;
   }
   public void clear() {
       size=0;;
       num=0;
   }
  
}

Fourth.java

/*
* Interface to generate sum additional functions using stack
*/
public interface Fourth {
   public void add();
   public void sub();
   public void mul();
   public void div();
   public void dup();
   public void twoDup();
}

FourthStack.java

public class FourthStack extends ArrayStack implements Fourth{
   //Parameterized constructor
   public FourthStack(int sz) {
       super(sz);
   }

   @Override
   //Pop 2 elements from stack and add values
   //Push into stack
   public void add() {
       if(super.count()<2) {
           throw new ArrayIndexOutOfBoundsException("Not enough elements to pop");
       }
       else {
           super.push(super.pop()+super.pop());
       }
   }

   @Override
   //Pop 2 elements from stack and subtract second from first
   //Push into stack
   public void sub() {
       if(super.count()<2) {
           throw new ArrayIndexOutOfBoundsException("Not enough elements to pop");
       }
       else {
           super.push(super.pop()-super.pop());
       }
   }

   @Override
   //Pop 2 elements from stack and multiply values
   //Push into stack
   public void mul() {
       if(super.count()<2) {
           throw new ArrayIndexOutOfBoundsException("Not enough elements to pop");
       }
       else {
           super.push(super.pop()*super.pop());
       }
   }

   @Override
   //Pop 2 elements from stack and divide second from first values
   //Push into stack
   public void div() {
       if(super.count()<2) {
           throw new ArrayIndexOutOfBoundsException("Not enough elements to pop");
       }
       else {
           super.push(super.pop()/super.pop());
       }
   }

   @Override
   //peek an element and make duplicate
   //Push into stack
   public void dup() {
       if(super.count()<1) {
           throw new ArrayIndexOutOfBoundsException("Not enough elements to pop");
       }
       else {
           super.push(super.peek());
       }
      
   }

   //Peek 2 elements from stack and make their duplicate
   //Push into stack in same order
   @Override
   public void twoDup() {
       if(super.count()<2) {
           throw new ArrayIndexOutOfBoundsException("Not enough elements to pop");
       }
       else {
           double first=super.peek();
           double second=super.peek(super.count()-2);
           super.push(second);
           super.push(first);
       }
   }

}

TestFourthStack.java

import java.util.Scanner;
/**
* Test implemented functions
* @author deept
*
*/
public class TestFourthStack {
   public static void main(String [] args){
   //Variables for input
   int choice;
    int pek;
    double val,poped;
     boolean empty;
    //Keyboard read
    Scanner sc =new Scanner(System.in);
    FourthStack as = new FourthStack(20);
    //Loop until exit
    while(true){
       //User choices
       System.out.println("1. Enter a Value in stack");
        System.out.println("2. Pop a Value");
        System.out.println("3. Check If array is Empty");
        System.out.println("4. Peek Function");
        System.out.println("5. Clear Stack");
        System.out.println("6. Add Function");
        System.out.println("7. Sub Function");
        System.out.println("8. Mul Function");
        System.out.println("9. Div Function");
        System.out.println("10. Dup Function");
        System.out.println("11. TwoDup Function");
        System.out.println("0. Exit\n");
        choice = sc.nextInt();
        //Execute each choice
        switch(choice){
        case 1:
           System.out.print("Enter a value To push : ");
           val = sc.nextDouble();
           as.push(val);
           break;
        case 2:
           poped = as.pop();
           System.out.println("Popped : "+poped);
           break;
        case 3:
           empty = as.isEmpty();
           System.out.println("Empty ? "+empty);
           break;
        case 4:
           poped = as.peek();
           if(poped != -1)
               System.out.println("Peeked Value : "+poped);
           else
               System.out.println("Oops it was not a valid index this place is empty");
           break;
        case 5:
           as.clear();
           break;
        case 6:
           as.add();
           break;
        case 7:
           as.sub();
           break;
        case 8:
           as.mul();
           break;
        case 9:
           as.div();
           break;
        case 10:
           as.dup();
           break;
        case 11:
           as.twoDup();
           break;
        case 0:
           System.exit(0);
        }
    }
}
}

--------------------------------------------------------------------

Output

1. Enter a Value in stack
2. Pop a Value
3. Check If array is Empty
4. Peek Function
5. Clear Stack
6. Add Function
7. Sub Function
8. Mul Function
9. Div Function
10. Dup Function
11. TwoDup Function
0. Exit

1
Enter a value To push : 1.5
Success
1. Enter a Value in stack
2. Pop a Value
3. Check If array is Empty
4. Peek Function
5. Clear Stack
6. Add Function
7. Sub Function
8. Mul Function
9. Div Function
10. Dup Function
11. TwoDup Function
0. Exit

1
Enter a value To push : 2.5
Success
1. Enter a Value in stack
2. Pop a Value
3. Check If array is Empty
4. Peek Function
5. Clear Stack
6. Add Function
7. Sub Function
8. Mul Function
9. Div Function
10. Dup Function
11. TwoDup Function
0. Exit

1
Enter a value To push : 3.5
Success
1. Enter a Value in stack
2. Pop a Value
3. Check If array is Empty
4. Peek Function
5. Clear Stack
6. Add Function
7. Sub Function
8. Mul Function
9. Div Function
10. Dup Function
11. TwoDup Function
0. Exit

6
Success
1. Enter a Value in stack
2. Pop a Value
3. Check If array is Empty
4. Peek Function
5. Clear Stack
6. Add Function
7. Sub Function
8. Mul Function
9. Div Function
10. Dup Function
11. TwoDup Function
0. Exit

4
Peeked Value : 6.0
1. Enter a Value in stack
2. Pop a Value
3. Check If array is Empty
4. Peek Function
5. Clear Stack
6. Add Function
7. Sub Function
8. Mul Function
9. Div Function
10. Dup Function
11. TwoDup Function
0. Exit

1
Enter a value To push : 8
Success
1. Enter a Value in stack
2. Pop a Value
3. Check If array is Empty
4. Peek Function
5. Clear Stack
6. Add Function
7. Sub Function
8. Mul Function
9. Div Function
10. Dup Function
11. TwoDup Function
0. Exit

7
Success
1. Enter a Value in stack
2. Pop a Value
3. Check If array is Empty
4. Peek Function
5. Clear Stack
6. Add Function
7. Sub Function
8. Mul Function
9. Div Function
10. Dup Function
11. TwoDup Function
0. Exit

4
Peeked Value : 2.0
1. Enter a Value in stack
2. Pop a Value
3. Check If array is Empty
4. Peek Function
5. Clear Stack
6. Add Function
7. Sub Function
8. Mul Function
9. Div Function
10. Dup Function
11. TwoDup Function
0. Exit

9
Success
1. Enter a Value in stack
2. Pop a Value
3. Check If array is Empty
4. Peek Function
5. Clear Stack
6. Add Function
7. Sub Function
8. Mul Function
9. Div Function
10. Dup Function
11. TwoDup Function
0. Exit

4
Peeked Value : 1.3333333333333333
1. Enter a Value in stack
2. Pop a Value
3. Check If array is Empty
4. Peek Function
5. Clear Stack
6. Add Function
7. Sub Function
8. Mul Function
9. Div Function
10. Dup Function
11. TwoDup Function
0. Exit

1
Enter a value To push : 2
Success
1. Enter a Value in stack
2. Pop a Value
3. Check If array is Empty
4. Peek Function
5. Clear Stack
6. Add Function
7. Sub Function
8. Mul Function
9. Div Function
10. Dup Function
11. TwoDup Function
0. Exit

8
Success
1. Enter a Value in stack
2. Pop a Value
3. Check If array is Empty
4. Peek Function
5. Clear Stack
6. Add Function
7. Sub Function
8. Mul Function
9. Div Function
10. Dup Function
11. TwoDup Function
0. Exit

4
Peeked Value : 2.6666666666666665
1. Enter a Value in stack
2. Pop a Value
3. Check If array is Empty
4. Peek Function
5. Clear Stack
6. Add Function
7. Sub Function
8. Mul Function
9. Div Function
10. Dup Function
11. TwoDup Function
0. Exit

10
Success
1. Enter a Value in stack
2. Pop a Value
3. Check If array is Empty
4. Peek Function
5. Clear Stack
6. Add Function
7. Sub Function
8. Mul Function
9. Div Function
10. Dup Function
11. TwoDup Function
0. Exit

4
Peeked Value : 2.6666666666666665
1. Enter a Value in stack
2. Pop a Value
3. Check If array is Empty
4. Peek Function
5. Clear Stack
6. Add Function
7. Sub Function
8. Mul Function
9. Div Function
10. Dup Function
11. TwoDup Function
0. Exit

11
Success
Success
1. Enter a Value in stack
2. Pop a Value
3. Check If array is Empty
4. Peek Function
5. Clear Stack
6. Add Function
7. Sub Function
8. Mul Function
9. Div Function
10. Dup Function
11. TwoDup Function
0. Exit

4
Peeked Value : 2.6666666666666665
1. Enter a Value in stack
2. Pop a Value
3. Check If array is Empty
4. Peek Function
5. Clear Stack
6. Add Function
7. Sub Function
8. Mul Function
9. Div Function
10. Dup Function
11. TwoDup Function
0. Exit

2
Popped : 2.6666666666666665
1. Enter a Value in stack
2. Pop a Value
3. Check If array is Empty
4. Peek Function
5. Clear Stack
6. Add Function
7. Sub Function
8. Mul Function
9. Div Function
10. Dup Function
11. TwoDup Function
0. Exit

5
1. Enter a Value in stack
2. Pop a Value
3. Check If array is Empty
4. Peek Function
5. Clear Stack
6. Add Function
7. Sub Function
8. Mul Function
9. Div Function
10. Dup Function
11. TwoDup Function
0. Exit

3
Empty ? true
1. Enter a Value in stack
2. Pop a Value
3. Check If array is Empty
4. Peek Function
5. Clear Stack
6. Add Function
7. Sub Function
8. Mul Function
9. Div Function
10. Dup Function
11. TwoDup Function
0. Exit

0

if any doubts above below program screen shot and output

​if your satisfy above answer please give positive rating or?

please don't dislike

Thank you!


Related Solutions

REVERSE POLISH CALCULATOR C++ ONLY. For this assignment, you are to write a program, which will...
REVERSE POLISH CALCULATOR C++ ONLY. For this assignment, you are to write a program, which will calculate the results of Reverse Polish expressions that are provided by the user. You must use a linked list to maintain the stack for this program (NO array implementations of the stack). You must handle the following situations (errors): Too many operators (+ - / *) Too many operands (doubles) Division by zero The program will take in a Polish expression that separates the...
Write a Reverse Polish Calculator, RPN We discussed in class a general implementation of RPN using...
Write a Reverse Polish Calculator, RPN We discussed in class a general implementation of RPN using a stack to keep track of the numbers. Each time an operand is encountered, two values are popped from this stack, the given operation is performed, and the result is pushed back onto the stack. Utilize Java generic Stack Class to implement this algorithm with four arithmetic operations: +, *, -, /. It is suggested that you implement a class RPN with a single...
Let's try to develop a C++ Reverse Polish Notation (RPN) calculator! Create a base class called...
Let's try to develop a C++ Reverse Polish Notation (RPN) calculator! Create a base class called Operand Give it a virtual destructor to avoid any weird problems later on! Derive a class called Number from Operand Maintain a double member variable in class Number For simplicity, you may make the member variable public if you would like Derive a class called Operator from Operand Derive a class called Add from Operator (2 + 3 = 5) Derive a class called...
I need this in java A6 – Shipping Calculator Assignment Introduction In this part, you will...
I need this in java A6 – Shipping Calculator Assignment Introduction In this part, you will solve a problem described in English. Although you may discuss ideas with your classmates, etc., everyone must write and submit their own version of the program. Do NOT use anyone else’s code, as this will result in a zero for you and the other person! Shipping Calculator Speedy Shipping Company will ship your package based on the weight and how far you are sending...
For Part 2 of this assignment, you will use the “Assignment 1 – Linear Kinematics Data”...
For Part 2 of this assignment, you will use the “Assignment 1 – Linear Kinematics Data” excel file. In the data set you are provided with vertical position and time data for a person’s vertical center of mass motion for an unspecified movement task. You will utilize excel in all (well, some…) of its glory to calculate the vertical velocity and vertical acceleration data from the position and time data provided in the excel file. Again you will use the...
Chapter 4 Lecture Assignment (part 1) Hide or show questions eBook Calculator Appendix: Completing an End-of-Period...
Chapter 4 Lecture Assignment (part 1) Hide or show questions eBook Calculator Appendix: Completing an End-of-Period Spreadsheet Alert Security Services Co. offers security services to business clients. Complete the following end-of-period spreadsheet for Alert Security Services Co. If a box does not require an entry, leave it blank. Alert Security Services Co. End-of-Period Spreadsheet (Work Sheet) For the Year Ended October 31, 2019 Adjusted Trial Balance Income Statement Balance Sheet Account Title Dr. Cr. Dr. Cr. Dr. Cr. Cash 373...
Assignment 2: Connection between Confidence Intervals and Sampling Distributions: The purpose of this activity is to...
Assignment 2: Connection between Confidence Intervals and Sampling Distributions: The purpose of this activity is to help give you a better understanding of the underlying reasoning behind the interpretation of confidence intervals. In particular, you will gain a deeper understanding of why we say that we are “95% confidentthat the population mean is covered by the interval.” When the simulation loads you will see a normal-shaped distribution, which represents the sampling distribution of the mean (x-bar) for random samples of...
IA 2 is a two-part assignment. The first part requires you to identify, by statute number,...
IA 2 is a two-part assignment. The first part requires you to identify, by statute number, and summarize the statute that prohibits telephone solicitors from blocking or interfering with a residential telephone subscriber's caller ID function.
pseudocode please! Assignment 5B: Moneyball: Part 2. Similar to the previous assignment, you’re going to read...
pseudocode please! Assignment 5B: Moneyball: Part 2. Similar to the previous assignment, you’re going to read in the number of years the player played and the starting year of that player – followed by the statistics for those years. This time, however, you’re going to print out the years from worst to best in sorted order. Hint: this will require a second array to store years. If you can sort one array, can you sort both? Sample Output #1: Enter...
FINC 350 – Attribution Assignment 1. What is the purpose of macro-attribution analysis? 2. Identify the...
FINC 350 – Attribution Assignment 1. What is the purpose of macro-attribution analysis? 2. Identify the categories in the attribution analysis and what they represent. 3. The Fund has a 2% policy-weight to emerging market equities – a sub-category within the international equity bucket. The broad category benchmark is MSCI-EAFE and has a 20% policy weight. MSCI-EAFE returned 2.5% and MSCI-EME returned 3.5% during the last month. What is the style-bias impact? Show calculations. 4. Instead of a passive ETF,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT