Assignment: Module 04
![]() |
Assignment: Module 04 |
Write a Class Overload contains add() which accepts different parameters to explain the overloading.
Input:
Enter the 2 integers
2
1
Enter 2 float values
22.3
11.1
Enter 3 double values
33.1
12.3
11.1
Enter first name
karthik
Enter second name
vishal
Output:
Sum of 2 integers =3
Sum of two float =33.4
Sum of double =56.50000000000001
Concatenated string =karthikvishal
Here is the sample code in Java:
class Overload {
public int add(int a, int b) {
return a + b;
}
public float add(float a, float b) {
return a + b;
}
public double add(double a, double b, double c) {
return a + b + c;
}
public String add(String a, String b) {
return a + b;
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Overload o = new Overload();
System.out.println("Enter the 2 integers");
int a = sc.nextInt();
int b = sc.nextInt();
System.out.println("Sum of 2 integers =" + o.add(a, b));
System.out.println("Enter 2 float values");
float x = sc.nextFloat();
float y = sc.nextFloat();
System.out.println("Sum of two float =" + o.add(x, y));
System.out.println("Enter 3 double values");
double p = sc.nextDouble();
double q = sc.nextDouble();
double r = sc.nextDouble();
System.out.println("Sum of double =" + o.add(p, q, r));
System.out.println("Enter first name");
sc.nextLine();
String s1 = sc.nextLine();
System.out.println("Enter second name");
String s2 = sc.nextLine();
System.out.println("Concatenated string =" + o.add(s1, s2));
}
}
Output:
Enter the 2 integers
2
1
Sum of 2 integers =3
Enter 2 float values
22.3
11.1
Sum of two float =33.4
Enter 3 double values
33.1
12.3
11.1
Sum of double =56.50000000000001
Enter first name
karthik
Enter second name
vishal
Concatenated string =karthikvishal