AIM:-Write a program using method overloading to write two overloaded function to
perform arithematic operations the two methods are:-
i)float calculator (int N1,int N2 string CODE).
ii) )int calculator (float N1,float N2 string CODE).
Where code is +,-,*,/.
OBJECTIVE:-
To understand and implement method overloading in Java by creating multiple methods
with the same name but different parameter types to perform basic arithmetic
operations (+, −, ×, ÷).
Code:
import [Link];
class Calculator {
// Method for integer values
float calculator(int N1, int N2, String code) {
switch(code) {
case "+":
return N1 + N2;
case "-":
return N1 - N2;
case "*":
return N1 * N2;
case "/":
return (float)N1 / N2;
default:
[Link]("Invalid Operation");
return 0;
}
// Method for float values
float calculator(float N1, float N2, String code) {
switch(code) {
case "+":
return N1 + N2;
case "-":
return N1 - N2;
case "*":
return N1 * N2;
case "/":
return N1 / N2;
default:
[Link]("Invalid Operation");
return 0;
public static void main(String[] args) {
Calculator obj = new Calculator();
Scanner sc = new Scanner([Link]);
int a = 20, b = 6;
float x = 10.5f, y = 2.5f;
[Link]("Integer Operation: " + [Link](a, b, "+"));
[Link]("Float Operation: " + [Link](x, y, "*"));
Learning Outcome:
• After completing this program, the student will be able to:
• Understand the concept of method overloading in Java.
• Differentiate between methods based on data types and parameters.
• Implement arithmetic operations using overloaded methods.
• Gain knowledge of handling both integer and floating-point values.
• Improve coding skills for writing reusable and efficient functions.
• Understand how function calls are resolved at compile time (compile-time
polymorphism).
Output: