Answer to Question #240496 in Java | JSP | JSF for Zia

Question #240496

Kindly answer this as soon as possible. Very urgent. And need a correct answer.


The program does 2 + 2; I need the program do 3 * 2; and 3 / 2; and 3^2;


The code is already written. You need to fix the code, so it accepts *, / and ^ operations need to add multiplication, addition, and power.


You can access the code via the link given below. Thanks


https://drive.google.com/file/d/1NoVc1HNT9kPNJhnFg0x2V44tak9C0x4c/view?usp=sharing

1
Expert's answer
2021-09-22T06:28:40-0400
class Node {
    private final String instance;

    private Node leftNode;
    private Node rightNode;

    private final int hashCode; // (x << 6) - x == (2^6 - 1) * x == 63 * x

    // full node
    public Node(Operation operation, Node leftNode, Node rightNode) {
        instance = operation.toString();
        this.leftNode = leftNode;
        this.rightNode = rightNode;

        int hash = instance.hashCode();
            hash = (hash << 6) - hash + leftNode.hashCode();
            hash = (hash << 6) - hash + rightNode.hashCode();
        this.hashCode = hash;
    }
    // half node
    public Node (Operation operation, Node leftNode) {
        instance = operation.toString();
        this.leftNode = leftNode;

        int hash = instance.hashCode();
            hash = (hash << 6) - hash + leftNode.hashCode();
        this.hashCode = hash;
    }
    // leaf
    public Node(String variable) {
        instance = variable;
        this.hashCode = instance.hashCode();
    }

    public String getInstance() {
        return this.instance;
    }

    public Node leftNode() {
        return this.leftNode;
    }
    public Node rightNode() {
        return this.rightNode;
    }

    @Override
    public int hashCode() {
        return this.hashCode;
    }

    public boolean equals(Node node) {
        return this.hashCode() == node.hashCode();
    }

    @Override
    public String toString() {
        if (rightNode != null) // returns: (A & B) or (A | B) or (A -> B)
            return "(" +
                    leftNode.toString() + // A
                    " " +
                    instance + // '&' or '|' or '->'
                    " " +
                    rightNode + // B
                    ")";

        else if (leftNode != null) // returns: !A
            return instance + leftNode.toString();

        else return instance; // returns: A
    }
}

class Parser {
    private int pointer;

    public Node parse(String expr) {
        this.pointer = 0;
        return implication(expr);
    }

    private static final String OPEN_BRACKET = "(";
    private static final String CLOSE_BRACKET = ")";

    // IMPL
    private Node implication(String expr) {
        Node node = disjunction(expr);

        if (nextSignIs(expr, Operation.IMPLICATION.toString())) {
            node = new Node(Operation.IMPLICATION, node, implication(expr));
        }
        return node;
    }

    // OR
    private Node disjunction(String expr) {
        Node node = conjunction(expr);

        while (nextSignIs(expr, Operation.DISJUNCTION.toString())) {
            node = new Node(Operation.DISJUNCTION, node, conjunction(expr));
        }
        return node;
    }

    // AND
    private Node conjunction(String expr) {
        Node node = negation(expr);

        while (nextSignIs(expr, Operation.CONJUNCTION.toString())) {
            node = new Node(Operation.CONJUNCTION, node, negation(expr));
        }
        return node;
    }

    // NOT
    private Node negation(String expr) {
        if (nextSignIs(expr, Operation.NEGATION.toString())) {
            return new Node(Operation.NEGATION, negation(expr));
        }

        if (nextSignIs(expr, OPEN_BRACKET)) {
            Node node = implication(expr);
            nextSignIs(expr, CLOSE_BRACKET);
            return node;
        }

        char character = expr.charAt(pointer);

        StringBuilder variable = new StringBuilder();
        while (pointer < expr.length() - 1 && isAllowedChar(character)) {
            variable.append(character);
            character = expr.charAt(++pointer);
        }
        if (pointer == expr.length() - 1 && isAllowedChar(character)) variable.append(character);
        return new Node(variable.toString());
    }

    // allowed only capital latin letters[A-Z], numbers[0-9] and apostrophe[\']
    private boolean isAllowedChar(char character) {
        return 64 < character && character < 91 || 47 < character && character < 58 || character == 39;
    }

    private boolean nextSignIs(String expr, String sign) {
        skipAllWhiteSpaces(expr);
        boolean result = expr.startsWith(sign, pointer);
        skipAllWhiteSpaces(expr);

        if (result) this.pointer += sign.length();
        return result;
    }

    // skips all whitespaces
    public void skipAllWhiteSpaces(String expr) {
        while (pointer < expr.length() && isAllowedWhite(expr.charAt(pointer))) {
            this.pointer ++;
        }
    }
    // allowed whitespaces[\\s+]
    private boolean isAllowedWhite(char character) {
        return character == ' ' || character == '\t' || character == '\r';
    }
}

enum Operation {

    IMPLICATION("->"),
    DISJUNCTION("|"),
    CONJUNCTION("&"),
    NEGATION("!");

    private final String sign;
    Operation(String sign) {
        this.sign = sign;
    }

    @Override
    public String toString() {
        return this.sign;
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS