Question
I need help doing a simple part of a project. I need help implementing a menu for a user to input an expression into my
I need help doing a simple part of a project. I need help implementing a menu for a user to input an expression into my expression tree program. The user should be able to enter an expression in the form of a string seperated by spaces, and that string input should then be transformed into a string array with every part seperated by spaces being an element in the expression. Here is the source code: (further clarifications will follow it). I only want changes made to ExpressionTest, the program is funtional I just don't know how to implement this menu.
ExpressionTree.java
//
import java.util.HashMap; import java.util.Iterator; import java.util.Stack;
public class ExpressionTree extends BinaryTree
ExpressionTreeInterface.java
public interface ExpressionTreeInterface
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
public BinaryNode
public void setRightChild(BinaryNode
BinaryTree.java
import java.util.Iterator; import java.util.Stack;
public class BinaryTree
private BinaryNode
public BinaryTree() { root = null; }
public BinaryTree(T rootData) { root = new BinaryNode
public BinaryTree(T rootData, BinaryTree
public void setTree(T rootData) { root = new BinaryNode
public void setTree(T rootData, BinaryTree
private void privateSetTree(T rootData, BinaryTree
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
public Iterator
public Iterator
public Iterator
public Iterator
private class PostorderIterator implements Iterator
private Stack
public PostorderIterator() { nodeStack = new Stack<>(); current = root; populateStack(current); } private void populateStack(BinaryNode
public boolean hasNext() { return !nodeStack.isEmpty(); }
public T next() { return nodeStack.pop().getData(); }
}
}
BinaryTreeInterface.java
public interface BinaryTreeInterface
TreeIteratorInterface.java
import java.util.Iterator;
public interface TreeIteratorInterface
TreeInterface.java
public interface TreeInterface
ExpressionTest.java
package TreePackage;
import java.util.Scanner;
import java.util.StringTokenizer;
public class ExpressionTest {
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
boolean quit = false;
ExpressionTree exp = new ExpressionTree(new String[]{"a", "b", "2", "/","+"});
while(!quit){
System.out.println("J. Botkin's Expression Tree "
+ "1. Enter New Expression "
+ "2. Set Expression Variables "
+ "3. Evaluate Expression "
+ "4. Display Postfix "
+ "5. Quit");
int option = kb.nextInt();
switch (option) {
case 1:
System.out.println("Enter Expression seperated by spaces.");
String userInput = kb.nextLine();
String[] userExp = userInput.split(" ");
StringTokenizer toToken = new StringTokenizer(userInput, " ");
exp = new ExpressionTree(new String[]{userExp[0], userExp[1], userExp[2], userExp[3],userExp[4]});
break;
case 2:
System.out.print("Variable name: ");
String name = kb.nextLine();
System.out.print(" Variable value: ");
double value = kb.nextDouble();
exp.setVariable(name, value);
break;
case 3:
System.out.println(exp.evaluate());
break;
case 4:
exp.displayPostfix();
break;
case 5:
quit = true;
break;
default:
System.out.println("Enter Correct option.");
break;
}
/* ExpressionTree exp = new ExpressionTree(new String[]{"a", "b", "2", "/","+"});
exp.setVariable("z", 1.5);
exp.setVariable("b", 3);
exp.setVariable("a", 2);
System.out.println(exp.evaluate());
exp.displayPostfix(); */
}
}
}
The commented out section at the end of ExpressionTest displays how these methods funtion, I just need this menu to work and the user to be able to enter an expression.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started