HW - Function Overloading
HW - 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);
}
}
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);
}
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);
}
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;
}
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;
}
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);
}
}