Getting 18 errors and I'm not sure why


#1



https://www.codecademy.com/courses/learn-java/projects/basic-calculator


I've finished the Basic Calculator project but it's not running correctly. These are the errors I'm getting:

Calculator.java:11: error: class, interface, or enum expected
public int add (int a, int b) {
^
Calculator.java:13: error: class, interface, or enum expected
}
^
Calculator.java:15: error: class, interface, or enum expected
public int subtract (int a, int b) {
^
Calculator.java:17: error: class, interface, or enum expected
}
^
Calculator.java:19: error: class, interface, or enum expected
public int multiply (int a, int b) {
^
Calculator.java:21: error: class, interface, or enum expected
}
^
Calculator.java:23: error: class, interface, or enum expected
public int divide (int a, int b) {
^
Calculator.java:26: error: class, interface, or enum expected
return 0;
^
Calculator.java:27: error: class, interface, or enum expected
} else {
^
Calculator.java:29: error: class, interface, or enum expected
}
^
Calculator.java:32: error: class, interface, or enum expected
public int modulo (int a, int b) {
^
Calculator.java:35: error: class, interface, or enum expected
return 0;
^
Calculator.java:36: error: class, interface, or enum expected
} else {
^
Calculator.java:38: error: class, interface, or enum expected
}
^
Calculator.java:41: error: class, interface, or enum expected
public static void main(String [ ] args)
^
Calculator.java:44: error: class, interface, or enum expected
System.out.println(myCalculator.add(5,7));
^
Calculator.java:45: error: class, interface, or enum expected
System.out.println(myCalculator.subtract(45,11));
^
Calculator.java:47: error: class, interface, or enum expected
}
^
18 errors

I assume I'm making the same mistake over and over, but I'm not sure what it is. Any help is greatly appreciated!


//calculator program

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));

}


#2

Start from the top, fix the first thing and then recompile. It's saying that at that location, only a few select things are allowed. What did you put there instead? Where is that allowed in java?


#3

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