1. Design a class to overload a function calculate() as follows:
void calculate(int n, char c) with one integer and one character argument, computes the square of integer argument if choice c is ‘s’ otherwise finds its cube.
void calculate(int a, int b, char c) with two integer arguments and one character argument. It computes the product of integer arguments if c is ‘p’ else adds the integers.
void calculate (String s, String s1) with two string arguments, which prints whether the strings are equal or not.
import java.io.*;
class fv1
{
static void calculate(int n,char c)
{
if(c=='s')
{
System.out.println("square is:"+n*n);
}
else
{
System.out.println("cube is:"+n*n*n);
}
}
static void calculate(int a,int b,char c)
{
if(c=='p')
{
System.out.println("product is:"+a*b);
}
else
{
System.out.println("sum is:"+(a+b));
}
}
static void calculate(String s,String s1)
{
if(s.equals(s1))
{
System.out.println("they are equal");
}
else
{
System.out.println("they are not equal");
}
}
public static void mian(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int ch;
System.out.println("enter your choice");
ch=Integer.parseInt(br.readLine());
switch(ch)
{
case 1:
System.out.println("enter a integer");
int n=Integer.parseInt(br.readLine());
System.out.println("enter s to find square otherwise cube");
char c=(char)(br.read());
calculate(n,c);
break;
case 2:
System.out.println("enter a integer");
n=Integer.parseInt(br.readLine());
System.out.println("enter another integer");
int n1=Integer.parseInt(br.readLine());
System.out.println("enter p to find product otherwise sum");
c=(char)(br.read());
calculate(n,n1,c);
break;
case 3:
System.out.println("enter a string");
String s=br.readLine();
System.out.println("enter another string");
String s1=br.readLine();
calculate(s,s1);
break;
default:
System.out.println("wrong choice");
}
}
}
2. Write a program using function overloading that computes the volume of a cube, a sphere and a cuboid?
volume of cuve: s*s*s
volume of sphere: 4/3 *pi*r*r*r
volume of cuboid : l*b*h
import java.io.*;
class fv2
{
static void volume(int s)
{
System.out.println("volume is"+s*s*s);
}
static void volume(double p,int r)
{
System.out.println("volume is"+(4/3*p*r*r*r));
}
static void volume(int l,int b,int h)
{
System.out.println("volume is"+(l*b*h));
}
public static void mian(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int ch;
System.out.println("enter your choice");
ch=Integer.parseInt(br.readLine());
switch(ch)
{
case 1:
System.out.println("enter side of cube");
int s=Integer.parseInt(br.readLine());
volume(s);
break;
case 2:
System.out.println("enter radius of sphere");
int r=Integer.parseInt(br.readLine());
double p;
p=3.142;
volume(p,r);
break;
case 3:
System.out.println("enter length of cuboid");
int l=Integer.parseInt(br.readLine());
System.out.println("enter bredth of cuboid");
int b=Integer.parseInt(br.readLine());
System.out.println("enter height of cuboid");
int h=Integer.parseInt(br.readLine());
volume(l,b,h);
break;
default:
System.out.println("wrong choice");
}
}
}
void calculate(int n, char c) with one integer and one character argument, computes the square of integer argument if choice c is ‘s’ otherwise finds its cube.
void calculate(int a, int b, char c) with two integer arguments and one character argument. It computes the product of integer arguments if c is ‘p’ else adds the integers.
void calculate (String s, String s1) with two string arguments, which prints whether the strings are equal or not.
import java.io.*;
class fv1
{
static void calculate(int n,char c)
{
if(c=='s')
{
System.out.println("square is:"+n*n);
}
else
{
System.out.println("cube is:"+n*n*n);
}
}
static void calculate(int a,int b,char c)
{
if(c=='p')
{
System.out.println("product is:"+a*b);
}
else
{
System.out.println("sum is:"+(a+b));
}
}
static void calculate(String s,String s1)
{
if(s.equals(s1))
{
System.out.println("they are equal");
}
else
{
System.out.println("they are not equal");
}
}
public static void mian(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int ch;
System.out.println("enter your choice");
ch=Integer.parseInt(br.readLine());
switch(ch)
{
case 1:
System.out.println("enter a integer");
int n=Integer.parseInt(br.readLine());
System.out.println("enter s to find square otherwise cube");
char c=(char)(br.read());
calculate(n,c);
break;
case 2:
System.out.println("enter a integer");
n=Integer.parseInt(br.readLine());
System.out.println("enter another integer");
int n1=Integer.parseInt(br.readLine());
System.out.println("enter p to find product otherwise sum");
c=(char)(br.read());
calculate(n,n1,c);
break;
case 3:
System.out.println("enter a string");
String s=br.readLine();
System.out.println("enter another string");
String s1=br.readLine();
calculate(s,s1);
break;
default:
System.out.println("wrong choice");
}
}
}
2. Write a program using function overloading that computes the volume of a cube, a sphere and a cuboid?
volume of cuve: s*s*s
volume of sphere: 4/3 *pi*r*r*r
volume of cuboid : l*b*h
import java.io.*;
class fv2
{
static void volume(int s)
{
System.out.println("volume is"+s*s*s);
}
static void volume(double p,int r)
{
System.out.println("volume is"+(4/3*p*r*r*r));
}
static void volume(int l,int b,int h)
{
System.out.println("volume is"+(l*b*h));
}
public static void mian(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int ch;
System.out.println("enter your choice");
ch=Integer.parseInt(br.readLine());
switch(ch)
{
case 1:
System.out.println("enter side of cube");
int s=Integer.parseInt(br.readLine());
volume(s);
break;
case 2:
System.out.println("enter radius of sphere");
int r=Integer.parseInt(br.readLine());
double p;
p=3.142;
volume(p,r);
break;
case 3:
System.out.println("enter length of cuboid");
int l=Integer.parseInt(br.readLine());
System.out.println("enter bredth of cuboid");
int b=Integer.parseInt(br.readLine());
System.out.println("enter height of cuboid");
int h=Integer.parseInt(br.readLine());
volume(l,b,h);
break;
default:
System.out.println("wrong choice");
}
}
}