Due: Tuesday 4/16 10:00am

Submission name: w19_expression

For this, you will need the ExpressionTreeNode and ExpressionTree classes, which you can get here: https://github.com/nextcs-dw/dwsource/tree/main/Expression

Reminder: Test Thursday 4/18

Expression Tree Nodes:

Modify the ExpressionTreeNodeclass as follows:

  • Add a new constructor ExpressionTreeNode(boolean operation, int x, int y)
    • If operation is true, set value to 0 and type to a random int in the range [1, 4].
    • If operation is false set value to a random integer in the range [-99, 99] and type to VALUE.
    • Set the other instance variables as they would be in the other provided constructor.
  • Add a toString() method which should return a String containing:
    • The value if the node is a value node.
    • The correct operation symbol (+ - * /) if the node is an operator node.
  • Change display so that is uses the toString() result when printing nodes.

Expression Tree:

Modify makeTree in the ExpressionTree class as follows:

  • Nodes should have a random chance of being operator or value nodes. Use this to determine the randomness:
    • operator = random(1) < float(levels)/numLevels
  • Leaf nodes (nodes at the last possible level of the tree) should always be value nodes, regardless of the random calculation above.
  • Value nodes should not have any child nodes.
  • Operator nodes should always have 2 child nodes.
  • The provided makeTree includes the code for position a node, but does not include any recursive parts (base case or recursive case).