Question: Turn the Arithmetic project from the last homework into the Algebra project, by including a new class named Variable. Objects of this class represent a
Turn the Arithmetic project from the last homework into the Algebra project, by including a new class named Variable. Objects of this class represent a variable, like X0or X4, whose value is determined sometime after it is defined.
In the Arithmetic project, we created expressions like ((3 + 4) / (2 + 9)). We even were able to evaluate expressions like this. Now we want to create (in the Algebra project) expressions like ((X0 3.14) + (((1.3 (X1 / 2.0)) * (2.3 X2))), where the expression knows how to evaluate itself if given an array of values for X0, X1 and X2. Of course, the tree representation of the above algebraic expression isnt really different. It just has a new node type, the Variable node, and can be slightly more complex than the expressions you created before.
The Xs must be given concrete values. So, the eval() method for every Node must be given an array of values (double[] data) so that when this array is passed down to a Variable, it can select its value. Thus a Variable node whose index is 2 should return data[2]. So every method eval()must now be eval(double[] data).
Here is the scheme. An object of the Variable class holds an integer (its index or subscript) whose value says if it is an X0 or X1 or X2 or When a Variable object is asked to evaluate itself, it uses its indexvariable to look in the data array to find its value, and returns this value. All the other Node subclasses work just as they did before. When you write the code for this problem, make Node an abstract class, as we did in class.
Create algebraic trees of a fixed size, just as you did for the Arithmetic project previously. Each tree should be a balanced binary tree, containing three operators and four terminals. For the terminals, first flip a fair coin. If it comes up heads, choose a random constant in the range [1, 20]. If it comes up tails, choose a variable, randomly in the range {X0, X1, X2}. Have your main() method evaluate your tree twice, by using the values {X1, X2, X3} = {1, 2, 3} and {X1, X2, X3} = {4, 5, 6}.
This is what I have already made:
9 import java.util.Random; 10 11 public class TestAlgebra 12 { 13 static Random rand = new Random(); 14 15 public static void main(String[] args) 16 { 17 for (int i = 0; i < 5; i++) //Produces 5 random arithmetic problems. 18 { 20 Node n = randOperator( randOperator( randConstant(), randConstant() ), 21 randOperator( randConstant(), randConstant() ) ); 22 23 System.out.println(n + " = " + n.eval(double[] data)); 27 } 28 } 29 30 //---------------------------------------------------------- 41 public static Binop randOperator (Node left, Node right) //Line 20 42 { 43 int randomOperator = rand.nextInt(4); 44 45 switch (randomOperator) //Randomly selects 1 of 4 operators methods. 46 { 47 case 0: 48 return new Plus(left , right); 49 case 1: 50 return new Minus(left , right); 51 case 2: 52 return new Mult(left , right); 53 case 3: 54 return new Divide(left , right); 55 default: 56 return null; 57 } 58 } 59 60 //------------------------------------------- 67 public static Node randConstant() 68 { 69 int randomConstant = rand.nextInt(20); 70 randomConstant += 1; //Changes range from 0-19 to 1-20. 71 return new Const(randomConstant); 72 } 73 }
9 public abstract class Node 10 { 11 public Node() {} //default constructor 12 13 //Displays an error if Node is referenced and has no value. 14 public abstract double eval(double[] data); 15 }
9 public class Binop extends Node 10 { 12 //feilds---------------------- 13 protected Node lChild, rChild; 14 15 //Constructor to set feilds------------------------------------------- 21 public Binop(Node left, Node right) 22 { 23 lChild = left; 24 rChild = right; 25 } 26 }
9 public class Const extends Node 10 { 11 private double value; 15 16 public Const (double d) 17 { 18 value = d; 19 } 20 21 //---------------------------------------------------- 25 public double eval(double[] data) 26 { 27 return value; 28 } 29 35 public String toString() 36 { 37 return "" + value; 38 } 39 }
9 public class Variable extends Node 10 { 11 private int index; 12 13 //Value accepting constructor------------------------- 14 //Passed from TestArithmetic (Main) in line 71 of the randomConstant method. 15 16 public Variable (int i) 17 { 18 index = i; 19 } 24 25 public double eval(double[] data) 26 { 27 return index; 28 } 35 public String toString() 36 { 37 return "" + index; 38 } 39 }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
