Question: Program 2, Restricted Structures 21. Write a program to evaluate an arithmetic expression written in postfixed notation. The arithmetic expression will be input as a

Program 2, Restricted Structures

21. Write a program to evaluate an arithmetic expression written in postfixed notation. The arithmetic expression will be input as a String (by the user) and will contain only integer operands. Use the following code sequence to parse (i.e., remove) the integers and the ioerators from the input string, mathExpression:

import java.util;

String thisToken;

StringTokenizer tokens = new StringTokenizer(mathExpression);

while(tokens.hasMoreTokens())

{ this token = tokens.nextToken();

//proccessing for thisToken goes here

}

You may assume the user will enter a one line properly formatted post fixed mathexpression, with the tokens (operators and operands) separated by white space. You should use the API Stack class and the StringTokenizer class. Declare your Stack object to be a stack that can only store objects in the class Double.

Below is an outline of the program.

import java.util.*; // needed for the StringTokenizer class

import javax.swing.*;

// Declare a Java API Stack object that can store Double objects (See page 168)

// Accept the users input of the math String into the variable mathExpression

// Evaluate the math expression //

// Pop the stack and output the returned value

(Be sure to parse the numeric operands to doubles before pushing them onto the stack)

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!