
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
expand_more
expand_more
format_list_bulleted
Question
Below is interpreter.java & there's errors in the code, so make sure to fix those errors. Attached is information of what the code must have.
Interpreter.java
import java.util.*;
public class Interpreter {
privateHashMap<String, Object> variables;
publicvoidinterpretFunction(FunctionNode fn){
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);
}
publicvoidinterpretBlock(List<StatementNode> statementNodes,HashMap<String, Object> variables){
for(StatementNode statement : statementNodes){
if(statement instanceofIfNode){
interpretIfNode((IfNode) statement, variables);
}elseif(statement instanceofVariableReferenceNode){
interpretVariableReferenceNode((VariableReferenceNode) statement, variables);
}elseif(statement instanceofMathOpNode){
interpretMathOpNode((MathOpNode) statement, variables);
}elseif(statement instanceofBooleanCompareNode){
interpretBooleanCompareNode((BooleanCompareNode) statement, variables);
}elseif(statement instanceofForNode){
interpretForNode((ForNode) statement, variables);
}elseif(statement instanceofRepeatNode){
interpretRepeatNode((RepeatNode) statement, variables);
}elseif(statement instanceofConstantNode){
interpretConstantNode((ConstantNode) statement, variables);
}elseif(statement instanceofWhileNode){
interpretWhileNode((WhileNode) statement, variables);
}elseif(statement instanceofAssignmentNode){
interpretAssignmentNode((AssignmentNode) statement, variables);
}elseif(statement instanceofFunctionCallNode){
interpretFunctionCallNode((FunctionCallNode) statement, variables);
}
}
}
publicvoidinterpretIfNode(IfNode ifNode,HashMap<String, Object> variables){
Object result =expression(ifNode.getBooleanCompare(), variables);
if(result !=null&&(result instanceofBoolean&&(Boolean) result)){
interpretBlock(ifNode.getStatements(), variables);
}elseif(ifNode.getNextIfNode()!=null){
interpretIfNode(ifNode.getNextIfNode(), variables);
}
}
publicObjectinterpretVariableReferenceNode(VariableReferenceNode varRefNode,HashMap<String, Object> variables){
String name = varRefNode.getName();
if(variables.containsKey(name)){
return variables.get(name);
}else{
thrownewRuntimeException("Variable '"+ name +"' does not exist.");
}
}
publicObjectinterpretMathOpNode(MathOpNode mathOpNode,HashMap<String, Object> variables){
Object leftNode =expression(mathOpNode.getLeft(), variables);
Object rightNode =expression(mathOpNode.getRight(), variables);
if(!(leftNode instanceofDouble)||!(rightNode instanceofDouble)){
thrownewRuntimeException("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:
thrownewRuntimeException();
}
}
}

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

This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
This is a popular solution
Trending nowThis is a popular solution!
Step by stepSolved in 3 steps

Follow-up Questions
Read through expert solutions to related follow-up questions below.
Follow-up Question
I ran the code and there are lots of errors in the code. Attached are images of the errors. Make sure to fix those errors and there must be no error in the code at all.

