Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am new in Java and need help on this. I am suppose to add two methods to a pro

ID: 3569379 • Letter: I

Question

I am new in Java and need help on this. I am suppose to add two methods to a program already written by our instructor. One is evaluate() and the other one is numPlus().

I also have a testing program given to me by our instructor. I have tried several different code but it keeps failing in the test. Can anyone help me on this?

Here are the codes I have written:

  
   public double evaluate(){
       if (root == null)
           throw new ArithmeticException("The tree is empty, nothing to be evaluated!");
       else
           return recursiveEvaluate(root);
      
   }
  
   private double recursiveEvaluate(ExpNode subTree){
       if (subTree == null)
           return 0;
       else
           return recursiveEvaluate(subTree.getLeft()) +
                   recursiveEvaluate(subTree.getRight()) + 1;
   }
  
   public int numPlus(){
       //if (root == null)
       return recursiveNumPlus(root);
   }
  
   private int recursiveNumPlus(ExpNode subTree){
       if (subTree == null)
           return 0;
      
       return recursiveNumPlus(subTree.getLeft()) +
               recursiveNumPlus(subTree.getRight()) + 1;
   }
}

Explanation / Answer

//ExpTree.java

import java.util.*;

public class ExpTree
{
//-------data
private ExpNode root;

//-------constructor
public ExpTree()
{
root = null;
}

//constructor where a string is passed in. It is parsed and stored
public ExpTree(String expString)
{
//declare StringTokenizer, Stacks, and other variables used in parsing
StringTokenizer tokenizer = new StringTokenizer (expString, "()+-*/%", true);
String token;
ExpNode operator, leftOperand, rightOperand;
Stack<ExpNode> operators = new Stack<ExpNode>();
Stack<ExpNode> operands = new Stack<ExpNode>();

//break up expString into tokens
while (tokenizer.hasMoreTokens())
{
token = tokenizer.nextToken();

// if the current token is a left paren, ignore it
if (token.equals ("("))
;

// if the current token is an operator, put it on the
// operator stack
else if ((token.equals ("+")) || (token.equals ("-")) ||
(token.equals ("*")) || (token.equals ("/")) || (token.equals ("%")))
operators.push (new ExpNode(token));

//if the current token is a right paren, pop the operators stack
//to get the operator, pop the operands stack twice to get the two
//operands (stored as expression trees). Then make the two operands
//children of the operator and push back on the operands tree.
else if (token.equals (")"))
{
operator = operators.pop();

rightOperand = operands.pop();
leftOperand = operands.pop();

operator.setLeft(leftOperand);
operator.setRight(rightOperand);

operands.push(operator);
}

//otherwise, the token should be a number - put it in the operands stack
else
operands.push (new ExpNode(token));

} // while (tokenizer.hasMoreTokens())

//when finished parsing, the operands stack should contain the fully-built
//expression tree.
if (!operands.isEmpty())
root = operands.pop();
}

//-------methods

//isEmpty()
public boolean isEmpty()
{
return (root == null);
}

//printTree methods - prints the tree in RNL order, with indents. Called from "outside"
public void printTree()
{
if (root == null)
System.out.println("The tree is empty");
else
printTree(root, 0); //start with the root with 0 indentations
}

//recursive, private version of printTree
private void printTree(ExpNode subTree, int indents)
{
//if there is a right side, handle it first (with 1 more indent)
if (subTree.getRight() != null)
printTree(subTree.getRight(), indents+1);

//then print the node itself (first move over the right amount of indents)
System.out.println(" ");
for (int i=0; i<indents; i++)
System.out.print(" ");
System.out.println(subTree);

//if there is a left side, handle it first (with 1 more indent)
if (subTree.getLeft() != null)
printTree(subTree.getLeft(), indents+1);
}

//inorder traversal - starts the recursive calls to print inorder
public String inOrder()
{
return inOrder(root);
}

//inorder traversal - recursive left side of tree, print node, right side of tree
private String inOrder(ExpNode theTreeToTraverse)
{
if (theTreeToTraverse == null)
return ""; //don't try to do anything if tree is null

//else build up a String to return. It will involve recursive calls
String returnString = "";
if (theTreeToTraverse.getLeft() != null)
{
returnString += "(" + inOrder(theTreeToTraverse.getLeft());
}
returnString += theTreeToTraverse;
if (theTreeToTraverse.getRight() != null)
{
returnString += inOrder(theTreeToTraverse.getRight()) + ")";
}

return returnString;
}
  
public double evaluate(){
if (root == null)
throw new ArithmeticException("The tree is empty, nothing to be evaluated!");
else
return Math.round(recursiveEvaluate(root)*100) / 100.0;
  
}
  
private double recursiveEvaluate(ExpNode subTree){
if (subTree == null)
return 0;
else if(subTree.getData().equals("+"))
return recursiveEvaluate(subTree.getLeft()) +
recursiveEvaluate(subTree.getRight()) ;
else if(subTree.getData().equals("-"))
return recursiveEvaluate(subTree.getLeft()) -
recursiveEvaluate(subTree.getRight()) ;
else if(subTree.getData().equals("*"))
return recursiveEvaluate(subTree.getLeft()) *
recursiveEvaluate(subTree.getRight()) ;
else if(subTree.getData().equals("/"))
{
double right = recursiveEvaluate(subTree.getRight());
if(right == 0.0)
throw new ArithmeticException("Divide by zero exception");

return recursiveEvaluate(subTree.getLeft()) / right;
}
else if(subTree.getData().equals("%"))
{
double right = recursiveEvaluate(subTree.getRight());
if(right == 0.0)
throw new ArithmeticException("Mod by zero exception");

return recursiveEvaluate(subTree.getLeft()) % right;
}
else
return Double.parseDouble(subTree.getData());

}
  
public int numPlus(){
//if (root == null)
return recursiveNumPlus(root);
}
  
private int recursiveNumPlus(ExpNode subTree){
if (subTree == null)
return 0;
  
if(subTree.getData().equals("+"))
return recursiveNumPlus(subTree.getLeft()) +
recursiveNumPlus(subTree.getRight()) + 1;
else
return recursiveNumPlus(subTree.getLeft()) +
recursiveNumPlus(subTree.getRight());
}
}

