(Solved Homework): Please respond to both questions to a give code A) Complete the evaluate(double x) method, which returns the value of

 

Please respond to both questions to a give code

A) Complete the evaluate(double x) method, which returns the value of
the expression rooted at the treeNode on which it called, for a particular
value of x: For example, evaluate(1) on the tree above should return
approximately 2.01. . . . For sin and cos, angles are measured in radians.

B) [Bonus, 30%] Complete the di erentiate() method, which returns a
new expression tree describing the derivative of the expression described
by the tree rooted at the treeNode on which it is called. Notice that
the original tree should remain intact and that the tree containing the
derivative should share no node with the original.

Here is the given code!

import java.lang.Math.*;

class expressionTreeNode {
private String value;
private expressionTreeNode leftChild, rightChild, parent;

expressionTreeNode() {
value = null;
leftChild = rightChild = parent = null;
}

// Constructor
/* Arguments: String s: Value to be stored in the node
expressionTreeNode l, r, p: the left child, right child, and parent of the node to created
Returns: the newly created expressionTreeNode
*/
expressionTreeNode(String s, expressionTreeNode l, expressionTreeNode r, expressionTreeNode p) {
value = s;
leftChild = l;
rightChild = r;
parent = p;
}

/* Basic access methods */
String getValue() { return value; }

expressionTreeNode getLeftChild() { return leftChild; }

expressionTreeNode getRightChild() { return rightChild; }

expressionTreeNode getParent() { return parent; }

/* Basic setting methods */
void setValue(String o) { value = o; }

// sets the left child of this node to n
void setLeftChild(expressionTreeNode n) {
leftChild = n;
n.parent = this;
}

// sets the right child of this node to n
void setRightChild(expressionTreeNode n) {
rightChild = n;
n.parent=this;
}
// Returns the root of the tree describing the expression s
// Watch out: it makes no validity checks whatsoever!
expressionTreeNode(String s) {
// check if s contains parentheses. If it doesn’t, then it’s a leaf
if (s.indexOf(“(“)==-1) setValue(s);
else { // it’s not a leaf

/* break the string into three parts: the operator, the left operand,
and the right operand. ***/
setValue( s.substring( 0 , s.indexOf( “(” ) ) );
// delimit the left operand 2008
int left = s.indexOf(“(“)+1;
int i = left;
int parCount = 0;
// find the comma separating the two operands
while (parCount>=0 && !(s.charAt(i)==’,’ && parCount==0)) {
if ( s.charAt(i) == ‘(‘ ) parCount++;
if ( s.charAt(i) == ‘)’ ) parCount–;
i++;
}
int mid=i;
if (parCount<0) mid–;

// recursively build the left subtree
setLeftChild(new expressionTreeNode(s.substring(left,mid)));

if (parCount==0) {
// it is a binary operator
// find the end of the second operand.F13
while ( ! (s.charAt(i) == ‘)’ && parCount == 0 ) ) {
if ( s.charAt(i) == ‘(‘ ) parCount++;
if ( s.charAt(i) == ‘)’ ) parCount–;
i++;
}
int right=i;
setRightChild( new expressionTreeNode( s.substring( mid + 1, right)));
}
}
}

// Returns a copy of the subtree rooted at this node… 2014
expressionTreeNode deepCopy() {
expressionTreeNode n = new expressionTreeNode();
n.setValue( getValue() );
if ( getLeftChild()!=null ) n.setLeftChild( getLeftChild().deepCopy() );
if ( getRightChild()!=null ) n.setRightChild( getRightChild().deepCopy() );
return n;
}

// Returns a String describing the subtree rooted at a certain node.
public String toString() {
String ret = value;
if ( getLeftChild() == null ) return ret;
else ret = ret + “(” + getLeftChild().toString();
if ( getRightChild() == null ) return ret + “)”;
else ret = ret + “,” + getRightChild().toString();
ret = ret + “)”;
return ret;
}

// Returns the value of the expression rooted at a given node
// when x has a certain value
double evaluate(double x) {
// WRITE YOUR CODE HERE

// AND CHANGE THIS RETURN STATEMENT
return 0;
}

/* returns the root of a new expression tree representing the derivative of the
original expression */
expressionTreeNode differentiate() {
// WRITE YOUR CODE HERE

// AND CHANGE THIS RETURN STATEMENT
return null;
}

public static void main(String args[]) {
expressionTreeNode e = new expressionTreeNode(“mult(add(2,x),cos(x))”);
System.out.println(e);
System.out.println(e.evaluate(1));
System.out.println(e.differentiate());

}
}

Expert Answer

