Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: What's the bug in this code?

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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:

    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.


  2. #2
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default 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.

  3. The Following User Says Thank You to chronoz13 For This Useful Post:

    r3dApple (January 11th, 2012)

  4. #3
    Junior Member
    Join Date
    Jan 2012
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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

  5. #4
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default 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

  6. #5
    Junior Member
    Join Date
    Jan 2012
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: What's the bug in this code?

    I'll keep that in mind. Thanks once again.

Similar Threads

  1. problem in my code java code graph editeur
    By kisokiso in forum Java Theory & Questions
    Replies: 5
    Last Post: January 6th, 2012, 08:36 AM
  2. Code is giving an error in console, but there are no errors apparent in the code!
    By JamEngulfer221 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 15th, 2011, 09:30 PM
  3. describe this program code by code ....
    By izzahmed in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 29th, 2011, 11:03 PM