[In Java] What am I doing wrong with my code on line 15? What I plan to do: crea
ID: 3884266 • Letter: #
Question
[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.
Explanation / 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());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.