Results 1 to 5 of 5
Thread: What did I do wrong?
- 04-02-2010, 07:52 PM #1
Member
- Join Date
- Apr 2010
- Posts
- 2
- Rep Power
- 0
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;
}
}
-
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.
- 04-02-2010, 08:52 PM #3
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
The return type is not part of the method signature, and you cannot have two methods with the same method signature.
-Gary-
- 04-03-2010, 03:28 AM #4
Member
- Join Date
- Apr 2010
- Posts
- 10
- Rep Power
- 0
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:
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.Java Code:float a = addParam ( x,y ); int b = addParam ( x,y );
But luckily, you can change your source to:
Good luck!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; } }
- 04-03-2010, 04:51 AM #5
Member
- Join Date
- Jan 2010
- Posts
- 80
- Rep Power
- 0
Similar Threads
-
where did i go wrong?
By Sri Vidhya in forum New To JavaReplies: 1Last Post: 01-27-2010, 03:11 PM -
Wrong output (well.. the one who's wrong is probably me ;) )
By shacht1 in forum New To JavaReplies: 2Last Post: 11-22-2009, 03:48 PM -
what wrong
By pro85 in forum New To JavaReplies: 3Last Post: 02-09-2009, 01:07 PM -
what's wrong in here!!!
By Annatar in forum New To JavaReplies: 8Last Post: 11-14-2008, 02:55 AM -
Can someone tell me what I did wrong??
By booter4429 in forum New To JavaReplies: 7Last Post: 08-13-2008, 08:35 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks