Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Here are my previous Arithmetic Java Files: (Main) (Classes) For this homework problem, turn the Arithmetic project from the last homework into the Algebra project,
Here are my previous Arithmetic Java Files:
(Main)
(Classes)
For this homework problem, 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 XOor 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 ((XO 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 isn't really different. It just has a new node type, the Variable node, and can be slightly more complex than the expressions you created before: | 3.1 1.3 2.3 X2 X1 2.0 But how can we evaluate a tree like this? 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 index variable 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. For this part of the assignment, 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 {XO, 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}. Submit .java files for your work, including a test file containing a main method that demonstrates that you can generate random algebraic trees that can evaluate themselves. public class TestArithmetic { static Random random = new Random(); public static void main(String[] args) { for (int i = 0; iStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started