Help! Cannot complete calculator project


#1


A basic calculator - Java. What am I doing wrong?


Calculator.java:32: error: '.class' expected
myCalculator.add(int aNum, int bNum);
^
Calculator.java:32: error: ';' expected
myCalculator.add(int aNum, int bNum);
^
Calculator.java:32: error: ';' expected
myCalculator.add(int aNum, int bNum);
^
Calculator.java:34: error: '.class' expected
myCalculator.subtract(int aNum, int bNum);
^
Calculator.java:34: error: ';' expected
myCalculator.subtract(int aNum, int bNum);
^
Calculator.java:34: error: ';' expected
myCalculator.subtract(int aNum, int bNum);
^
6 errors


class Calculator {
  public Calculator(){
  }
   public void add(int aNum, int bNum){
 return aNum + bNum;
  }
   public void subtract(int aNum, int bNum){
 return aNum - bNum;
}
   public void multiply(int aNum, int bNum){
 return aNum * bNum;
} 
  public void divide(int aNum, int bNum){
    if(bNum==0) {
      system.out.println("Error! Dividing by zero is not allowed.");
        return 0;
   } else {
 return aNum / bNum;
      }
    } 
   public void modulo(int aNum, int bNum){
     if(bNum==0){
       system.out.prinln("Error! Dividing by zero is not allowed.");
      return 0;
   } else {
 return aNum % bNum;
     }
} 
 
  public static void main(String[] args) {
    Calculator myCalculator = new Calculator();
      myCalculator.add(int aNum, int bNum);
    System.out.println(myCalculator.add(5, 7));
      myCalculator.subtract(int aNum, int bNum);
    System.out.println(myCalculator.subtract(45, 11));
    
  }
}


#2

If you're unable to isolate the problems when there's this much code, then do it with less code.

Start with just a class and a main method, compile it and run.
Add one very small part. Test it.
If it doesn't work, reconsider what you just did, go back to testing.
If it does behave as you expect, add the next thing.

Essentially you've got a knowledge/experience debt and the solution to that is to only handle very small changes at a time.

create a class with a main method..
print out hello world..
create another method, empty..
call the empty method..
add a parameter to that method..
adjust the calling of the method to supply an argument..
add another parameter, adjust the call again..

and so on - compile and run each time you do something like that. And in particular, observe how the compiler reacts to your errors so that you can identify them later as well, when there is more code.


#3

All right, thanks, I will try out that approach.


#5

Let me just thank you again for this answer - I've just completed the exercise, but even more important, you gave me a method that I can use in future exercises. It kinda fits the expression:

I you catch a fish for a man he has food for 1 day, but if you learn him how to fish he has food for the rest of his life.

Great help, thanks a lot!


#6

This is Binoy,
I have the following code, but getting error. Please help me to figure it out?
shared code removed


#7

Binoy, I recommend following the steps recommended above for the original poster of this thread. In future please create your own topic as it is slightly rude to hijack threads.


#8