[In Java]
What am I doing wrong with my code on line 15?
What I plan to do:
create a hash table with the String keys “+”, “-“, “/”, “*” . And the values would be the object(of the classes). I have already made
Addition.java, Subtraction.java, Multiplication.java, Division.java.
They all have nothing filled in them.
ρ Evaluatorul.java ρ Operator.Java |や龟る! 헬 훨 | 0 java 2 Operand.java 1.aye’E 8gIls. Addition·java Subtraction.java. Source History |圈犀,屈. |及争于 |些 import java.util.HashMap: /*Hashmap K-keys V type of mapped values 6 public abstract class Operator // The Operator class should contain an instance of a HashMap // This map will use keys as the tokens we’re interested in, // and values will be instances of the Operators. 10 12 13 14 // Example: // Where does this declaration go? What should its access level be? // Class or instance variable? Is this the right declaration? static Ha3hMap opera tor = new Ha3hMap(){ operator.put(, new Addition) 16 17 18 19 20 21 //map.put”-, new Subtraction) public abstract int priority): public abstract Operand execut rand op1, Operand op2) 23 24 25 26 public static boolean check( String token return false;
Expert Answer
Hi Friend, there is error is HashMap creation.
Please find correct way.
import java.util.HashMap;
public class Test {
static HashMap operator = new HashMap();
// static block
static{
operator.put(“+”, new Addition());
}
// instance function
void fun1() {
operator.put(“+”, new Addition());
}
// static function
static void fun2() {
operator.put(“+”, new Addition());
}
// main function
public static void main(String[] args) {
operator.put(“+”, new Addition());
}
}