Code is wrong


#1

Good day,

I really cannot understand what is wrong with my code anymore. Could you please help me?

// This program calculates the calculator

class Calculator {
int parameterA;
int parameterB;
public Calculator (int a, int b) {
a = parameterA;
b = parameterB;
}

public int add() {

return a+b;
}

public int subtract() {
return a-b;
}

public int multiply() {
return a*b;

public int divide() {
if (b==0) {

    System.out.println("Error! Dividing by zero is not allowed");
      
  } else {
    
return a/b; 
  }
 
public int modulo() {
  
  if (b==0) {
    
    System.out.println("Error! Dividing by zero is not allowed");
		return 0;

  } else {

return a%b;       
}

  public static int main(String[] args){

    Calculator.myCalculator = new Calculator ("myCalculator"); 
    System.out.println(myCalculator.add(5, 7));
    System.out.println(myCalculator.subtract(45, 11));
  }
}

}
}
}


#2

I think the way you create your constructor and the way you create an instance by using the constructor in the main method are wrong.

Here are my codes.
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.");
return 0;
}
else{
return a/b;
}
}
public int modulo(int a, int b){
if (b==0){
System.out.println("Error! Dividing by zero is not allowed.");
return 0;
}
else{
return a%b;
}
}
public static void main(String[] args){
Calculator myCalculator=new Calculator();
System.out.println(myCalculator.add(5,7));
System.out.println(myCalculator.subtract(45,11));

}

}


#3

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