Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

My code compiles fine in eclipse, but it wont compile in the program my teacher is grading in. He is using putty(a terminal emulator) to

My code compiles fine in eclipse, but it wont compile in the program my teacher is grading in. He is using putty(a terminal emulator) to run the program. Here are his instructions :

image text in transcribed

What do I need to change in my source code to make it work in these conditions? Below is my source code. Please provide the compileable code, how you save the files, and screenshots of it compiling in a terminal emulator.

Here is my source code:

ExpressionTree.java

//

import java.util.HashMap; import java.util.Iterator; import java.util.Stack;

public class ExpressionTree extends BinaryTree implements ExpressionTreeInterface{ private HashMap variables; private String[] postfix; public ExpressionTree(String[] args) { variables = new HashMap(); postfix = args; } public double evaluate() { return evaluate(postfixToExpressionTree(postfix).getRootNode()); } public void setVariable(String str, double d){ variables.put(str, new Variable(str, d)); } private double evaluate(BinaryNode rootNode) { double result; if(rootNode == null) { result = 0; }else if(rootNode.isLeaf()) { String root = rootNode.getData(); if(isVariable(root)) result = variables.get(root).getValue(); else result = Double.parseDouble(root); }else { double firstOp = evaluate(rootNode.getLeftChild()); double secondOp = evaluate(rootNode.getRightChild()); String operator = rootNode.getData(); result = compute(operator, firstOp, secondOp); } return result; } private double compute(String operator, double first, double second){ switch(operator) { case "+": return first + second; case "-": return first - second; case "*": return first * second; case "/": if (second == 0) throw new ArithmeticException("Cannot divide by zero"); return first / second; } return 0; } private BinaryTree postfixToExpressionTree(String[] postfix){ Stack> stack = new Stack>(); for(String token : postfix) { if(isVariable(token) || isDouble(token)) { stack.push(new BinaryTree(token)); continue; }else if(!isOperator(token) && !isVariable(token) && !isDouble(token)) { throw new IllegalArgumentException("Variable must be defined."); } else if(isOperator(token)) { BinaryTree right = stack.pop(); BinaryTree left = stack.pop(); stack.push(new BinaryTree(token, left, right)); } } return stack.pop(); } private boolean isDouble(String str) { try { Double.parseDouble(str); return true; }catch(NumberFormatException e) { return false; } } private boolean isVariable(String str) { return variables.containsKey(str); } private boolean isOperator(String str) { switch(str) { case "+": case "-": case "*": case "/": return true; } return false; } public void displayPostfix() { String post = ""; Iterator it = postfixToExpressionTree(postfix).getPostorderIterator(); while(it.hasNext()) { post += it.next(); if(it.hasNext()) { post += " "; } } System.out.println(post); } }

ExpressionTreeInterface.java

public interface ExpressionTreeInterface extends BinaryTreeInterface { public double evaluate(); }

Variable.java

public class Variable { private String name; private double value; public Variable(String name) { this.name = name; } public Variable(String name, double value) { this.name = name; this.setValue(value); } public String getName() { return name; } public void setName(String newName) { this.name = newName; }

public double getValue() { return value; }

public void setValue(double value) { this.value = value; } }

BinaryNode.java

public class BinaryNode { private T data; private BinaryNode leftChild, rightChild; public BinaryNode() { this(null); } public BinaryNode(T data) { this(data, null, null); } public BinaryNode(T data, BinaryNode left, BinaryNode right) { this.data = data; leftChild = left; setRightChild(right); } public T getData() { return data; } public void setData(T data) { this.data = data; } public BinaryNode getLeftChild(){ return leftChild; } public void setLeftChild(BinaryNode left) { leftChild = left; }

public BinaryNode getRightChild() { return rightChild; }

public void setRightChild(BinaryNode right) { this.rightChild = right; } public boolean hasLeftChild() { return leftChild != null; } public boolean hasRightChild() { return rightChild != null; } public boolean isLeaf() { return (!hasLeftChild() && !hasRightChild()); } public BinaryNode copy(){ BinaryNode newRoot = new BinaryNode(data); if(leftChild != null) { newRoot.setLeftChild(leftChild.copy()); } if(rightChild != null) { newRoot.setRightChild(rightChild.copy()); } return newRoot; } public int getHeight() { return getHeight(this); } private int getHeight(BinaryNode node) { int height = 0; if(node != null) { height = 1 + Math.max(getHeight(node.getLeftChild()), getHeight(node.getRightChild())); } return height; } public int getNumberOfNodes() { return getNumberOfNodes(this); } private int getNumberOfNodes(BinaryNode node) { int rightNumber = 0; int leftNumber = 0; if(leftChild != null) { leftNumber = getNumberOfNodes(node.getLeftChild()); } if(rightChild != null) { rightNumber = getNumberOfNodes(node.getRightChild()); } return 1 + leftNumber + rightNumber; } }

BinaryTree.java

import java.util.Iterator; import java.util.Stack;

