Question: For this assignment you will write a program that uses a stack to evaluate an RPN expression. DoubleStack is a class that manages a stack

For this assignment you will write a program that uses a stack to evaluate an RPN expression.

  • DoubleStack is a class that manages a stack of double data.

Make no changes to DoubleStack; this stack implementation has been tested.

Your task is to write a program (class containing main()) that allows the user to input an RPN expression. The program will then evaluate the RPN expression using DoubleStack to store the values input and those calculated as the expressions value is determined.

After the program has evaluated the RPN expression, the expressions value will printed.

You may use either a switch statement or an ifelse ifelse ifelse construction to determine the nature of the current token being examined.

As mentioned in the earlier discussion of inputting RPN expressions, one concern is how to determine when the end of the expression has been reached. To make the work easier, at the end of the RPN expression the user will input a colon. In that way when the token being examined is found to be a colon, the program determines that the entire RPN expression has been read. At this time it will then print the value of the expression.

  • For example, the user might type: 3 4 + 5 * :
    • The program would evaluate this expression and print its value: 35.0.
  • The RPN expression may include addition, subtraction, multiplication and/or division operations.

For this program you may assume that the RPN expression input by the user is a valid RPN expression. Your code does not have to check for validity of the expression; it only needs to evaluate the expression.

Each run of the program will evaluate one RPN expression; to evaluate another, the program would be run again.

A run of the program might look like the following:

Type an RPN expression (":" to end): 3 4 + 5 * :

Value of expression = 35.0

// DoubleStack.java // Double stack.

public class DoubleStack { // Constructor. public DoubleStack(int size) { stack = new double[size]; top = 0; } // Pop method. public double pop() { top--; return stack[top]; } // Push method. public void push(double nr) { stack[top] = nr; top++; } // Stack full method. public boolean stackFull() { if(top == stack.length) return true; else return false; } // Stack empty method. public boolean stackEmpty() { if(top == 0) return true; else return false; } // Print stack method. public void printStack() { for(int ctr = 0; ctr < top; ctr++) { System.out.println("Stack[" + ctr + "] = " + stack[ctr]); } }

// Instance variables. private double[] stack; private int top;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!