L13: Methods-II (Method Overloading)

Q1. Write a program with an overloaded method max to find the maximum of two integers, doubles.

class Test {
  public static void main(String[ ] args) {
    System.out.println("The maximum of 3 and 4 is: "+max(3,4));
    System.out.println("The maximum of 3.5 and 4.5 is: "+max(3.5, 4.5));
  }
  public static int max(int a, int b) {
    if(a>b)
      return a;
    else
      return b;
  }
  public static double max(double a, double b) {
    if(a>b)
      return a;
    else
      return b;
  }
}

Q2. Write a program with an overloaded method average that works with different number of arguments.

class Test {
  public static void main(String[ ] args) {
    System.out.println("The average of 9.2, 3.7 and 4.1 is: "+average(9.2,3.7,4.1));
    System.out.println("The average of 3.5 and 4.5 is: "+average(3.5, 4.5));
  }
  public static double average(double n1, double n2) {
    return (n1+n2)/2;
  }
  public static double average(double n1, double n2, double n3) {
    return (n1+n2+n3)/3;
  }
}

Q3. Write a method calculateArea to compute the area of a circle from the radius. Then, overload the calculateArea method to find the area of a rectangle from its length and breadth.

import java.util.Scanner;
class Test {
  public static void main(String[ ] args) {
    Scanner ip=new Scanner(System.in);
    System.out.print("Enter radius of circle: ");
    double rad=ip.nextDouble();
    double circleArea=calculateArea(rad);
    System.out.println("Area of the circle is: "+circleArea);
    System.out.print("Enter length and breadth of rectangle: ");
    double length=ip.nextDouble();
    double breadth=ip.nextDouble();
    double rectangleArea=calculateArea(length,breadth);
    System.out.println("Area of the rectangle is: "+rectangleArea);
  }
  public static double calculateArea(double r) {
    return Math.PI*r*r;
  }
  public static double calculateArea(double l,double b) {
    return l*b;
  }
}

Q4. Can we define two methods in a class that have same method name and parameter list but different return types ?

No, it is not allowed. Overloaded methods must have different parameter lists. You cannot overload methods based on different return types or modifiers.

Q5. Consider the following two method definitions:
public static double m(double x, double y)
public static double m(int x, double y)

identify which of the above two methods is invoked for:
(a) double z=m(2,3.4);
(b) double z=m(2.3,3.5);
(c) double z=m(2,3);

Answer:
For the call m(2,3.4), the method m(int, double) is invoked.
For the call m(2.3,3.5),the method m(double, double) is invoked.
For the call m(2,3) the method m(int, double) is invoked. Here, the argument 3 is automatically converted to a double value and passed to the method.

Q5. Consider following two method definitions:
public static double max(int a, double b)
public static double max(double a, int b)

identify which of the above two methods is invoked for max(5,8).

Answer:
For the invocation of max(5,8), there are two possible matches in this scenario i.e. both max(int,double) and max(double,int). Hence, the compiler fails to identify the best match and results ambiguous invocation. This type of ambiguous invocation leads to compilation error.