Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2010
    Posts
    2
    Rep Power
    0

    Default What did I do wrong?

    public class Question
    {
    public static void main ( String args[] )
    {
    int x = 5, y = 6;
    float a = addParam ( x,y );
    int b = addParam ( x,y );
    System.out.printf ("a is %.2f and b is %d\n", a, b);
    }

    public static int addParam ( int a, int b )
    {
    return a + b;
    }

    public static float addParam ( int a, int b )
    {
    return a + b;
    }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    I don't know -- What is your question? That would go a long way towards helping others help you. Please have a look at the "Smart Questions" link as well as the Code Tags link in my signature below.

    Much luck.

  3. #3
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    The return type is not part of the method signature, and you cannot have two methods with the same method signature.

    -Gary-

  4. #4
    wangwei is offline Member
    Join Date
    Apr 2010
    Posts
    10
    Rep Power
    0

    Smile

    Great! You have learned a very very important part of java, method overloading. As Bruce Eckel said, method overloading is essential to allow the same method name to be used with different argument types. Now you can understand the meaning of overloading?
    So, in your code:
    Java Code:
    float a = addParam ( x,y );
    int b = addParam ( x,y );
    for the x and y are both type int, and the complier doesn't know exactly what method to call, like gcalvin says, The return type is not part of the method signature.
    But luckily, you can change your source to:
    Java Code:
    public class Question
    {
        public static void main ( String args[] )
        {
            int x = 5, y = 6;
            float m = 5f, n = 6f;
            float a = addParam ( m, n ); // it will call the 2nd method without doubt
            int b = addParam ( x, y );
            System.out.printf ("a is %.2f and b is %d\n", a, b);
        }
    
        public static int addParam ( int a, int b )
        {
            return a + b;
        }
    
        public static float addParam ( float a, float b )
        {
            return a + b;
        }
    }
    Good luck!

  5. #5
    Join Date
    Jan 2010
    Posts
    80
    Rep Power
    0

    Default

    First: you have two methods that have the same names

    public static int addParam
    public static float addParam

    So, you have to change the name of one of them.

Similar Threads

  1. Replies: 4
    Last Post: 06-11-2013, 02:37 AM
  2. where did i go wrong?
    By Sri Vidhya in forum New To Java
    Replies: 1
    Last Post: 01-27-2010, 04:11 PM
  3. what wrong
    By pro85 in forum New To Java
    Replies: 3
    Last Post: 02-09-2009, 02:07 PM
  4. what's wrong in here!!!
    By Annatar in forum New To Java
    Replies: 8
    Last Post: 11-14-2008, 03:55 AM
  5. Can someone tell me what I did wrong??
    By booter4429 in forum New To Java
    Replies: 7
    Last Post: 08-13-2008, 09:35 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •