0% found this document useful (0 votes)
0 views14 pages

HW - Function Overloading

The document provides examples of function overloading in Java, showcasing how to define multiple methods with the same name but different parameters. It includes various classes that implement overloaded methods for operations such as calculating sums, areas, volumes, and series, as well as drawing shapes and comparing values. Each class contains a main method demonstrating the usage of the overloaded functions with different argument types and counts.

Uploaded by

anshuljariwala5
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)
0 views14 pages

HW - Function Overloading

The document provides examples of function overloading in Java, showcasing how to define multiple methods with the same name but different parameters. It includes various classes that implement overloaded methods for operations such as calculating sums, areas, volumes, and series, as well as drawing shapes and comparing values. Each class contains a main method demonstrating the usage of the overloaded functions with different argument types and counts.

Uploaded by

anshuljariwala5
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
You are on page 1/ 14

FUNCTION OVERLOADING

Function Overloading means a class contains more than one function with same name but
different number or type of arguments.
class Overloading
{
void sum(int a, int b)
{
int c = a + b;
System.out.println("Sum is " + c);
}
void sum(int a, int b, int c)
{
int d = a + b + c;
System.out.println("Sum is " + d);
}
void sum(float a, float b, float c)
{
float d = a + b + c;
System.out.println("Sum is " + d);
}
void sum(int a, double b)
{
double c = a + b;
System.out.println("Sum is " + c);
}
public static void main()
{
Overloading obj = new Overloading();
obj.sum(10,20);
obj.sum(10,20,30);
obj.sum(10,20.3);
obj.sum(10.1f,20.2f,30.3f);
}
}

2024 - Define a class to overload the method perform as follows:


double perform (double r, double h) — to calculate and return the value of curved surface
area of cone CSA=πrl where 𝑙 = √𝑟 2 + ℎ2
void perform (int r, int c) — Use NESTED FOR LOOP to generate the following format
r = 4, c = 5
output
12345
12345
12345
12345
void perform (int m, int n, char ch) — to print the quotient of the division of m and n if ch is
Q else print the remainder of the division of m and n if ch is R
class Overload
{
double perform(double r, double h)
{
double l = Math.sqrt(Math.pow(r,2) + Math.pow(h,2));
double csa = Math.PI * r * l;
return csa;
}
void perform(int r, int c)
{
for(int i = 1; i <= r; i++)
{
for(int j = 1; j <= c; j++)
{
System.out.print(j + " ");
}
System.out.println();
}
}
void perform(int m, int n, char ch)
{
if(ch == 'Q' || ch == 'q')
System.out.println("Quotient: " + (m / n));
else if(ch == 'R' || ch == 'r')
System.out.println("Remainder: " + (m % n));
}
public static void main()
{
Overload obj = new Overload();
double ans = obj.perform(2.3,5.6);
System.out.println("Curved Surface Area "+ ans);
obj.perform(2,3);
obj.perform(12,3,'q');
}
}
2023 - Define a class to overload the function print as follows:
void print() - to print the following format
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4
5 5 5 5
void print(int n) - To check whether the number is a lead number. A lead number is the one
whose sum of even digits are equal to sum of odd digits. e.g. 3669
odd digits sum = 3 + 9 = 12 even digits sum = 6 + 6 = 12 3669 is a lead number.
class P2023
{
void print()
{
int i, j;
for(i = 1; i <= 5; i++)
{
for(j = 1; j <= 4; j++)
{
System.out.print(i + " ");
}
System.out.println();
}
}
void print(int n)
{
int rem, sume = 0, sumo = 0;
while(n > 0)
{
rem = n%10;
if(rem%2==0)
sume += rem;
else
sumo += rem;
n=n/10;
}
if(sume == sumo)
System.out.println("It is a Lead number");
else
System.out.println("It is not a Lead number");
}
public static void main( )
{
P2023 obj = new P2023();
obj.print();
obj.print(3669);
}
}
2020 - Design a class to overload a method number() as follows:
(i) void number(int num, int d) – to count and display the frequency of a digit in a number.
Example: num = 2565685 d=5
Frequency of digit 5 = 3
(ii) void number(int n) – to find and display the sum of even digits of a number.
Example: n = 29865 Sum of even digits = 16
Write a main() method to create an object and invoke the above methods.

