Question: Refine the expression evaluator of Exercise P13.9 so that expressions can contain the variable x. For example, 3*x*x+4*x+5 is a valid expression. Change the Expression

Refine the expression evaluator of Exercise P13.9 so that expressions can contain the variable x. For example, 3*x*x+4*x+5 is a valid expression. Change the Expression interface so that its value method has as parameter the value that x should take. Add a class Variable that denotes an x. Write a program that reads an expression string and a value for x, translates the expression string into an Expression object, and prints the result of calling value(x).


Data from Exercise P13.9

The expression evaluator in Section 13.5 returns the value of an expression. Modify the evaluator so that it returns an instance of the Expression interface with five implementing classes, Constant, Sum, Difference, Product, and Quotient. The Expression interface has a method int value(). The Constant class stores a number, which is returned by the value method. The other four classes store two arguments of type Expression, and their value method returns the sum, difference, product, and quotient of the values of the arguments. Write a test program that reads an expression string, translates it into an Expression object, and prints the result of calling value.

Step by Step Solution

3.38 Rating (173 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

Expression Interface public interface Expression public int valueint x Constant class public class C... View full answer

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 Java Concepts Late Objects Questions!