Edit XMLTreeIntExpressionEvaluatorjava to implement the firs
Edit XMLTreeIntExpressionEvaluator.java to implement the first version of the evaluate method using recursion. This is the one that uses int for the operand type and computes the value of the expression with Java\'s built-in integer operators. Here is the contract: /** * Evaluate the given expression. * * @param exp * the {@code XMLTree} representing the expression * @return the value of the expression * @requires
* @ensures evaluate = [the value of the expression] */ private static int evaluate(XMLTree exp) {...} As the requires clause states, you can assume that the given XMLTree is a well-formed XML expression and you do not need to check for errors. You can use the Integer.parseInt(String) static method to convert a String value into an integer.
import components.simplereader.SimpleReader;
import components.simplereader.SimpleReader1L;
import components.simplewriter.SimpleWriter;
import components.simplewriter.SimpleWriter1L;
import components.xmltree.XMLTree;
import components.xmltree.XMLTree1;
/**
* Program to evaluate XMLTree expressions of {@code int}.
*
* @author
*
*/
public final class XMLTreeIntExpressionEvaluator {
/**
* Private constructor so this utility class cannot be instantiated.
*/
private XMLTreeIntExpressionEvaluator() {
}
/**
* Evaluate the given expression.
*
* @param exp
* the {@code XMLTree} representing the expression
* @return the value of the expression
* @requires <pre>
* [exp is a subtree of a well-formed XML arithmetic expression] and
* [the label of the root of exp is not \"expression\"]
* </pre>
* @ensures evaluate = [the value of the expression]
*/
private static int evaluate(XMLTree exp) {
assert exp != null : \"Violation of: exp is not null\";
// TODO - fill in body
/*
* This line added just to make the program compilable. Should be
* replaced with appropriate return statement.
*/
return 0;
}
/**
* Main method.
*
* @param args
* the command line arguments
*/
public static void main(String[] args) {
SimpleReader in = new SimpleReader1L();
SimpleWriter out = new SimpleWriter1L();
out.print(\"Enter the name of an expression XML file: \");
String file = in.nextLine();
while (!file.equals(\"\")) {
XMLTree exp = new XMLTree1(file);
out.println(evaluate(exp.child(0)));
out.print(\"Enter the name of an expression XML file: \");
file = in.nextLine();
}
in.close();
out.close();
}
}
Solution
import components.simplereader.SimpleReader ;
import components.simplereader.SimpleReader1L ;
import components.simplewriter.SimpleWriter ;
import components.simplewriter.SimpleWriter1L ;
import components.utilities.Reporter ;
import components.xmltree.XMLTree ;
import components.xmltree.XMLTree1 ;
/**
* Program to evaluate XMLTree expressions of {@code int}.
*
* @author Put your name here
*
*/
public final class XMLTreeIntExpressionEvaluator {
/**
* Private constructor so this utility class cannot be instantiated.
*/
private XMLTreeIntExpressionEvaluator ( ) {
}
/**
* Evaluate the given expression.
*
* @param exp
* the {@code XMLTree} representing the expression
* @return the value of the expression
* @requires <pre>
* [exp is a subtree of a well-formed XML arithmetic expression] and
* [the label of the root of exp is not \"expression\"]
* </pre>
* @ensures evaluate = [the value of the expression]
*/
private static NaturalNumber evaluate ( XMLTree exp ) {
assert exp != null : \" Violation of : exp is not null \" ;
NaturalNumber noReOccur =new NaturalNumber1L ( ) ;
if ( exp.label ( ).equals ( “ number ” ) {
noReOccur =new NaturalNumber1L ( exp.attributeValue ( “ value ” ) ) ;
} else {
String action =exp.label ( ) ;
XMLTree one =exp.child ( 0 ) ;
XMLTree two =exp.child ( 1 ) ;
noReOccur.transferFrom ( evaluate ( one ) ) ;
if ( action.equals ( “ plus ” ) ) {
noReOccur.add ( evaluate ( two ) ) ;
} else if ( action.equals ( “ divide ” ) ) {
noReOccur.divide ( evaluate ( two ) ) ;
} else if ( action.equals ( “ times ” ) ) {
noReOccur.multiply ( evaluate ( two ) ) ;
} else {
noReOccur.subtract ( evaluate ( two ) ) ;
}
}
return noReOccur ;
}
/**
* Main method.
*
* @param args
* the command line arguments
*/
public static void main ( String[] args ) {
SimpleReader in =new SimpleReader1L ( ) ;
SimpleWriter out =new SimpleWriter1L ( ) ;
out.print ( \" Enter the name of an expression XML file : \" ) ;
String file =in.nextLine ( ) ;
while( !file.equals ( \"\" ) ) {
XMLTree exp =new XMLTree1 ( file ) ;
out.println ( evaluate ( exp.child ( 0 ) ) ) ;
out.print ( \" Enter the name of an expression XML file : \" ) ;
file =in.nextLine ( ) ;
}
in.close ( ) ;
out.close ( ) ;
}
}



