private Object expression(Node booleanCompare, HashMap variables2) { // TODO Auto-generated method stub return null; } public Object interpretVariableReferenceNode(VariableReferenceNode varRefNode, HashMap variables) { String name = varRefNode.getName(); if(variables.containsKey(name)) { return variables.get(name); } else { throw new RuntimeException("Variable '" + name + "' does not exist."); } } public Object interpretMathOpNode(MathOpNode mathOpNode, HashMap variables) { Object leftNode = expression(mathOpNode.getLeft(), variables); Object rightNode = expression(mathOpNode.getRight(), variables); if(!(leftNode instanceof Double) || !(rightNode instanceof Double)) { throw new RuntimeException("Cannot add/subtract/multiply/divide/modulo " + leftNode + " and " + rightNode); } Double left = (Double) leftNode; Double right = (Double) rightNode; Object result = null; switch(mathOpNode.getOp()) { case "+": result = left + right; break; case "-": result = left - right; break; case "*": result = left * right; break; case "/": result = left / right; break; case "%": result = left % right; break; default: throw new RuntimeException("Unrecognized arithmetic operation"); } }

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

Java Programming: There are lots of errors in this code. Please help me fix them. Attached is images of what the code must have and circled the errors in the code. 

 

Interpreter.java

 

public class Interpreter {
    private HashMap<String, Object> variables;

    public void interpretFunction(FunctionNode fn) {
        // Create a hash map for local variables
        variables = new HashMap<String, Object>();

        for(Node constantNode : fn.getConstants()) {
            Object constant = constantNode.getValue();
            variables.put(constantNode.getName(), constant);
        }
        for(Node localVarNode : fn.getVariables()) {
            Object localVar = null;
            variables.put(localVarNode.getName(), localVar);
        }
        interpretBlock(fn.getStatements(), variables);
    }

    public void interpretBlock(List<StatementNode> statementNodes, HashMap<String, Object> variables) {
        for(StatementNode statement : statementNodes) {
            if(statement instanceof IfNode) {
                interpretIfNode((IfNode) statement, variables);
            } else if(statement instanceof VariableReferenceNode) {
                interpretVariableReferenceNode((VariableReferenceNode) statement, variables);
            } else if(statement instanceof MathOpNode) {
                interpretMathOpNode((MathOpNode) statement, variables);
            } else if(statement instanceof BooleanCompareNode) {
                interpretBooleanCompareNode((BooleanCompareNode) statement, variables);
            } else if(statement instanceof ForNode) {
                interpretForNode((ForNode) statement, variables);
            } else if(statement instanceof RepeatNode) {
                interpretRepeatNode((RepeatNode) statement, variables);
            } else if(statement instanceof ConstantNode) {
                interpretConstantNode((ConstantNode) statement, variables);
            } else if(statement instanceof WhileNode) {
                interpretWhileNode((WhileNode) statement, variables);
            } else if(statement instanceof AssignmentNode) {
                interpretAssignmentNode((AssignmentNode) statement, variables);
            } else if(statement instanceof FunctionCallNode) {
                interpretFunctionCallNode((FunctionCallNode) statement, variables);
            }
        }
    }

    private void interpretFunctionCallNode(FunctionCallNode statement, HashMap<String, Object> variables2) {
        // TODO Auto-generated method stub
        
    }

    private void interpretAssignmentNode(AssignmentNode statement, HashMap<String, Object> variables2) {
        
        
    }

    private void interpretWhileNode(WhileNode statement, HashMap<String, Object> variables2) {
 
        
    }

    private void interpretConstantNode(ConstantNode statement, HashMap<String, Object> variables2) {
 
        
    }

    public void interpretIfNode(IfNode ifNode, HashMap<String, Object> variables) {
        Object result = expression(ifNode.getBooleanCompare(), variables);
        if(result != null && (result instanceof Boolean && (Boolean) result)) {
            interpretBlock(ifNode.getStatements(), variables);
        } else if(ifNode.getNextIfNode() != null) {
            interpretIfNode(ifNode.getNextIfNode(), variables);
        }
    }

    private Object expression(Node booleanCompare, HashMap<String, Object> variables2) {
        // TODO Auto-generated method stub
        return null;
    }

    public Object interpretVariableReferenceNode(VariableReferenceNode varRefNode, HashMap<String, Object> variables) {
        String name = varRefNode.getName();
        if(variables.containsKey(name)) {
            return variables.get(name);
        } else {
            throw new RuntimeException("Variable '" + name + "' does not exist.");
        }
    }

    public Object interpretMathOpNode(MathOpNode mathOpNode, HashMap<String, Object> variables) {
        Object leftNode = expression(mathOpNode.getLeft(), variables);
        Object rightNode = expression(mathOpNode.getRight(), variables);
        if(!(leftNode instanceof Double) || !(rightNode instanceof Double)) {
            throw new RuntimeException("Cannot add/subtract/multiply/divide/modulo " + leftNode + " and " + rightNode);
        }
        Double left = (Double) leftNode;
        Double right = (Double) rightNode;
        Object result = null;
        switch(mathOpNode.getOp()) {
            case "+":
                result = left + right;
                break;
            case "-":
                result = left - right;
                break;
            case "*":
                result = left * right;
                break;
            case "/":
                result = left / right;
                break;
            case "%":
                result = left % right;
                break;
            default:
                throw new RuntimeException("Unrecognized arithmetic operation");

}
    }
}

320
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
580
59
60
61
public void interpretBlock(List<StatementNode>
for statementNode statement statement Modes) {
if(statement instanceof IfNode) {
}
statementNodes, HashMap<String, Object> variables) {
}
}
interpretIfNode ((IfNode) statement, variables);
} else if (statement instanceof VariableReferenceNode) {
interpretVariableReferenceNode ((VariableReferenceNode) statement, variables);
} else if (statement instanceof MathOpNode) {
interpretMathOpNode ((MathOpNode) statement, variables);
} else if (statement instanceof BooleanCompareNode) {
interpretBooleanCompareNode((BooleanCompareNode) statement, variables);
} else if (statement instanceof ForNode) {
interpretForNode((ForNode) statement, variables);
} else if (statement instanceof RepeatNode) {
interpretRepeatNode ((RepeatNode) statement, variables);
} else if(statement instanceof ConstantNode) {
interpretConstantNode((ConstantNode) statement, variables);
} else if (statement instanceof WhileNode) {
interpretWhileNode ((WhileNode) statement, variables);
} else if (statement instanceof AssignmentNode) {
interpretAssignmentNode ((AssignmentNode) statement, variables);
} else if (statement instanceof FunctionCallNode) {
interpretFunctionCallNode((FunctionCallNode) statement, variables);
}
private void interpretFunction CallNode (FunctionCallMode statement. HashMapstring, Object> variables2) {
// TODO Auto-generated method stub
Transcribed Image Text:320 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 580 59 60 61 public void interpretBlock(List<StatementNode> for statementNode statement statement Modes) { if(statement instanceof IfNode) { } statementNodes, HashMap<String, Object> variables) { } } interpretIfNode ((IfNode) statement, variables); } else if (statement instanceof VariableReferenceNode) { interpretVariableReferenceNode ((VariableReferenceNode) statement, variables); } else if (statement instanceof MathOpNode) { interpretMathOpNode ((MathOpNode) statement, variables); } else if (statement instanceof BooleanCompareNode) { interpretBooleanCompareNode((BooleanCompareNode) statement, variables); } else if (statement instanceof ForNode) { interpretForNode((ForNode) statement, variables); } else if (statement instanceof RepeatNode) { interpretRepeatNode ((RepeatNode) statement, variables); } else if(statement instanceof ConstantNode) { interpretConstantNode((ConstantNode) statement, variables); } else if (statement instanceof WhileNode) { interpretWhileNode ((WhileNode) statement, variables); } else if (statement instanceof AssignmentNode) { interpretAssignmentNode ((AssignmentNode) statement, variables); } else if (statement instanceof FunctionCallNode) { interpretFunctionCallNode((FunctionCallNode) statement, variables); } private void interpretFunction CallNode (FunctionCallMode statement. HashMapstring, Object> variables2) { // TODO Auto-generated method stub
Rubric
Comments
Variable/Function
naming
interpretFunction
interpretBlock
expression
booleanCompare
variableReferenceNode
mathOpNode
ifNode
forNode
repeatNode
constantNodes
whileNode
assignmentNode
Poor
None/Excessive
(0)
Single letters
everywhere (0)
Not handled (0)
Not handled (0)
Not handled (0)
Not handled (0)
Not handled (0)
Not handled(0)
Not handled(0)
Not handled (0)
Not handled (0)
Not handled (0)
Not handled (0)
Not handled (0)
OK
"What" not
"Why", few (5)
Lots of
abbreviations (5)
Good
Some "what" comments
or missing some (7)
Full words most of the
time (8)
Great
Anything not obvious has reasoning
(10)
Full words, descriptive (10)
Creates variables and calls
interpretBlock (10)
Loops over the statement nodes and
calls methods for each (5)
Handles mathOpNode,
constantNodes, variable references
(10)
Calls expression(), then compares (5)
Looks up nodes and returns IDT (5)
Calls expression (), then calculates (5)
Calls booleanCompare and chains (10)
Loops over the range and calls
interpretBlock(10)
Calls booleanCompare and
interpretBlock correctly (5)
Returns a new IDT (5)
Calls booleanCompare and
interpretBlock correctly (5)
calls expression() and replaces the IDT
entry for the variable(5)
Transcribed Image Text:Rubric Comments Variable/Function naming interpretFunction interpretBlock expression booleanCompare variableReferenceNode mathOpNode ifNode forNode repeatNode constantNodes whileNode assignmentNode Poor None/Excessive (0) Single letters everywhere (0) Not handled (0) Not handled (0) Not handled (0) Not handled (0) Not handled (0) Not handled(0) Not handled(0) Not handled (0) Not handled (0) Not handled (0) Not handled (0) Not handled (0) OK "What" not "Why", few (5) Lots of abbreviations (5) Good Some "what" comments or missing some (7) Full words most of the time (8) Great Anything not obvious has reasoning (10) Full words, descriptive (10) Creates variables and calls interpretBlock (10) Loops over the statement nodes and calls methods for each (5) Handles mathOpNode, constantNodes, variable references (10) Calls expression(), then compares (5) Looks up nodes and returns IDT (5) Calls expression (), then calculates (5) Calls booleanCompare and chains (10) Loops over the range and calls interpretBlock(10) Calls booleanCompare and interpretBlock correctly (5) Returns a new IDT (5) Calls booleanCompare and interpretBlock correctly (5) calls expression() and replaces the IDT entry for the variable(5)
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps

Blurred answer
Follow-up Questions
Read through expert solutions to related follow-up questions below.
Follow-up Question

This is NOT a complete code. Give me the complete code!!!!!!!!

Solution
Bartleby Expert
SEE SOLUTION
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY