|
-
Aug 27th, 2006, 11:29 PM
#1
Thread Starter
PowerPoster
Overloaded Methods
Hi...
I want to create 3 overloaded methods for the following code block.
int i1 = 11;
int i2 = 5;
int iTotal = addNumbers(i1,i2); // method 1
System.out.println("iTotal: "+iTotal);
double d1 = 12.0;
double d2 = 3.9;
double dTotal = addNumbers(d1,d2); // method 2
System.out.println("dTotal: "+dTotal);
double mixedTotal = addNumbers(i1,d1); //method 3
System.out.println("mixedTotal: "+mixedTotal);
I done it as follows,
class OverloadMethods
{
public static void main(String args[])
{
int i1 = 11;
int i2 = 5;
int iTotal = addNumbers(i1,i2); // method 1
System.out.println("iTotal: "+iTotal);
double d1 = 12.0;
double d2 = 3.9;
double dTotal = addNumbers(d1,d2); // method 2
System.out.println("dTotal: "+dTotal);
double mixedTotal = addNumbers(i1,d1); //method 3
System.out.println("mixedTotal: "+mixedTotal);
}
static int addNumbers(int i1,int i2)
{
return i1 +i2;
}
static double addNumbers(double d1,double d2)
{
return d1 + d2;
}
static double addNumbers(int i1,double d1)
{
return (double)i1 + d1;
}
}
It’s ok for me. Now what I want to do it need to write a 4th method, let say orderedOutput() that take three totals as parameters (iTotal,dTotal,mixedTotal) and displays them from greatest to least.
I’m confusing how to display all these three totals in a single method. Please can someone help on this?
-
Aug 28th, 2006, 06:41 AM
#2
Re: Overloaded Methods
Code:
static void orderOutput(double v1,double v2, double v3){}
total is a value not an object you know , and since the parameters could be either a double or an integer, go with the larger memory block
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Aug 28th, 2006, 07:12 AM
#3
Thread Starter
PowerPoster
Re: Overloaded Methods
 Originally Posted by ComputerJy
Code:
static void orderOutput(double v1,double v2, double v3){}
total is a value not an object you know  , and since the parameters could be either a double or an integer, go with the larger memory block
Its ok,
What I want to know is how to return three parameters through a singlel method.
-
Aug 28th, 2006, 07:28 AM
#4
Re: Overloaded Methods
you can set them as class attributes or pass objects instead of values (Integer instead of int...)
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Aug 28th, 2006, 11:18 AM
#5
Re: Overloaded Methods
The primitive wrappers are immutable. Pass arrays with one element or custom wrappers.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Aug 28th, 2006, 10:24 PM
#6
Thread Starter
PowerPoster
Re: Overloaded Methods
 Originally Posted by CornedBee
The primitive wrappers are immutable. Pass arrays with one element or custom wrappers.
That you mean, first ordered three elemwnts and store in a array, from the order what I want. Then return that array. Isn't it?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|