Question: Add a toString method to the Expression class (as described in Exercise P13.9 and Exercise P13.10) that returns a string representation of the expression. It

Add a toString method to the Expression class (as described in Exercise P13.9 and Exercise P13.10) that returns a string representation of the expression. It is ok to use more parentheses than required in mathematical notation. For example, for the expression 3*x*x+5, you can print (((3*x)*x)+5).


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.50 Rating (170 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

Here is an implementation of the toString method for the Expression class public class Expression private char variable private double coefficient pri... 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!