I have an exercise to solve, which is to create a simple Java language calculator.
I have to through the Terminal, send the two numbers as arguments to my program. I am using the following build command:
zeluis@zeluis-HP-EliteBook-8460p ~/NetBeansProjects/SOCP1/src/socp1 $ javac MainEX1.java
It just gives me the following error:
EX1.java
MainEX1.java:62: error: cannot find symbol
CalculatorMethodos cM = new CalculatorMethodos();
^
symbol: class CalculatorMethodos
location: class MainEX1
MainEX1.java:62: error: cannot find symbol
CalculatorMethodos cM = new CalculatorMethodos();
^
symbol: class CalculatorMethodos
location: class MainEX1
2 errors
I leave the MAIN class below:
int num1, num2, total = 0, opcCalc, vef = -1;
CalculatorMethodos cM = new CalculatorMethodos();
//read from keyboard
Scanner lerDataKeyBoard = new Scanner(System.in);
// BufferedReader lerDataKeyBoard = new BufferedReader(new InputStreamReader(System.in));
/* System.out.println("First number:\n");
num1 = lerDataKeyBoard.nextInt();
System.out.println("Second number:\n");
num2 = lerDataKeyBoard.nextInt(); */
System.out.println("Introduza a operação:\n");
System.out.println("'1' - SUM\n");
System.out.println("'2' - SUBTRACT\n");
System.out.println("'3' - MULTIPLY\n");
System.out.println("'4' - DIVIDE\n");
opcCalc = lerDataKeyBoard.nextInt();
switch (opcCalc) {
case 1:
total = cM.add(Integer.parseInt(args[0]), Integer.parseInt(args[1]));
break;
case 2:
// total = cM.sub(args[0], args[1]);
break;
case 3:
// total = cM.sub(args[0], args[1]);
break;
case 4:
// total = cM.div(args[0], args[1]);
break;
}
System.out.println("Resultado:" + total);
}
CLASS CALCULATOR:
public class CalculatorMethodos {
private int total;
public int add (int num1, int num2) {
return total = num1 + num2;
}
public int sub(int num1, int num2) {
return total = num1 - num2;
}
public int mult (int num1, int num2) {
return total = num1 * num2;
}
public int div (int num1, int num2) {
return total = num1 / num2;
}
}
SOLUTION The solution to this error was to use the javac command with -cp and "..":
javac -cp .. MainEX1.java
NEW ERROR - when I try to use the command java MainEX1
Error: Could not find or load main class MainEX1