implement JavaFX front-end for the Desktop Calculator import java.util.HashMap; import java.util.Map; class Token { private String value; public Token(String value) { this.value = value; } public String getValue() { return value; } } class TokenStream { private Token[] tokens; private int currentPosition; public TokenStream(Token[] tokens) { this.tokens = tokens; this.currentPosition = 0; } public Token getCurrentToken() { if (currentPosition < tokens.length) { return tokens[currentPosition]; } return null; } public void consume() { currentPosition++; } public boolean isIdentifier() { Token currentToken = getCurrentToken(); return currentToken != null && currentToken.getValue().matches("[a-zA-Z]+"); } } class AstNode { private static Map variables = new HashMap<>(); private String type; private AstNode leftNode; private AstNode rightNode; private double value; public AstNode(String type, AstNode leftNode, AstNode rightNode) { this.type = type; this.leftNode = leftNode; this.rightNode = rightNode; } public AstNode(double value) { this.type = "number"; this.value = value; } public double evaluate() { if (type.equals("number")) { return value; } else if (type.equals("=")) { String variableName = leftNode.getValue(); double variableValue = rightNode.evaluate(); variables.put(variableName, (int) variableValue); // Assuming variables are integers return variableValue; } else if (type.equals("+")) { return leftNode.evaluate() + rightNode.evaluate(); } else if (type.equals("-")) { return leftNode.evaluate() - rightNode.evaluate(); } else if (type.equals("*")) { return leftNode.evaluate() * rightNode.evaluate(); } else if (type.equals("/")) { double divisor = rightNode.evaluate(); if (divisor == 0) { throw new ArithmeticException("Division by zero"); } return leftNode.evaluate() / divisor; } else if (type.equals("%")) { return leftNode.evaluate() % rightNode.evaluate(); } else if (type.equals("identifier")) { String variableName = leftNode.getValue(); if (variables.containsKey(variableName)) { return variables.get(variableName); } else { throw new RuntimeException("Variable not defined: " + variableName); } } else { throw new RuntimeException("Unknown node type: " + type); } } public String getValue() { if (type.equals("identifier") || type.equals("number")) { return String.valueOf(value); } else { throw new RuntimeException("Cannot get value for non-leaf node"); } } } public class DesktopCalcA2 { public static void main(String[] args) { // Example expression: x = 5 + 3 * 2 Token[] tokens = {new Token("x"), new Token("="), new Token("5"), new Token("+"), new Token("3"), new Token("*"), new Token("2")}; TokenStream tokenStream = new TokenStream(tokens); AstNode root = new AstNode("=", new AstNode("x"), new AstNode("+", new AstNode("5"), new AstNode("*", new AstNode("3"), new AstNode("2")))); double result = root.evaluate(); System.out.println("Result: " + result); }
implement JavaFX front-end for the Desktop Calculator
import java.util.HashMap;
import java.util.Map;
class Token {
private String value;
public Token(String value) {
this.value = value;
}
public String getValue() {
return value;
}
}
class TokenStream {
private Token[] tokens;
private int currentPosition;
public TokenStream(Token[] tokens) {
this.tokens = tokens;
this.currentPosition = 0;
}
public Token getCurrentToken() {
if (currentPosition < tokens.length) {
return tokens[currentPosition];
}
return null;
}
public void consume() {
currentPosition++;
}
public boolean isIdentifier() {
Token currentToken = getCurrentToken();
return currentToken != null && currentToken.getValue().matches("[a-zA-Z]+");
}
}
class AstNode {
private static Map<String, Integer> variables = new HashMap<>();
private String type;
private AstNode leftNode;
private AstNode rightNode;
private double value;
public AstNode(String type, AstNode leftNode, AstNode rightNode) {
this.type = type;
this.leftNode = leftNode;
this.rightNode = rightNode;
}
public AstNode(double value) {
this.type = "number";
this.value = value;
}
public double evaluate() {
if (type.equals("number")) {
return value;
} else if (type.equals("=")) {
String variableName = leftNode.getValue();
double variableValue = rightNode.evaluate();
variables.put(variableName, (int) variableValue); // Assuming variables are integers
return variableValue;
} else if (type.equals("+")) {
return leftNode.evaluate() + rightNode.evaluate();
} else if (type.equals("-")) {
return leftNode.evaluate() - rightNode.evaluate();
} else if (type.equals("*")) {
return leftNode.evaluate() * rightNode.evaluate();
} else if (type.equals("/")) {
double divisor = rightNode.evaluate();
if (divisor == 0) {
throw new ArithmeticException("Division by zero");
}
return leftNode.evaluate() / divisor;
} else if (type.equals("%")) {
return leftNode.evaluate() % rightNode.evaluate();
} else if (type.equals("identifier")) {
String variableName = leftNode.getValue();
if (variables.containsKey(variableName)) {
return variables.get(variableName);
} else {
throw new RuntimeException("Variable not defined: " + variableName);
}
} else {
throw new RuntimeException("Unknown node type: " + type);
}
}
public String getValue() {
if (type.equals("identifier") || type.equals("number")) {
return String.valueOf(value);
} else {
throw new RuntimeException("Cannot get value for non-leaf node");
}
}
}
public class DesktopCalcA2 {
public static void main(String[] args) {
// Example expression: x = 5 + 3 * 2
Token[] tokens = {new Token("x"), new Token("="), new Token("5"),
new Token("+"), new Token("3"), new Token("*"), new Token("2")};
TokenStream tokenStream = new TokenStream(tokens);
AstNode root = new AstNode("=", new AstNode("x"), new AstNode("+", new AstNode("5"), new AstNode("*", new AstNode("3"), new AstNode("2"))));
double result = root.evaluate();
System.out.println("Result: " + result);
}
Trending now
This is a popular solution!
Step by step
Solved in 3 steps