0% found this document useful (0 votes)
5 views3 pages

Java

The document outlines a Java program that demonstrates method overloading by creating two overloaded 'calculator' methods for performing arithmetic operations on integers and floats. The program includes a main method that tests these operations with sample inputs and prints the results. Learning outcomes include understanding method overloading, differentiating methods by data types, and improving coding skills for reusable functions.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

Java

The document outlines a Java program that demonstrates method overloading by creating two overloaded 'calculator' methods for performing arithmetic operations on integers and floats. The program includes a main method that tests these operations with sample inputs and prints the results. Learning outcomes include understanding method overloading, differentiating methods by data types, and improving coding skills for reusable functions.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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:

You might also like