Transcribed Image Text:27
43
5 44 45 46 48 49 5
45
47
if(statement instanceof IfNode) {
interpretIfNode ((IfNode) statement, variables);
} else if (statement instanceof VariableReferenceNode) {
interpretVariable ReferenceNode ((VariableReferenceNode) statement, variables);
} else if (statement instanceof MathOpNode) {
56
57
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);
}
50
51 // It looks like there are some syntax errors in the provided Java code. Here is the corrected code with annot
52
530 public void interpretIfNode (IfNode ifNode, HashMap<String, Object> variables) {
54
Object result = expression (ifNode.getBooleanCompare(), variables);
if (result != null && (result instanceof Boolean && (Boolean) result)) {
interpretBlock (ifNode.getStatements(), variables);
} else if (ifNode.getNextIfNode() != null) {

Transcribed Image Text:71 public Object interpretMathOpNode (MathOpNode mathOpNode, HashMap<String, Object> variables) {
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
Object leftNode = expression (mathOpNode.getLeft(), variables);
Object rightNode = expression (mathOpNode.getRight(), variables);
if (!(leftNode instanceof Double) || !(rightNode instanceof Double)) {
11
throw new RuntimeException("Cannot perform arithmetic operation on + leftNode + " and " + rightNode)
}
Double left = (Double) leftNode;
Double right = (Double) rightNode;
Object result = null;
switch (mathOpNode.get0p()) {
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");
Solution
by Bartleby Expert
Follow-up Questions
Read through expert solutions to related follow-up questions below.
Follow-up Question
I ran the code and there are lots of errors in the code. Attached are images of the errors. Make sure to fix those errors and there must be no error in the code at all.

Transcribed Image Text:27
43
5 44 45 46 48 49 5
45
47
if(statement instanceof IfNode) {
interpretIfNode ((IfNode) statement, variables);
} else if (statement instanceof VariableReferenceNode) {
interpretVariable ReferenceNode ((VariableReferenceNode) statement, variables);
} else if (statement instanceof MathOpNode) {
56
57
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);
}
50
51 // It looks like there are some syntax errors in the provided Java code. Here is the corrected code with annot
52
530 public void interpretIfNode (IfNode ifNode, HashMap<String, Object> variables) {
54
Object result = expression (ifNode.getBooleanCompare(), variables);
if (result != null && (result instanceof Boolean && (Boolean) result)) {
interpretBlock (ifNode.getStatements(), variables);
} else if (ifNode.getNextIfNode() != null) {

Transcribed Image Text:71 public Object interpretMathOpNode (MathOpNode mathOpNode, HashMap<String, Object> variables) {
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
Object leftNode = expression (mathOpNode.getLeft(), variables);
Object rightNode = expression (mathOpNode.getRight(), variables);
if (!(leftNode instanceof Double) || !(rightNode instanceof Double)) {
11
throw new RuntimeException("Cannot perform arithmetic operation on + leftNode + " and " + rightNode)
}
Double left = (Double) leftNode;
Double right = (Double) rightNode;
Object result = null;
switch (mathOpNode.get0p()) {
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");
Solution
by Bartleby Expert
Knowledge Booster
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
- I need help solving this in JAVAarrow_forwardPlease enter the following code into a Java file. import java.util.*; public class SetDemo { int count[] public static void main(String args[]) { {34, 22,10,60,30,22}; Set unsortedSet = new HashSet(); try { for(int i = 0; i (unsortedSet); System.out.println("The sorted set is: System.out.println("The System.out.println("The catch(Exception e) {} + sorted Set); first element of the set is: "+ (Integer) sorted Set.first()); last element of the set is: "+ (Integer)sortedSet.last()); Run the program to confirm it works as expected. Then, expand the program to do the following steps in order. For each test, display the result of the test to the console.arrow_forwardUSING THE FOLLOWING METHOD SWAP CODE: import java.util.*; class ListIndexOutOfBoundsException extends IndexOutOfBoundsException { public ListIndexOutOfBoundsException(String s) { super(s); } } class ListUtil { public static int maxValue(List<Integer> aList) { if(aList == null || aList.size() == 0) { throw new IllegalArgumentException("List cannot be null or empty"); } int max = aList.get(0); for(int i = 1; i < aList.size(); i++) { if(aList.get(i) > max) { max = aList.get(i); } } return max; } public static void swap(List<Integer> aList, int i, int j) throws ListIndexOutOfBoundsException { if(aList == null) { throw new IllegalArgumentException("List cannot be null"); } if(i < 0 || i >= aList.size() || j < 0 || j >= aList.size()) { throw new ListIndexOutOfBoundsException("Index out…arrow_forward
- BuiltInFunctionDefinitionNode.java has an error so make sure to fix it. BuiltInFunctionDefinitionNode.java import java.util.HashMap; import java.util.function.Function; public class BuiltInFunctionDefinitionNode extends FunctionDefinitionNode { private Function<HashMap<String, InterpreterDataType>, String> execute; private boolean isVariadic; public BuiltInFunctionDefinitionNode(Function<HashMap<String, InterpreterDataType>, String> execute, boolean isVariadic) { this.execute = execute; this.isVariadic = isVariadic; } public String execute(HashMap<String, InterpreterDataType> parameters) { return this.execute.apply(parameters); } }arrow_forwardI ran the code and got an error. Please fix the error and provide me the correct code for all parts. Make sure to give the screenshot of the output as well.arrow_forwardC. package Final; import java.util.HashSet; public class LLCycle_FE { public static void main(String[] args) { Node head = buildLL(); // Given the above linked list write the 2 methods below (removeDuplicates and showLL) System.out.printf("\n --------- "); // This method will remove any duplicate LL nodes (that is, with the same color) head = removeDuplicates( head ); showLL( head ); } **public static Node removeDuplicates(Node head) { return head; } ** private static void showLL(Node head) { // ToDo: Output the entire linked list } private static Node buildLL() { // Use this code to create your LL Node head = new Node("Red", null); Node n2 = new Node("Blue", null); head.next = n2; Node n3 = new Node("Green", null); n2.next = n3; Node n4 = new Node("Yellow", null); n3.next = n4; Node n5 = new…arrow_forward
- Please write in JAVA, Im trying to get a program that Returns first index where given value is found in list (if exist). param value - value to be searched. return first index of the value or -1 if value does not exist public boolean set(int index, int value) throws IndexOutOfBoundsException { //Implement this method return false; }arrow_forwardJava Code: Below is Parser.java and there are errors. getType() and getStart() is undefined for the type Optional<Token> and there is an error in addNode(). Make sure to get rid of all the errors in the code. Attached is images of the errors. Parser.java import java.text.ParseException;import java.util.LinkedList;import java.util.List;import java.util.Optional; import javax.swing.ActionMap; public class Parser { private TokenHandler tokenHandler; private LinkedList<Token> tokens; public Parser(LinkedList<Token> tokens) { this.tokenHandler = new TokenHandler(tokens); this.tokens = tokens; } public boolean AcceptSeparators() { boolean foundSeparator = false; while (tokenHandler.MoreTokens()) { Optional<Token> currentToken = tokenHandler.getCurrentToken(); if (currentToken.getType() == Token.TokenType.NEWLINE || currentToken.getType() == Token.TokenType.SEMICOLON) {…arrow_forwardJAVA PROGRAM MODIFY THIS PROGRAM SO IT READS THE TEXT FILES IN HYPERGRADE. I HAVE PROVIDED THE INPUTS AND THE FAILED TEST CASE AS A SCREENSHOT. HERE IS THE WORKING CODE TO MODIFY: import java.io.*;import java.util.*;public class NameSearcher { private static List<String> loadFileToList(String filename) throws FileNotFoundException { List<String> namesList = new ArrayList<>(); File file = new File(filename); if (!file.exists()) { throw new FileNotFoundException(filename); } try (Scanner scanner = new Scanner(file)) { while (scanner.hasNextLine()) { String line = scanner.nextLine().trim(); String[] names = line.split("\\s+"); for (String name : names) { namesList.add(name.toLowerCase()); } } } return namesList; } private static Integer searchNameInList(String name, List<String> namesList) {…arrow_forward
- import java.util.Comparator; import java.util.PriorityQueue; class StringLengthComparator implements Comparator public int compare(String o1, String 02) if (01.length()02.length()){ return 1; }else{ } } } } class StringLengthComparator1 implements Comparator { return 0; public int compare(String o1, String o2) { if(01.length()02.length()){ return -1;//means here we swap it }else{ } } return 0; public class StringLength { public static void main(String[] args) { String arr[] = {"this", "at", "a", "their", "queues"}; StringLengthComparator string Comparator = new StringLengthComparator(); StringLengthComparator1 string Comparator1 = new StringLengthComparator1(); PriorityQueue pq = new PriorityQueue(stringComparator); PriorityQueue pq1 = new PriorityQueue(stringComparator1); for(int i=0;iarrow_forwardI need help with this Java problem to output as it's explained in the image below: public class BST { private Node root; public void insert(int data) { root = insertRec(root, data); } private Node insertRec(Node root, int data) { if (root == null) { root = new Node(data); return root; } if (data < root.data) { root.left = insertRec(root.left, data); } else if (data > root.data) { root.right = insertRec(root.right, data); } return root; } public int getHeight() { return getHeightRec(root); } private int getHeightRec(Node root) { if (root == null) { return 0; } else { int leftHeight = getHeightRec(root.left); int rightHeight = getHeightRec(root.right); return Math.max(leftHeight, rightHeight) + 1; } } public int search(int data) { return searchRec(root, data, 0); }…arrow_forward
arrow_back_ios
arrow_forward_ios
Recommended textbooks for you
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education

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)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON

Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON

C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON

Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning

Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education