 import java.lang.Math.*;

class expressionTreeNode {

private String value;
private expressionTreeNode leftChild, rightChild, parent;

expressionTreeNode() {
value = null;
leftChild = rightChild = parent = null;
}

// Constructor
/* Arguments: String s: Value to be stored in the node
expressionTreeNode l, r, p: the left child, right child, and parent of the node to created
Returns: the newly created expressionTreeNode
*/
expressionTreeNode(String s, expressionTreeNode l, expressionTreeNode r, expressionTreeNode p) {
value = s;
leftChild = l;
rightChild = r;
parent = p;
}

/* Basic access methods */
String getValue() {
return value;
}

expressionTreeNode getLeftChild() {
return leftChild;
}

expressionTreeNode getRightChild() {
return rightChild;
}

expressionTreeNode getParent() {
return parent;
}

/* Basic setting methods */
void setValue(String o) {
value = o;
}

// sets the left child of this node to n
void setLeftChild(expressionTreeNode n) {
leftChild = n;
n.parent = this;
}

// sets the right child of this node to n
void setRightChild(expressionTreeNode n) {
rightChild = n;
n.parent = this;
}

// Returns the root of the tree describing the expression s
// Watch out: it makes no validity checks whatsoever!
expressionTreeNode(String s) {
// check if s contains parentheses. If it doesn’t, then it’s a leaf
if (s.indexOf(“(“) == -1) {
setValue(s);
} else { // it’s not a leaf
/* break the string into three parts: the operator, the left operand,
and the right operand. ***/
setValue(s.substring(0, s.indexOf(“(“)));
// delimit the left operand 2008
int left = s.indexOf(“(“) + 1;
int i = left;
int parCount = 0;
// find the comma separating the two operands
while (parCount >= 0 && !(s.charAt(i) == ‘,’ && parCount == 0)) {
if (s.charAt(i) == ‘(‘) {
parCount++;
}
if (s.charAt(i) == ‘)’) {
parCount–;
}
i++;
}
int mid = i;
if (parCount < 0) {
mid–;
}
// recursively build the left subtree
setLeftChild(new expressionTreeNode(s.substring(left, mid)));

if (parCount == 0) {
// it is a binary operator
// find the end of the second operand.F13
while (!(s.charAt(i) == ‘)’ && parCount == 0)) {
if (s.charAt(i) == ‘(‘) {
parCount++;
}
if (s.charAt(i) == ‘)’) {
parCount–;
}
i++;
}
int right = i;
setRightChild(new expressionTreeNode(s.substring(mid + 1, right)));
}
}
}

// Returns a copy of the subtree rooted at this node… 2014
expressionTreeNode deepCopy() {
expressionTreeNode n = new expressionTreeNode();
n.setValue(getValue());
if (getLeftChild() != null) {
n.setLeftChild(getLeftChild().deepCopy());
}
if (getRightChild() != null) {
n.setRightChild(getRightChild().deepCopy());
}
return n;
}

// Returns a String describing the subtree rooted at a certain node.
public String toString() {
String ret = value;
if (getLeftChild() == null) {
return ret;
} else {
ret = ret + “(” + getLeftChild().toString();
}
if (getRightChild() == null) {
return ret + “)”;
} else {
ret = ret + “,” + getRightChild().toString();
}
ret = ret + “)”;
return ret;
}

// Returns the value of the expression rooted at a given node
// when x has a certain value
double evaluate(double x) {
expressionTreeNode left = getLeftChild();
expressionTreeNode right = getRightChild();
if (left == null && right == null) {
if (value.toString().equals(“x”)) {
return x;
} else {
return new Double(value.toString());
}
}
String op = value.toString();
if (op.equals(“add”)) {
return left.evaluate(x) + right.evaluate(x);
} else if (op.equals(“mult”)) {
return left.evaluate(x) * right.evaluate(x);
} else if (op.equals(“minus”)) {
return left.evaluate(x) – right.evaluate(x);
} else if (op.equals(“cos”)) {
return java.lang.Math.cos(left.evaluate(x));
} else if (op.equals(“sin”)) {
return java.lang.Math.sin(left.evaluate(x));
} else if (op.equals(“exp”)) {
return java.lang.Math.exp(left.evaluate(x));
} else {
System.out.println(“Invalid operator in eval(): ” + op);
}
return 0;
}

/* returns the root of a new expression tree representing the derivative of the
original expression */
expressionTreeNode differentiate() {
expressionTreeNode left = getLeftChild();
expressionTreeNode right = getRightChild();
if (left == null && right == null) {
if (value.toString().equals(“x”)) {
return new expressionTreeNode(“1”);
} else {
return new expressionTreeNode(“0”);
}
}
String op = value.toString();
if (op.equals(“add”)) {
return new expressionTreeNode(“add”, left.differentiate(), right.differentiate(), null);
} else if (op.equals(“mult”)) {
expressionTreeNode prod = new expressionTreeNode(“add”, null, null, null);
prod.setLeftChild(new expressionTreeNode(“mult”, right.differentiate(), left, null));
prod.setRightChild(new expressionTreeNode(“mult”, left.differentiate(), right, null));
return prod;
} else if (op.equals(“minus”)) {
return new expressionTreeNode(“minus”, left.differentiate(), right.differentiate(), null);
} else if (op.equals(“cos”)) {
expressionTreeNode copy = deepCopy();
copy.setValue(“sin”);
expressionTreeNode prod = new expressionTreeNode(“mult”, left.differentiate(), null, null);
prod.setRightChild(new expressionTreeNode(“minus”, new expressionTreeNode(“0”), copy, null));
return prod;
} else if (op.equals(“sin”)) {
expressionTreeNode copy = deepCopy();
copy.setValue(“cos”);
expressionTreeNode prod = new expressionTreeNode(“mult”, left.differentiate(), null, null);
prod.setRightChild(copy);
return prod;
} else if (op.equals(“exp”)) {
expressionTreeNode copy = deepCopy();
expressionTreeNode prod = new expressionTreeNode(“mult”, left.differentiate(), null, null);
prod.setRightChild(copy);
return prod;
} else {
System.out.println(“Invalid operator in diff(): ” + op);
}
return new expressionTreeNode(“0”);
}

public static void main(String args[]) {
expressionTreeNode e = new expressionTreeNode(“mult(add(2,x),cos(x))”);
System.out.println(e);
System.out.println(e.evaluate(1));
System.out.println(e.differentiate());

}
}

OUTPUT

iWriteHomework
Order NOW for a 10% Discount
Pages (550 words)
Approximate price: -

Why Us?

Top Quality and Well-Researched Papers

All ourbpapers are written from scratch. In fact, Clients who ask for paraphrasing services are highly discouraged. We have writers ready to craft any paper from scratch and deliver quality ahead of time.

Professional and Experienced Academic Writers

Our writers keeps you posted on your papers progress - providing you with paper outline/draft. You are also at liberty to communicate directly with your writer.

Free Unlimited Revisions

If you think we missed something, send your order for a free revision. You have 10 days to submit the order for review after you have received the final document. You can do this yourself after logging into your personal account or by contacting our support.

Prompt Delivery and 100% Money-Back-Guarantee

All papers are always delivered on time, in many cases quite ahead of time. In case we need more time to master your paper, we may contact you regarding the deadline extension. In case you cannot provide us with more time, a 100% refund is guaranteed.

Original & Confidential

We use several writing tools checks to ensure that all documents you receive are free from plagiarism. Our editors carefully review all quotations in the text. We also promise maximum confidentiality in all of our services.

24/7 Customer Support

Our support agents are available 24 hours a day 7 days a week and committed to providing you with the best customer experience. Get in touch whenever you need any assistance.

Try it now!

Calculate the price of your order

Total price:
$0.00

How it works?

Follow these simple steps to get your paper done

Place your order

Fill in the order form and provide all details of your assignment.

Proceed with the payment

Choose the payment system that suits you most.

Receive the final file

Once your paper is ready, we will email it to you.

Our Services

No need to work on your paper at night. Sleep tight, we will cover your back. We offer all kinds of writing services.

Essays

Essay Writing Service

No matter what kind of academic paper you need and how urgent you need it, you are welcome to choose your academic level and the type of your paper at an affordable price. We take care of all your paper needs and give a 24/7 customer care support system.

Admissions

Admission Essays & Business Writing Help

An admission essay is an essay or other written statement by a candidate, often a potential student enrolling in a college, university, or graduate school. You can be rest assurred that through our service we will write the best admission essay for you.

Reviews

Editing Support

Our academic writers and editors make the necessary changes to your paper so that it is polished. We also format your document by correctly quoting the sources and creating reference lists in the formats APA, Harvard, MLA, Chicago / Turabian.

Reviews

Revision Support

If you think your paper could be improved, you can request a review. In this case, your paper will be checked by the writer or assigned to an editor. You can use this option as many times as you see fit. This is free because we want you to be completely satisfied with the service offered.

× Contact Live Agents