S.No: 7 Exp.
Name: Write a Java program to implement Method overloading Date: 2022-04-29
ID: 20095A0363
Page No:
Aim:
Write a Java program with a class name Addition with the methods add(int, int) ,
add(int, float) , add(float, float) and add(float, double, double) to add values of
different argument types.
Write the main(String[]) method within the class and assume that it will always receive a total of 6 command
line arguments at least, such that the first 2 are int, next 2 are float and the last 2 are of type double.
If the main() is provided with arguments : 1, 2, 1.5f, 2.5f, 1.0, 2.0 then the program should print the output as:
Sum of 1 and 2 : 3
Sum of 1.5 and 2.5 : 4.0
Sum of 2 and 2.5 : 4.5
Sum of 1.5, 1.0 and 2.0 : 4.5
Note: Please don't change the package name.
Source Code:
q11266/Addition.java
Rajeev Gandhi Memorial College of Engineering and Technology (Autonomous) 2019-2023-MECH-FDH
package q11266;
class Addition
{
public static int add(int a,int b)
return a+b;
public static float add(int a,float b)
return a+b;
public static float add(float a, float b)
return a+b;
}
public static double add(float a,double b,double c)
return a+b+c;
public static void main(String args[])
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
float c = Float.parseFloat(args[2]);
float d = Float.parseFloat(args[3]);
double e = Double.parseDouble(args[4]);
double f = Double.parseDouble(args[5]);
System.out.println("Sum of "+a+" and "+b+" : "+add(a,b));
System.out.println("Sum of "+c+" and "+d+" : "+add(c,d));
System.out.println("Sum of "+b+" and "+d+" : "+add(b,d));
System.out.println("Sum of "+c+", "+e+" and "+f+" : "+add(c,e,
f));
ID: 20095A0363
Page No:
}
Execution Results - All test cases have succeeded!
Test Case - 1
User Output
Sum of 2 and 1 : 3
Sum of 5.0 and 3.6 : 8.6
Sum of 1 and 3.6 : 4.6
Sum of 5.0, 9.2 and 5.26 : 19.46
Rajeev Gandhi Memorial College of Engineering and Technology (Autonomous) 2019-2023-MECH-FDH