TestArithmetic.java
import java.util.Random;
public class TestArithmetic {
static Random random = new Random();
public static void main(String[] args) {
for (int i = 0; i < 5; i++)
{
Node n =
randOperator( randOperator( randConstant(), randConstant() ),
randOperator( randConstant(), randConstant() ) );
printResult(n);
}
// Node node2 = new
Divide(randConstant(), new Const(0));
//
System.out.println(node2.eval());
}
public static Binop randOperator(Node l, Node r)
{
int operator =
random.nextInt(4);
switch (operator) {
case 0:
return new
Plus(l, r);
case 1:
return new
Minus(l, r);
case 2:
return new
Mult(l, r);
case 3:
return new
Divide(l, r);
default:
return
null;
}
}
public static Node randConstant() {
int constant = random.nextInt(20) +
1; // generate constant integers randomly in the range from 1 to
20
return new Const(constant);
}
public static void printResult(Node node) {
double result = node.eval();
int round = (int) (result *
10);
if (result * 10 - (double) round !=
0) {
System.out.print(node.toString() + " = " );
System.out.printf( "%.2f", node.eval() );
System.out.println();
} else {
System.out.print(node.toString() + " = " );
System.out.printf( "%.1f", node.eval() );
System.out.println();
}
}
}
Node.java
public class Node {
/**
* Constructor
*/
public Node() {
}
/**
* @return the evaluated value of this node
*/
public double eval() {
System.out.println("Error: eval
Node");
return 0;
}
}
Binop.java
public class Binop extends Node {
protected Node lChild, rChild;
/**
* @param l
* @param r
*/
public Binop(Node l, Node r) {
lChild = l;
rChild = r;
}
}
Divide.java
public class Divide extends Binop {
public Divide(Node l, Node r) {
super(l, r);
}
public double eval() {
return lChild.eval() /
rChild.eval();
}
public String toString() {
return "(" + lChild.toString() + "
/ " + rChild.toString() + ")";
}
}
Minus.java
public class Minus extends Binop {
public Minus(Node l, Node r) {
super(l, r);
}
public double eval() {
return lChild.eval() -
rChild.eval();
}
public String toString() {
return "(" + lChild.toString() + "
- " + rChild.toString() + ")";
}
}
Mult.java
public class Mult extends Binop {
public Mult(Node l, Node r) {
super(l, r);
}
public double eval() {
return lChild.eval() *
rChild.eval();
}
public String toString() {
return "(" + lChild.toString() + "
* " + rChild.toString() + ")";
}
}
Const.java
public class Const extends Node {
private double value;
/**
* Constructor to initialize the constant value for
operation
*
* @param d constant value
*/
public Const(double d) {
value = d;
}
public double eval() {
return value;
}
public String toString() {
return "" + value;
}
}
Plus.java
public class Plus extends Binop {
public Plus(Node l, Node r) {
super(l, r);
}
public double eval() {
return lChild.eval() +
rChild.eval();
}
public String toString() {
return "(" + lChild.toString() + "
+ " + rChild.toString() + ")";
}
}
ECLIPSE Extend the Arithrmetic example from the course notes to include classes Minus, Mult and Divide....
Code to be written in C++: Initially, you will be given symbols printed in a preorder traversal of a boolean expression tree. The internal nodes of the tree will contain one of the following operators: & (and), | (or), ^ (exclusive-or), or ! (not). The nodes containing the first three operators will have two children, while the nodes containing the ! operator will contain only a left child. The leaves of the tree will contain an operand - either f...