//***************************************************************************
// ExpNode holds a "node" for an ExpTree.
class ExpNode
{
//data
private String data;
private ExpNode left;
private ExpNode right;

//constructor
public ExpNode(String el)
{
data = el;
left = right = null;
}

//methods
//toString() - this is how an ExpNode represents itself as a String
public String toString()
{
return data;
}

//getLeft - returns the reference to the left subTree
public ExpNode getLeft()
{
return left;
}

//getRight - returns the reference to the right subTree
public ExpNode getRight()
{
return right;
}

//getData - returns the data (could be an operator or a number, so returns as a String)
public String getData()
{
return data;
}

//setLeft - sets the left subTree to whatever is passed in
public void setLeft(ExpNode newNode)
{
left = newNode;
}

//setRight - sets the right subTree to whatever is passed in
public void setRight(ExpNode newNode)
{
right = newNode;
}

}

------------------------------------------------

OUTPUT

run:


----------------------------------------------------
Part 1: Testing empty ExpTree
----------------------------------------------------

Test 1a: testing with numPlus()
expected: 0
got: 0

Test 1b: testing with evaluate()
expected: java.lang.ArithmeticException: <your string>
got: java.lang.ArithmeticException: The tree is empty, nothing to be evaluated!


----------------------------------------------------
Part 2: Testing simple tree w/integers: new ExpTree, passing in (5+3)
----------------------------------------------------

Test 2a: testing with numPlus()
expected: 1
got: 1

Test 2b: testing with evaluate()
expected: 8.0
got: 8.0


----------------------------------------------------
Part 3: Testing simple tree w/doubles: new ExpTree, passing in (8.14+6.2)
----------------------------------------------------

Test 3a: testing with numPlus()
expected: 1
got: 1

Test 3b: testing with evaluate()
expected: 14.34
got: 14.34


----------------------------------------------------
Part 4: Testing other operations
----------------------------------------------------

Test 4a: a new ExpTree, with input (5.3-1.2) evaluate()
expected: 4.1
got: 4.1

Test 4b: a new ExpTree, with input (5.3*1.2) evaluate()
expected: 6.36
got: 6.36

Test 4c: a new ExpTree, with input (5/2) evaluate()
expected: 2.5
got: 2.5

Test 4d: a new ExpTree, with input (17%3) evaluate()
expected: 2.0
got: 2.0


----------------------------------------------------
Part 5: Testing exception handling - zero in denominator
----------------------------------------------------

Test 5a: evaluate a division by zero: "(3/0)"
expected: java.lang.ArithmeticException: <your string>
got: java.lang.ArithmeticException: Divide by zero exception

Test 5b: evaluate a mod by zero: "(3%0)"
expected: java.lang.ArithmeticException: <your string>
got: java.lang.ArithmeticException: Mod by zero exception


----------------------------------------------------
Part 6: Testing example program from requirements: new ExpTree with input (((3*4)+(2-(4+2)))+10)
----------------------------------------------------

Test 6a: testing with numPlus()
expected: 3
got: 3

Test 6b: testing with evaluate()
expected: 18.0
got: 18.0