Results 1 to 6 of 6

Thread: Overloaded Methods

  1. #1

    Thread Starter
    PowerPoster eranga262154's Avatar
    Join Date
    Jun 2006
    Posts
    2,201

    Wink 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?

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    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

  3. #3

    Thread Starter
    PowerPoster eranga262154's Avatar
    Join Date
    Jun 2006
    Posts
    2,201

    Wink Re: Overloaded Methods

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

  4. #4
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    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

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    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.

  6. #6

    Thread Starter
    PowerPoster eranga262154's Avatar
    Join Date
    Jun 2006
    Posts
    2,201

    Wink Re: Overloaded Methods

    Quote 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
  •  



Click Here to Expand Forum to Full Width