Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

import java.util.*; public class Evaluator { private Stack operandStack; private Stack operatorStack; private StringTokenizer tokenizer; private static final String DELIMITERS = +-*^/#! ; public Evaluator()

import java.util.*; public class Evaluator { private Stack operandStack; private Stack operatorStack; private StringTokenizer tokenizer; private static final String DELIMITERS = "+-*^/#! "; public Evaluator() { operandStack = new Stack(); operatorStack = new Stack(); } public int eval( String expression ) { String token; // The 3rd argument is true to indicate that the delimiters should be used // as tokens, too. But, we'll need to remember to filter out spaces. this.tokenizer = new StringTokenizer( expression, DELIMITERS, true ); // initialize operator stack - necessary with operator priority schema // the priority of any operator in the operator stack other than // the usual mathematical operators - "+-*/" - should be less than the priority // of the usual operators // TODO Operator is abstract - this will need to be fixed: // operatorStack.push( new Operator( "#" )); // When is it a good time to add the "!" operator? while ( this.tokenizer.hasMoreTokens() ) { // filter out spaces if ( !( token = this.tokenizer.nextToken() ).equals( " " )) { // check if token is an operand if ( Operand.check( token )) { operandStack.push( new Operand( token )); } else { if ( ! Operator.check( token )) { System.out.println( "*****invalid token******" ); System.exit( 1 ); } // TODO Operator is abstract - these two lines will need to be fixed: // The Operator class should contain an instance of a HashMap, // and values will be instances of the Operators. See Operator class // skeleton for an example. Operator newOperator = new Operator( token ); while ( operatorStack.peek().priority() >= newOperator.priority() ) { // note that when we eval the expression 1 - 2 we will // push the 1 then the 2 and then do the subtraction operation // This means that the first number to be popped is the // second operand, not the first operand - see the following code Operator oldOpr = operatorStack.pop(); Operand op2 = operandStack.pop(); Operand op1 = operandStack.pop(); operandStack.push( oldOpr.execute( op1, op2 )); } operatorStack.push( newOperator ); } } } // Control gets here when we've picked up all of the tokens; you must add // code to complete the evaluation - consider how the code given here // will evaluate the expression 1+2*3 // When we have no more tokens to scan, the operand stack will contain 1 2 // and the operator stack will have + * with 2 and * on the top; // In order to complete the evaluation we must empty the stacks (except // the init operator on the operator stack); that is, we should keep // evaluating the operator stack until it only contains the init operator; // Suggestion: create a method that takes an operator as argument and // then executes the while loop; also, move the stacks out of the main // method return 0; } }

public class EvaluatorTester { public static void main(String[] args) { Evaluator evaluator = new Evaluator(); for ( String arg : args ) { System.out.format( "%s = %d ", arg, evaluator.eval( arg ) ); // System.out.println( arg + " = " + evaluator.eval( arg ) ); } } }

import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class EvaluatorUI extends JFrame implements ActionListener { private TextField txField = new TextField(); private Panel buttonPanel = new Panel(); // total of 20 buttons on the calculator, // numbered from left to right, top to bottom // bText[] array contains the text for corresponding buttons private static final String[] bText = { "7", "8", "9", "+", "4", "5", "6", "- ", "1", "2", "3", "*", "0", "^", "=", "/", "(", ")", "C", "CE" }; private Button[] buttons = new Button[ bText.length ]; public static void main(String argv[]) { EvaluatorUI calc = new EvaluatorUI(); } public EvaluatorUI() { setLayout( new BorderLayout() ); add( txField, BorderLayout.NORTH ); txField.setEditable( false ); add( buttonPanel, BorderLayout.CENTER ); buttonPanel.setLayout( new GridLayout( 5, 4 )); //create 20 buttons with corresponding text in bText[] array for ( int i = 0; i

public class Operand { public Operand( String token ) { } public Operand( int value ) { } public int getValue() { return 0; } public static boolean check( String token ) { return false; } }

public abstract class Operator { // The Operator class should contain an instance of a HashMap // This map will use keys as the tokens we're interested in, // and values will be instances of the Operators. // Example: // Where does this declaration go? What should its access level be? // Class or instance variable? Is this the right declaration? // HashMap operators = new HashMap(); // operators.put( "+", new AdditionOperator() ); // operators.put( "-", new SubtractionOperator() ); public abstract int priority(); public abstract Operand execute( Operand op1, Operand op2 ); public static boolean check( String token ) { return false; } }

image text in transcribed

image text in transcribed

image text in transcribed

Computer Science Department San Francis co State Univers ity CSC 413 Spring 2018 Assignment 1 - Expression Evaluator and Calculator GUI Note that the due date applies to the last commit times tamp into the main branch of your repository Overview The purpose of this assignment is to practice object oriented design to create two programs 1. An object that evahuates mathematical expressions 2. A GUI around the artifact from (1) Submis sion You are re quired to submit a documenta tion PDF which is to be stored in the documenta tion folder in your re pos itory, and provide your source code in the repository created in the github classroom The docume nta tion must inchide the following sections * Project introduction and overview (practice concisely summarizing technical work, and provide informa tion on execution and development environment). Inchude scope of work (what were you tasked with completing, what did you comple te) *Ins tructions to compile as jar and execute (you will be penalized if this is not provide d) *Assumptions (what assumptions did you make in order to complete the assignment) *Implementation discussion (I strongly recommend the use ofgraphical artif cts to heh describe your system and its implementation: class dia grams, hierarchy, etc Implementation decisions, code organization) *Results and conchsions (what did you learn, future work, what challenges did you encounter and how did you overcome them) Organization and appearance of this document is critical Please use spelling and grammar checkers - your ability to communicate about software and technology is almost as important as your ability to write software! Requirements You will be provided with an almost complete version of the Evaluator class (Evahiator.java). You should program the utility classes it uses - Operand and Operator and then follow the suggestions in the code to complete the imple mentation of the Evaluator class. The Evaluator implements a single public method, eval, that takes a single String parameter that represents an infx mathematicalexpression, parses and evaluates the expression, and returns the inte ger result. An example expression is 2 + 3 *4, which would be evahuated to 14

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Spatial Databases With Application To GIS

Authors: Philippe Rigaux, Michel Scholl, Agnès Voisard

1st Edition

1558605886, 978-1558605886

More Books

Students also viewed these Databases questions