Results 1 to 6 of 6
  1. #1
    glopez09 is offline Member
    Join Date
    Nov 2009
    Posts
    7
    Rep Power
    0

    Default method overloading and arrays.

    hi guys. i need help understanding how to use these together. i have to write a program that finds the average of a set of numbers (from an array), but use 2 methods, one returning an int, and the other a double. i have just about everything, i just don't know how to call and display the 2 values..?

  2. #2
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    display... use System.out.println(number);

    call.... should be int/double number = methodName(array);

  3. #3
    glopez09 is offline Member
    Join Date
    Nov 2009
    Posts
    7
    Rep Power
    0

    Default

    ok i understand that, but whats the point of the other method if i'm just gonna cast the returned result...? or am i understanding that wrong?

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default

    Just to mention, you won't be able to overload the method unless the input parameters are different.

    If you plan on having your method take an array of ints, for example, and want to overload it to return either a double or an int...you can't. For example, this is not allowed:

    Java Code:
    public int average(int[] array) {}
    public double average(int[] array) {}

  5. #5
    marcme is offline Member
    Join Date
    Dec 2009
    Posts
    2
    Rep Power
    0

    Default

    Overloading a method is done by adding another method with a different signature, but the same name. The returned type is not part of the signature.

    To get two different returned types, you HAVE to name your methods differently (if you want to keep the exact same parameters in both, here your int[] array).

    but whats the point of the other method if i'm just gonna cast the returned result...?
    Exactly, what is the point to write two methods, if really both results can be retrieved from a same double value ?

    What you can do instead, is having two "print" methods:
    Java Code:
    public void printInt(double average) {
        int approxAverage = (int) average;
        System.out.println("Approx average is : " +  approxAverage);
    }
    public void printDouble(double average) {
        System.out.println("Average is : " +  average);
    }

    If really you want to overload a method, then you need different signatures:
    Java Code:
    public void printAverage(double average, boolean approx) {
        if (approx) {
           int approxAverage = (int) average;
           System.out.println("Approx average is : " +  approxAverage);
        } else {
           System.out.println("Average is : " +  average);
        }
    }
    
    public void printAverage(double average) {
         printAverage(average, false);
    }
    Last edited by marcme; 12-08-2009 at 01:10 PM.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,529
    Blog Entries
    7
    Rep Power
    20

    Default

    The best you can do is this:

    Java Code:
    double getAverage(double[] array) { ... }
    double getAverage(int[] array) { ... }
    The logic of both methods will be nearly the same of course.

    kind regards,

    Jos

Similar Threads

  1. add method in arrays
    By MuslimCoder in forum New To Java
    Replies: 1
    Last Post: 03-14-2009, 11:58 AM
  2. Replies: 0
    Last Post: 04-17-2008, 07:43 PM
  3. Overloading?
    By padutch2 in forum New To Java
    Replies: 2
    Last Post: 12-31-2007, 03:26 AM
  4. Method/Operator Overloading
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-01-2007, 08:33 PM
  5. Overloading methods dynamically
    By Syed.muddasir in forum New To Java
    Replies: 6
    Last Post: 07-31-2007, 02:34 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
  •