Question
JAVA**** Could anyone help me fix the randomOperator method with these directions: you need a random object. You could either create it inside the method
JAVA****
Could anyone help me fix the randomOperator method with these directions: you need a random object. You could either create it inside the method or create a static object.
This is the code so far:
17 public static Binop randomOperator(Node l, Node r) { 18 int operator = random.nextInt(4); 19 switch (operator) { 20 case 0: 21 return new Plus(l, r); 22 case 1: 23 return new Subtract(l, r); 24 case 2: 25 return new Multiply(l, r); 26 case 3: 27 return new Divide(l, r); 28 default: 29 return null; 30 } 31 } 32
Also could anyone help me fix these 5 nodes below with the following directions: When you need a constant, call the randConstant() method to give you a constant node. When you need a binop node, call the randOperator() method to give you one.
Node n1 = new Const(1.1);
Node n2 = new Const(2.2);
Node n3 = new Plus(n1, n2);
Node n4 = new Const(3.3);
Node n5 = new Plus(n3, n4);
Any help is appreciated, if anyone could answer this asap that would be great!
This is the complete code:
1 import java.util.Random; 2 3 public class TestArithmetic { 4 5 static Random random = new Random(); 6 7 public static void main(String[] args) { 8 9 for (int i = 0; i < 5; i++) { 10 Node n = randomOperator( randomOperator( randomConstant(), randomConstant() ), randomOperator( randomConstant(), randomConstant() ) ); 11 printResult(n); 12 } 13 14 } 15 16 17 public static Binop randomOperator(Node l, Node r) { 18 int operator = random.nextInt(4); 19 switch (operator) { 20 case 0: 21 return new Plus(l, r); 22 case 1: 23 return new Subtract(l, r); 24 case 2: 25 return new Multiply(l, r); 26 case 3: 27 return new Divide(l, r); 28 default: 29 return null; 30 } 31 } 32 33 public static Constant randomConstant() { 34 int constant = random.nextInt(20) + 1; 35 return new Constant(constant); 36 } 37 38 public static void printResult(Node node) { 39 40 41 double result = node.eval(); 42 int round = (int) (result * 10); 43 44 if (result * 10 - (double) round != 0) { 45 System.out.print(node.toString() + " = " ); 46 System.out.printf( "%.2f", node.eval() ); 47 System.out.println(); 48 } else { 49 System.out.print(node.toString() + " = " ); 50 System.out.printf( "%.1f", node.eval() ); 51 System.out.println(); 52 } 53 54 } 55 56 }
1 import java.util.Random; 2 3 4 public class Plus extends Binop { 5 6 public Plus(Node l, Node r) { 7 super(l, r); 8 } 9 10 public double eval() { 11 return lChild.eval() + rChild.eval(); 12 } 13 14 public String toString() { 15 return "(" + lChild.toString() + " + " + rChild.toString() + ")"; 16 } 17 }
1 import java.util.Random; 2 3 public class Const extends Node { 4 5 private double value; 6 public Const(double d) { 7 value = d; 8 } 9 10 public double eval() { 11 return value; 12 } 13 14 public String toString() { 15 return "" + value; 16 } 17 }
1 import java.util.Random; 2 3 4 public class Multiply extends Binop { 5 6 public Multiply(Node l, Node r) { 7 super(l, r); 8 } 9 10 public double eval() { 11 return lChild.eval() * rChild.eval(); 12 } 13 14 public String toString() { 15 return "(" + lChild.toString() + " * " + rChild.toString() + ")"; 16 } 17 }
1 import java.util.Random; 2 3 public class Subtract extends Binop { 4 5 public Subtract(Node l, Node r) { 6 super(l, r); 7 } 8 9 public double eval() { 10 return lChild.eval() - rChild.eval(); 11 } 12 13 public String toString() { 14 return "(" + lChild.toString() + " - " + rChild.toString() + ")"; 15 } 16 17 }
1 import java.util.Random; 2 3 4 public class Divide extends Binop { 5 6 public Divide(Node l, Node r) { 7 super(l, r); 8 } 9 10 public double eval() { 11 return lChild.eval() / rChild.eval(); 12 } 13 14 public String toString() { 15 return "(" + lChild.toString() + " / " + rChild.toString() + ")"; 16 } 17 }
1 import java.util.Random; 2 3 public class Binop extends Node { 4 5 protected Node lChild, rChild; 6 public Binop(Node l, Node r) { 7 lChild = l; 8 rChild = r; 9 } 10 11 12 }
1 import java.util.Random; 2 public class Node { 3 4 public Node() { 5 } 6 public double eval() { 7 System.out.println("Error: eval Node"); 8 return 0; 9 } 10 }
Step 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