+------+ | IExp |<---------------+-+ +------+ | | / \ | | --- | | | | | --------------------- | | | | | | +-----------+ +--------------+ | | | Value | | BinOp | | | +-----------+ +--------------+ | | | int value | +--| IOperator op | | | +-----------+ | | IExp left |---+ | | | IExp right |-----+ | +--------------+ v +---------------------------+ | IOperator | +---------------------------+ | int eval(int r1, int r2); | +---------------------------+ / \ --- | +-----------+ | Plus | +-----------+Create a new Java project in Eclipse from the given source file (place each interface and class in a separate file, as usual). Study the class diagram for this class hierarchy. Extend the program so that the expressions can also include multiplication.
toString()
that produces a String
representation of this expression with parentheses surrounding every binary expression. Define examples that represent the following expressions and include tests that verify that they have been correctly rendered as String
's:
(2 + (3 + 4)) ((3 + 5) * ((2 * 3) + 5))Make sure you include a
toString()
method for every class you design for the remaining problems in this assignment.
Using Java generics,
change the interface and class definitions so that they are parametrized over the type of data you will use.
The IExp
interface will be parametrized only over the type of value it represents when evaluated.
The BinOp
class needs to be parametrized over the type of operands it receives, as well as the type of value it produces.
Add the necessary class definitions so you can represent relational and
boolean expressions.
Make sure you have examples for each of them, as well as tests for the eval()
method.
IntVar
and BoolVar
that implement IExp
and represent variables of the appropriate
type in an expression.
(Before you do any coding, add these classes to your class diagram.)
Each of the classes needs to keep track of the variable's name, e.g. x, or width.
The IntVar
class should include a method called substInt()
, and the BoolVar
class should include a method called
substBool()
, that consumes a String and an argument of the appropriate type (Integer or Boolean) and produces an instance of a Value that represents the given value, provided the given String matches the variable name. In all other cases it just returns this
.
Of course, these classes are obligated to include the method eval()
. However, eval()
should throw an exception, indicating that an expression with a variable in it cannot be evaluated.
noVars()
, a predicate that verifies that this expression does not contain any variables.
substInt()
and substBool()
for the entire IExp class hierarchy. These methods produce a new IExp in which every occurrence of a variable that matches the given name is replaced with an instance of the class Value
with the given value. Throw an exception if there is an attempt to substitute a boolean value for the identifier that represents an int value as well as if there is an attempt to substitute a int value for the identifier that represents an boolean value.
Create an archive of your Eclipse project. Using web-based turnin, turn in a single zip file containing all code and documentation for this assignment. Follow the naming conventions when naming your file.