PROGRAMS ON FUNCTION/METHOD
OVERLOADING
1) Program to calculate the area of a rectangle and
square using method overloading:
Ans: public class Rectangle
{
public double calculateArea(double length,
double width)
{
return length * width;
}
public int calculateArea(int side)
{
return side * side;
}
}
Output:
Topic: Method Overloading Programs Prepared by Ashwini Y.
Topic: Method Overloading Programs Prepared by Ashwini Y.
******************************************************************
OR
public class Rectangle
{
void calculateArea(double length, double width)
{
double area1= length * width;
System.out.print("Area 1: "+area1);
}
void calculateArea(int side)
{
double area2= side * side;
Topic: Method Overloading Programs Prepared by Ashwini Y.
System.out.print("Area 2: "+area2);
}
}
*****************************************************
Or
import java.util.*;
public class Rectangle
{
Scanner sc=new Scanner(System.in);
void calculateArea(double length, double width)
{
length=sc.nextDouble();
width=sc.nextDouble();
double area1= length * width;
System.out.print("Area 1: "+area1);
}
void calculateArea(int side)
{
side=sc.nextInt();
double area2= side * side;
Topic: Method Overloading Programs Prepared by Ashwini Y.
System.out.print("Area 2: "+area2);
}
}
*****************************************************
Or
import java.util.*;
public class Rectangle
{
Scanner sc=new Scanner(System.in);
double calculateArea(double length, double width)
{
return length * width;
}
int calculateArea(int side)
{
return side * side;
}
public static void main(String args[])
{
Rectangle r = new Rectangle();
Topic: Method Overloading Programs Prepared by Ashwini Y.
double area1 = r.calculateArea(4.5,2.5);
System.out.println("Area of rectangle: " + area1);
int area2 = r.calculateArea(5);
System.out.println("Area of square: " + area2);
}
}
*****************************************************
Programs:
1) WAP using method overloading for the following:
void print(int a, int b): To swap 2 numbers using a
third variable.
void print(): To print
*
**
***
****
*****
*****************************************************
Topic: Method Overloading Programs Prepared by Ashwini Y.
2) WAP using method overloading for the
following:
void calculate(double a,double b): To calculate:
(a2+b2)/2ab
void calculate(): To print
1
23
456
7 8 9 10
**********************************************************
Topic: Method Overloading Programs Prepared by Ashwini Y.
Ans:
public class SeriesCalculator
{
void series(int x, int n)
{
int sum = 0;
for (int i = 1; i <= n; i++)
{
sum += Math.pow(x, i);
}
System.out.println("Sum of the series x^1 + x^2 + ... + x^" +
n + " is: " + sum);
}
void series(int p)
{
for (int i = 1; i < p; i++)
{
int term = i * i * i - 1;
System.out.print(term + " ");
}
System.out.println();
}
void series()
{
double sum = 0;
for (int i = 2; i <= 10; i++)
Topic: Method Overloading Programs Prepared by Ashwini Y.
{
sum += 1.0 / i;
}
System.out.println("Sum of the series 1/2 + 1/3 + ... + 1/10
is: " + sum);
}
}
**********************************************************
Ans:
public class P2
Topic: Method Overloading Programs Prepared by Ashwini Y.
{
void print()
{
for(int i = 1; i <= 5; i++)
{
for(int j = 1; j <= 4; j++)
{
System.out.print(i + " ");
}
System.out.println();
}
}
public void print(int n)
{
int digit = 0;
int evenSum = 0;
int oddSum = 0;
while( n != 0)
{
digit = n % 10;
if (digit % 2 == 0)
{
evenSum += digit;
}
else
{
Topic: Method Overloading Programs Prepared by Ashwini Y.
oddSum += digit;
}
n = n / 10;
}
if(evenSum == oddSum)
{
System.out.println("Lead number");
}
else
{
System.out.println("Not a lead number");
}
}
}
**********************************************************
Topic: Method Overloading Programs Prepared by Ashwini Y.
Ans:
public class P3
{
void display()
{
for(int i = 1; i <= 5; i++)
Topic: Method Overloading Programs Prepared by Ashwini Y.
{
for(int j = 1; j <= i; j++)
{
System.out.print(j + " ");
}
System.out.println();
}
}
void display(int n)
{
while( n != 0)
{
double digit = n % 10;
System.out.println(Math.sqrt(digit));
n = n / 10;
}
}
}
**********************************************************
Q) WAP to define a class “compute” to overload the
method “print” as follows:
Topic: Method Overloading Programs Prepared by Ashwini Y.
void print(): to print the following pattern using
nested loop:
*
*#
*#*
*#*#
*#*#*
void print(int number): to accept a number and check
whether the number is a lead number.
A lead number is the one whose sum of even digits is equal
to the sum of odd digits.
e.g. 3669
odd digits sum = 3 + 9 = 12
even digits sum = 6 + 6 = 12
3669 is a lead number.
Ans: import java.util.*;
public class compute
{
void print()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
Topic: Method Overloading Programs Prepared by Ashwini Y.
if(j%2==0)
{
System.out.print("#");
}
else
{
System.out.print("*");
}
}
System.out.println("");
}
}
void print(int number)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
number = sc.nextInt();
int evenSum = 0;
int oddSum = 0;
int tempNumber = number;
// Calculate the sum of even and odd digits
while (tempNumber > 0)
{
int digit = tempNumber % 10;
if (digit % 2 == 0)
{
Topic: Method Overloading Programs Prepared by Ashwini Y.
evenSum += digit;
}
else
{
oddSum += digit;
}
tempNumber /= 10;
}
// Check if the sums are equal
if (evenSum == oddSum)
{
System.out.println(number + " is a lead number.");
}
else
{
System.out.println(number + " is not a lead number.");
}
}
}
—---------------------------------------------------------------------------------
Q) WAP to define a class “calculate” to overload the
method “display” as follows:
void display(): to print the following pattern using
nested loop:
1
22
Topic: Method Overloading Programs Prepared by Ashwini Y.
333
4444
55555
void display(int n): Check and display whether it is a
Niven number or not.
(A number is said to be Niven which is divisible by the
sum of its digits).
Example: Sample Input 126
Sum of its digits = 1 + 2 + 6 = 9
and 126 is divisible by 9.
Ans: import java.util.*;
public class calculate
{
void display()
{
for (int i=1;i<=5;i++)
{
for (int j=1;j<=i;j++)
{
System.out.print(i+" ");
}
System.out.println("");
}
}
void display(int n)
{
Scanner sc = new Scanner(System.in);
Topic: Method Overloading Programs Prepared by Ashwini Y.
System.out.print("Enter a number: ");
n = sc.nextInt();
// Calculate the sum of the digits
int sumOfDigits = 0;
int tempNumber = n;
while (tempNumber > 0)
{
sumOfDigits += tempNumber % 10;
tempNumber /= 10;
}
if (n % sumOfDigits == 0)
{
System.out.println(n + " is a Niven number.");
}
else
{
System.out.println(n + " is not a Niven number.");
}
}
}
—--------------------------------------------------------------------
SOME OTHER PROGRAMS ON METHOD OVERLOADING:
Topic: Method Overloading Programs Prepared by Ashwini Y.
1) Program to calculate the area of a rectangle using method
overloading:
Ans: public class Rectangle
{
public double calculateArea(double length, double width)
{
return length * width;
}
public int calculateArea(int side)
{
return side * side;
}
public static void main(String[] args)
{
Rectangle rect = new Rectangle();
double area1 = rect.calculateArea(4.5, 2.5);
System.out.println("Area of rectangle: " + area1);
int area2 = rect.calculateArea(5);
System.out.println("Area of square: " + area2);
}
}
2) Program to perform addition using method overloading:
Ans: public class Addition
{
Topic: Method Overloading Programs Prepared by Ashwini Y.
public int add(int a, int b)
{
return a + b;
}
public double add(double a, double b)
{
return a + b;
}
public static void main(String[] args)
{
Addition ob = new Addition();
int sum1 = ob.add(5, 7);
System.out.println("Sum of integers: " + sum1);
double sum2 = ob.add(3.14, 2.86);
System.out.println("Sum of doubles: " + sum2);
}
}
3) Program to concatenate strings using method overloading:
Ans: public class StringConcatenation
{
public String concatenate(String str1, String str2)
{
return str1 + str2;
}
Topic: Method Overloading Programs Prepared by Ashwini Y.
public String concatenate(String str1, String str2, String str3)
{
return str1 + str2 + str3;
}
public static void main(String[] args)
{
StringConcatenation ob = new StringConcatenation();
String result1 = ob.concatenate("Hello, ", "World!");
System.out.println(result1);
String result2 = ob.concatenate("Java", " is", " awesome!");
System.out.println(result2);
}
}
4) Program to find the maximum of two numbers using method
overloading:
Ans: public class MaximumFinder
{
public int getMax(int a, int b)
{
return Math.max(a, b);
}
public double getMax(double a, double b)
{
return Math.max(a, b);
}
Topic: Method Overloading Programs Prepared by Ashwini Y.
public static void main(String[] args)
{
MaximumFinder ob = new MaximumFinder();
int max1 = ob.getMax(5, 7);
System.out.println("Maximum of integers: " + max1);
double max2 = ob.getMax(3.14, 2.86);
System.out.println("Maximum of doubles: " + max2);
}
}
5) Program to calculate the volume of shapes using method
overloading:
Ans: public class ShapeVolumeCalculator
{
public double calculateVolume(double radius)
{
return (4/3.0) * Math.PI * radius * radius * radius;
}
public double calculateVolume(double length, double width,
double height)
{
return length * width * height;
}
public static void main(String[] args)
{
Topic: Method Overloading Programs Prepared by Ashwini Y.
ShapeVolumeCalculator ob = new ShapeVolumeCalculator();
double volume1 = ob.calculateVolume(3.5);
System.out.println("Volume of sphere: " + volume1);
double volume2 = ob.calculateVolume(4.5, 2.5, 3.0);
System.out.println("Volume of rectangular prism: "+ volume2);
}
}
******************************************************************************
Topic: Method Overloading Programs Prepared by Ashwini Y.