What's the bug in this code?
I'm sure it's something really simple that I'm missing, but after spending a long while going over it, I can't figure out what's wrong. It looks like it should work:
Code :
public class HW {
public static void main(String args[])
{
System.out.println("\nEx0811.JAVA\n");
double num1 = 200;
double num2 = 100;
Calc.add(num1,num2);
Calc.subtract(num1,num2);
System.out.println();
}
}
class Calc
{
public static void add(double a, double b);
{
System.out.println(a + b);
}
public static void subtract(double a, double b);
{
System.out.println(a - b);
}
}
It is supposed to display the sum and difference of num1 and num2. But instead, I get these 6 errors:
#
C:\HW.java:35: error: missing method body, or declare abstract
public static void add(double a, double b);
^
C:\HW.java:37: error: cannot find symbol
System.out.println(a + b);
^
symbol: variable a
location: class Calc
C:\HW.java:37: error: cannot find symbol
System.out.println(a + b);
^
symbol: variable b
location: class Calc
C:\HW.java:42: error: missing method body, or declare abstract
public static void subtract(double a, double b);
^
C:\HW.java:44: error: cannot find symbol
System.out.println(a - b);
^
symbol: variable a
location: class Calc
C:\HW.java:44: error: cannot find symbol
System.out.println(a - b);
^
symbol: variable b
location: class Calc
6 errors
Process completed.
Can someone explain what's wrong to me? Any help greatly appreciated.
Re: What's the bug in this code?
a semicolon is used to terminate a statement in the code, its like a comma or period when you are writing phrases or sentences in an essay, its like telling the compiler "ok i've had enough telling what i need on that phrase", look at your methods, methods are kind of structures that has statements/or statements inside of it, it should not have a semi colons, you only use it if you plan to write abstract classes, well in your case its not.
Re: What's the bug in this code?
Aaaah, yes, thankyou, I figured it out literally just before checking back here. There are semicolons at the ends of the methods in the Calc class. I gotta watch out for that, thankyou for your help, too :)
Re: What's the bug in this code?
dont consider it an error, its also a valid statement, unfortunately not in your case, dont forget about abstract classes
Re: What's the bug in this code?
I'll keep that in mind. Thanks once again.