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); }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

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);
    }

 

 

 

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Map
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education