class P2020
{
void Number(int num, int d)
{
int rem,cnt = 0;
while(num > 0)
{
rem = num%10;
if(rem==d)
cnt++;
num=num/10;
}
System.out.println("Frequency of digit " + d + " = " + cnt);
}
void Number(int n1)
{
int rem, sume = 0;
while(n1 > 0)
{
rem = n1%10;
if(rem%2==0)
sume += rem;
n1=n1/10;
}
System.out.println("Sum of even digits = " + sume);
}

public static void main( )


{
P2020 obj = new P2020();
obj.Number(252255,5);
obj.Number(3669);
}
}
2019-Design a class to overload a function series( ) as follows:
void series (int x, int n) – To display the sum of the series given below:
x1 + x2 + x3 + .......... xn terms
void series (int p) – To display the following series:
0, 7, 26, 63 .......... p terms
void series () – To display the sum of the series given below:
1/2 + 1/3 + 1/4 + .......... 1/10
class P2019
{
void series(int x, int n)
{
int i;
double sum =0.0;
for(i = 1; i <= n; i++)
{
sum = sum + Math.pow(x,i);
}
System.out.println("Sum of series = "+ sum);
}
void series(int p)
{
int i, c, cnt = 1;
for(i = 1; cnt <= p; i++)
{
c = (i*i*i)-1;
System.out.print(c + "\t");
cnt++;
}
}
void series()
{
int i;
double sum =0.0;
for(i = 2; i <= 10; i++)
{
sum = sum + 1.0/i;
}
System.out.println("Sum of series = "+ sum);
}
public static void main( )
{
P2019 obj = new P2019();
obj.series(3,5);
obj.series(6);
obj.series();
}
}
2018-Design a class to overload a function volume() as follows:
double volume (double R) – with radius (R) as an argument, returns the volume of sphere
using the formula. V = 4/3 x 22/7 x R3
double volume (double H, double R) – with height(H) and radius(R) as the arguments,
returns the volume of a cylinder using the formula. V = 22/7 x R2 x H
double volume (double L, double B, double H) – with length(L), breadth(B) and Height(H)
as the arguments, returns the volume of a cuboid using the formula. V=LxBxH

class P2018
{
double volume(double R)
{
return(4.0/3*Math.PI*Math.pow(R,3));
}
double volume(double H, double R)
{
return(Math.PI*Math.pow(R,2)*H);
}
double volume(double L, double B,double H)
{
return(L*B*H);
}
public static void main()
{
P2018 obj=new P2018 ();
System.out.println(obj.volume(5.5));
System.out.println(obj.volume(2.0,1.0));
System.out.println(obj.volume(2.5,3.5,4.5));
}
}
2016-Design a class to overload a function sumSeries() as follows:
void sumSeries(int n, double x): with one integer argument and one double argument to find
and display the sum of the series given below:
s=x/1 − x/2 + x/3 − x/4 + x/5... ... ... to n terms
void sumSeries(): to find and display the sum of the following series:
s=1 + (1×2) + (1×2×3) +... ... ... + (1×2×3×4... ... ... ×20)

class P2016
{
void sumSeries(int n,double x)
{
int i;
double sum=0.0;
for(i=1;i<=n;i++)
{
if(i%2!=0)
sum=sum+x/i;
else
sum=sum-x/i;
}
System.out.println("Sum of series = "+sum);
}
void sumSeries()
{
int i,p=1;
double sum=0.0;
for(i=1;i<=20;i++)
{
p=p*i;
sum=sum+p;
}
System.out.println("Sum of series = "+sum);
}

public static void main()


{
P2016 obj = new P2016();
obj.sumSeries(3,2.0);
obj.sumSeries();
}
}
2014- Design a class to overload a function area( ) as follows:
double area (double a, double b, double c) with three double arguments, returns the
area of a scalene triangle using the formula:
area =√𝑠(𝑠 − 𝑎)(𝑠 − 𝑏)(𝑠 − 𝑐) where s = (a+b+c) / 2
double area (int a, int b, int height) with three integer arguments, returns the area of
a trapezium using the formula: area = (1/2)height(a + b)
double area (double diagonal1, double diagonal2) with two double arguments,
returns the area of a rhombus using the formula: area = 1/2(diagonal1 x diagonal2)

class Overload
{
double area(double a, double b, double c)
{
double s = (a+b+c)/2;
double ar = Math.sqrt(s*(s-a)*(s-b)*(s-c));
return ar;
}

double area(int a, int b, int height)


{
double ar=0.5*height*(a+b);
return ar;
}

double area(double diagonal1, double diagonal2)


{
double ar=0.5*(diagonal1*diagonal2);
return ar;
}

public static void main()


{
Overload obj= new Overload();
System.out.println(obj.area(5,4,8));
System.out.println(obj.area(5.5,4.4,8.8));
System.out.println(obj.area(8.5,9.0));
}
}
2013-Design a class to overload a function series( ) as follows:
double series(double n) with one double argument and returns the sum of the series.
sum = (1/1) + (1/2) + (1/3) + .......... + (1/n)
double series(double a, double n) with two double arguments and returns the sum of
the series. sum = (1/a2) + (4/a5) + (7/a8) + (10/a11) + .......... to n terms