public class BinaryTree implements BinaryTreeInterface {

private BinaryNode root;

public BinaryTree() { root = null; }

public BinaryTree(T rootData) { root = new BinaryNode(rootData); }

public BinaryTree(T rootData, BinaryTree leftTree, BinaryTree rightTree) { privateSetTree(rootData, leftTree, rightTree); }

public void setTree(T rootData) { root = new BinaryNode(rootData); }

public void setTree(T rootData, BinaryTree left, BinaryTree right) { privateSetTree(rootData, left, right); }

private void privateSetTree(T rootData, BinaryTree left, BinaryTree right) { root = new BinaryNode(rootData);

if ((left != null) && (!left.isEmpty())) { root.setLeftChild(left.root.copy()); } if ((right != null) && (!right.isEmpty())) { root.setRightChild(right.root.copy()); } }

public T getRootData() { return root.getData(); }

public int getHeight() { return root.getHeight(); }

public int getNumberOfNodes() { return root.getNumberOfNodes(); }

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

public void clear() { root = null; }

protected BinaryNode getRootNode() { return root; }

public Iterator getPreorderIterator() { throw new UnsupportedOperationException("Preorder not supported."); }

public Iterator getInorderIterator() { throw new UnsupportedOperationException("Inorder not supported."); }

public Iterator getPostorderIterator() { return new PostorderIterator(); }

public Iterator getLevelorderIterator() { throw new UnsupportedOperationException("Level Order not supported."); }

private class PostorderIterator implements Iterator {

private Stack> nodeStack; private BinaryNode current;

public PostorderIterator() { nodeStack = new Stack(); current = root; populateStack(current); } private void populateStack(BinaryNode node){ nodeStack.add(node); if(node.hasRightChild()){ populateStack(node.getRightChild()); } if(node.hasLeftChild()){ populateStack(node.getLeftChild()); } }

public boolean hasNext() { return !nodeStack.isEmpty(); }

public T next() { return nodeStack.pop().getData(); }

}

}

BinaryTreeInterface.java

public interface BinaryTreeInterface extends TreeInterface, TreeIteratorInterface{ public void setTree(T rootData); public void setTree(T rootData, BinaryTree left, BinaryTree right); }

TreeIteratorInterface.java

import java.util.Iterator;

public interface TreeIteratorInterface { public Iterator getPreorderIterator(); public Iterator getInorderIterator(); public Iterator getPostorderIterator(); public Iterator getLevelorderIterator(); }

TreeInterface.java

public interface TreeInterface { public T getRootData(); public int getHeight(); public int getNumberOfNodes(); public boolean isEmpty(); public void clear(); }

ExpressionTest.java

package TreePackage;

import java.util.InputMismatchException;

import java.util.Scanner;

public class ExpressionTest {

public static boolean isDouble(String value) {

try {

Double.parseDouble(value);

return true;

} catch (NumberFormatException e) {

return false;

}

}

public static void main(String[] args) {

Scanner kb = new Scanner(System.in);

boolean hasExpression = false;

boolean quit = false;

String userInput = null;

ExpressionTree exp = new ExpressionTree(new String[]{"a", "b", "2", "/","+"});

while(!quit){

System.out.print(" J. Botkin's Expression Tree ");

if(hasExpression){

System.out.println("Current Expression: " + userInput

+ "");

}

System.out.println(" 1. Enter New Expression "

+ "2. Set Expression Variable "

+ "3. Evaluate Expression "

+ "4. Display Postfix "

+ "5. Quit");

try{

int option = kb.nextInt();

switch (option) {

case 1:

System.out.println("Enter Expression seperated by spaces.");

Scanner b = new Scanner(System.in);

userInput = b.nextLine();

String[] userExp = userInput.split(" ");

switch (userExp.length){

case 1:

exp = new ExpressionTree(new String[]{userExp[0]});

break;

case 2:

exp = new ExpressionTree(new String[]{userExp[0], userExp[1]});

break;

case 3:

exp = new ExpressionTree(new String[]{userExp[0], userExp[1], userExp[2]});

break;

case 4:

exp = new ExpressionTree(new String[]{userExp[0], userExp[1], userExp[2], userExp[3]});

break;

case 5:

exp = new ExpressionTree(new String[]{userExp[0], userExp[1], userExp[2], userExp[3],userExp[4]});

break;

case 6:

exp = new ExpressionTree(new String[]{userExp[0], userExp[1], userExp[2], userExp[3],userExp[4], userExp[5]});

break;

case 7:

exp = new ExpressionTree(new String[]{userExp[0], userExp[1], userExp[2], userExp[3],userExp[4], userExp[5],userExp[6]});

break;

case 8:

exp = new ExpressionTree(new String[]{userExp[0], userExp[1], userExp[2], userExp[3],userExp[4], userExp[5],userExp[6], userExp[7]});

break;

case 9:

exp = new ExpressionTree(new String[]{userExp[0], userExp[1], userExp[2], userExp[3],userExp[4], userExp[5],userExp[6], userExp[7], userExp[8]});

break;

}

/*for(int i = 0; i

if(isDouble(userExp[i])){

exp.setVariable(userExp[i], Double.parseDouble(userExp[i]));

}

}

hasExpression = true;*/

break;

case 2:

Scanner k = new Scanner(System.in);

System.out.print("Variable name: ");

String name = k.nextLine();

System.out.print("Variable value: ");

double value = k.nextDouble();

exp.setVariable(name, value);

break;

case 3:

try{

System.out.println(exp.evaluate());

}

catch (IllegalArgumentException e){

System.out.println("Please define all variables.");

}

break;

case 4:

try{

exp.displayPostfix();

}catch(IllegalArgumentException e){

System.out.println("Please define all variables.");

}

break;

case 5:

quit = true;

break;

default:

System.out.println("Enter Correct option.");

break;

}}catch(InputMismatchException e){

System.out.println("Enter Correct option.");

main(args);

}

}

}

}

2. Compress all the source codes (*.java) into a single zip file with the structure: proj1.zip ExpressionTest.java TreePackage/ BinaryNode. java and submit it with the following name: flast-projl zip, where flast is your first initial and last name, lowercase. cp proj1.zip /user/ You should check out your project on theintranet using cp projl.zip /users24102/flast-proj1.zip /cs24102/flast-projl.zip javac ExpressionTest.java java ExpressionTest

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

e. Compute R2 and .

Answered: 1 week ago

Question

Create a financial projection of a flash drive business

Answered: 1 week ago