North South University
Department of Electrical and Computer Engineering
CSE 215L (Programming Language II Lab)
Lab 5: Method Overloading
Objective:
● Method Overloading: Understand the concept of method overloading, allowing the
creation of multiple methods with the same name but different parameter lists. Explore its
application for versatility in method usage.
Method Overloading:
Method overloading in Java is the feature that enables defining several methods in a class having
the same name but with different parameter lists. These algorithms may vary concerning the
number or type of parameters. When a method is called, Java decides which version of it to
execute depending on the arguments given. If we have to perform only one operation, having the
exact name of the methods increases the program's readability.
Suppose you have to perform the addition of the given numbers, but there can be any number of
arguments if you write the method such as a(int, int) for two parameters, and b(int, int, int) for
three parameters, then it may be difficult for you as well as other programmers to understand the
behavior of the method because its name differs
How Method Overloading Works
Method overloading is resolved at compile time (i.e., it is an example of compile-time
polymorphism or static polymorphism).
Rules for Method Overloading:
● Methods must have the same name.
● They must have different parameters (type, number, or both).
● Return type can be different, but it is not enough to distinguish overloaded methods
Example :
class UserInputCalculator {
static int calculate(int a, int b) {
return a + b;
}
static int calculate(int a, int b, int c) {
return a * b * c;
}
static String calculate(double num, String operator) {
if ([Link]("^")) {
return ""+num * num;
} else {
return "Invalid";
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter two integers (a b): ");
int a = [Link]();
int b = [Link]();
[Link]("Sum of " + a + " and " + b + ": " + calculate(a, b));
[Link]("Enter three integers (x y z): ");
int x = [Link]();
int y = [Link]();
int z = [Link]();
[Link]("Product of " + x + ", " + y + ", and " + z + ": " + calculate(x,
y,z));
[Link]("Enter a number to square: ");
double num = [Link]();
[Link]("Square of " + num + ": " + calculate(num, "^"));
}
}
Classwork :
1. Write an overloaded method calculate() that performs different operations based
on parameters:
● If given two integers, return their sum.
● If given three integers, return their product.
● If given a double and an operator ("square" or "cube"), return the
result.
● If given two doubles and a string representing an operation ("add",
"subtract", "multiply", "divide"), return the result accordingly
Sample Output
Homework :
1. Write a program that overloads the convert() method to perform different types of
conversions:
● If given an integer, return its binary representation as a String.
● If given a double, return its rounded integer value.
● If given a character, return its ASCII value.
2. Write a program where printMessage() is overloaded to:
● Print a message without any parameters.
● Print a message with a given name.
● Print a message multiple times if an integer is provided.
3. Write a program that overloads calculateArea() method to support:
● Triangle (given base and height).
● Trapezium (given base1, base2, and height).
● Cube (given side length for surface area)