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);
}
}
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");
}
}
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");
}
}
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));
}
}