My basic Calculator program does not return any numbers. i have no error messages. its blank


#1

public class Calculator {

public Calculator() {

}
public int add(int a, int b) {
return a + b;
}
public int subtract(int a, int b) {
return a - b;
}
public int multiply(int a, int b) {
return a * b;
}
public int divide(int a, int b) {
if (b == 0) {
System.out.println(" Error! Dividing by zero is not allowed");
}
else {
return a / b;
}
return 0;
}

public int modulo(int a, int b) {
if (b == 0) {
System.out.println("Error! Dividing by zero is not allowed.");
}
else {
return a % b;
}
return 0;
}
public static void main (String[] args) {
Calculator myCalculator = new Calculator();

myCalculator.add(5, 7);

myCalculator.subtract(45, 11);
}
}


#2

Return? Do you mean print?
Can you motivate why it would print something? What chain of events in that code would lead to printing?


#4

Hmm
So i followed the instructions and it says it should add the numbers and subtract the number in the main method. The result should show in the right side window but nothing happens. I have no idea what i did wrong.

Thank You!


#5

I tried this to print:
System.out.println(myCalculator.add);
But I get an error message.
Maybe i dont know how to call a method and print?


#6

That is how you would print but that isn't how you call a method.
Nothing will print unless you tell it to.
So obtain the value and then print the value


#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.