views:

71

answers:

4

I have created a class named Times and I have to construct 4 overloaded methods. I just would like some help understanding overloaded methods and at least maybe some help with the first one. I would really appreciate it. Thanks :)

  • multiply 2 integers and return the (integer) product
  • multiply 3 integers and return the (integer) product
  • multiply 2 double values and return the (double) product
  • multiply 3 double values and return the (double) product
+1  A: 

it would look somthing like this :


public class Times {
    public int mult(int a, int b) {
        return a*b;
    }
    public int mult(int a, int b, int c) {
        return a*b*c;
    }
    //2 more overloaded versions to come here
}

as for understanding what they mean - when your code is compiled the compiler determines which of the methods (all called the same name) to use by looking at the arguments.

so for instance for something like this


int a = 1;
int b = 1;
Times t = new Times();
t.mult(a,b);

the compiler will pick the 1st of the 2 mult methods i demonstrated, while for this:


int a = 1;
int b = 1;
int c = 2;
Times t = new Times();
t.mult(a,b,c);

it will pick the 2nd (based on the number of arguments)

hatchetman82
+3  A: 

like this?

public class Times {
    public static int multiply(int a, int b) {
        return a * b;
    }

    public static int multiply(int a, int b, in c) {
        return multiply(a, b) * c;
    }

    public static double multiply(double a, double b) {
        return a * b;
    }

    public static double multiply(double a, double b) {
        return multiply(a, b) * c;
    }
}
Michael Pardo
Here you have a gentle description about what is method overloading:http://en.wikipedia.org/wiki/Method_overloading
Alexandre
+1  A: 

"Overloaded methods" just means the methods would all have the same name (and be in the same class). The parameters need to be different either in number or type, however.

Since the all multiply stuff, "multiply" makes sense as a name.

The first one:

multiply 2 integers and return the (integer) product

So it returns an integer (an int), is named "multiply", takes 2 ints as parameters, that gives us:

int multiply(int a, int b) {

It returns the product, so the body is:

    return a * b;

And then we're done with that one:

}

That gives us:

int multiply(int a, int b) {
    return a * b;
}

Use the same approach and the same name for the others.

Laurence Gonsalves
A: 

You can do something like this

public class Times {    
public static void main(String[] args) {
    System.out.println(multiplyInt(1,2));
    System.out.println(multiplyDoubles(2.0,3.0));
}

public static int multiplyInt(int... numbers){
    int multiply = 1;
    for(int number : numbers ){
        multiply = multiply*number;
    }
    return multiply;
}

public static double multiplyDoubles(double... numbers){
    double multiply = 1;
    for(double number : numbers ){
        multiply = multiply*number;
    }
    return multiply;
}

}

Shekhar