class Overload
{
double series(double n)
{
int i;
double sum=0.0;
for(i=1;i<=n;i++)
{
sum = sum+1.0/i;
}
return sum;
}

double series(double a, double n)


{
int i,cnt =1;
double sum=0.0;
for(i=1;cnt<=n;i+=3)
{
sum=sum+ i/Math.pow(a,(i+1));
cnt++;
}
return sum;
}

public static void main( )


{
Overload obj = new Overload();
double ans1 = obj.series(3.0);
System.out.println("Sum of series 1 = " + ans1);
double ans2 = obj.series(3.0,2.0);
System.out.println("Sum of series 2 = " + ans2);
}
}
2012- Design a class to overload a function polygon() as follows:
void polygon(int n, char ch) — with one integer and one character type argument to
draw a filled square of side n using the character stored in ch.
void polygon(int x, int y) — with two integer arguments that draws a filled rectangle
of length x and breadth y, using the symbol '@'.
void polygon() — with no argument that draws a filled triangle shown below:
Example:
1. Input value of n=2, ch = 'O'
Output:
OO
OO
2. Input value of x = 2, y = 5
Output:
@@@@@
@@@@@
3. Output:
*
**
***
class P2012
{
void polygon(int n,char ch)
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
System.out.print(ch);
}
System.out.println();
}
}
void polygon(int x,int y)
{
for(int i=1;i<=x;i++)
{
for(int j=1;j<=y;j++)
{
System.out.print("@");
}
System.out.println();
}
}
void polygon()
{
for(int i=1;i<=3;i++)
{
for(int j=1;j<=i;j++)
{
System.out.print("*");
}
System.out.println();
}
}
public static void main( )
{
P2012 obj=new P2012();
obj.polygon(2,'O');
obj.polygon(3,5);
obj.polygon();
}
}

2011- Design a class to overload a function compare( ) as follows:


void compare(int, int) — to compare two integers values and print the greater of the
two integers.
void compare(char, char) — to compare the numeric value of two characters and print
with the higher numeric value.
void compare(String, String) — to compare the length of the two strings and print the
longer of the two.
class P2011
{
void compare(int a,int b)
{
if(a>b)
System.out.println(a + " is greater ");
else
System.out.println(b + " is greater ");
}
void compare(char a,char b)
{
if((int)a>(int)b)
System.out.println(a + " is greater ");
else
System.out.println(b + " is greater ");
}
void compare(String s1, String s2)
{
if(s1.length() > s2.length())
System.out.println(s1 + " is longer");
else
System.out.println(s2 + " is longer");
}

public static void main()


{
P2011 obj= new P2011();
obj.compare(10,20);
obj.compare('a','c');
obj.compare("Hello","Hi");
}
}

2009-Design a class to overload a function num_calc() as follows :


void num_calc (int num, char ch) with one integer argument and one character argument,
computes the square of integer argument if choice ch is ‘s’ otherwise finds its cube.
void num_calc (int a, int b, char ch) with two integer arguments and one character argument. It
computes the product of integer arguments if ch is ‘p’
else adds the integers.
void num_calc (String s1, String s2) with two string arguments, which prints whether the
strings are equal or not.

class Overload
{
void num_calc(int num, char ch)
{
if(ch == 's')
System.out.println(Math.pow(num,2));
else
System.out.println(Math.pow(num,3));
}
void num_calc(int a, int b, char ch)
{
if(ch == 'p')
System.out.println(a*b);
else
System.out.println((a+b));
}
void num_calc(String s1, String s2)
{
if(s1.equals(s2))
System.out.println("Equal");
else
System.out.println("Not Equal");
}
public static void main()
{
Overload obj = new Overload();
obj.num_calc(2,'s');
obj.num_calc(2,3,'p');
obj.num_calc("hi","he");
}
}

2008- Write a class with the name volume using function overloading that computes the
volume of a cube, a sphere and a cuboid.
Formula volume of a cube (vc) = s*s*s
volume of a sphere (vs) = 4/3* π* r* r*r (where π = 3.14 or 22/7)
volume of a cuboid (vcd) = l* b* h

class P2008
{
void vol(int s)
{
System.out.println("Volume of a cube is " + Math.pow(s,3));
}
void vol(double r)
{
System.out.println("Volume of a sphere is " + (4/3.0 * Math.PI * Math.pow(r,3)));
}
void vol(int l, int b, int h)
{
System.out.println("Volume of a cuboid is " + (l*b*h));
}
public static void main( )
{
P2008 obj = new P2008();
obj.vol(5);
obj.vol(5.6);
obj.vol(5,6,7);
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy