Overload
Excersise : 2
Use Overload i) Method ii) Constructor
Program : Method Overloading
import java.io.*;
import java.lang.*;
import java.math.*;
class Methov
{
int area(int a)
{
return a*a;
}
float area(float l,float b)
{
return l*b;
}
double area(double sa,double sb,double sc)
{
double s,ta;
s=(sa+sb+sc)/2;
ta=Math.sqrt(s*(s-sa)*(s-sb)*(s-sc));
return ta;
}
}
class Area
{
public static void main(String arg[])
{
Methov mo=new Methov();
DataInputStream din=new DataInputStream(System.in);
try
{
int s1,sar;
System.out.println("Enter the side value:");
s1=Integer.parseInt(din.readLine());
sar=mo.area(s1);
System.out.println("Area of the Square:"+sar);
float l1,b1,ra;
System.out.println("Enter the Value of length and breadth:");
l1=Float.parseFloat(din.readLine());
b1=Float.parseFloat(din.readLine());
ra=mo.area(l1,b1);
System.out.println("Area of the Rectangle:"+ra);
double sa1,sb1,sc1,ta1;
System.out.println("Enter the value of side 1:");
sa1=Double.parseDouble(din.readLine());
System.out.println("Enter the value of side 2:");
sb1=Double.parseDouble(din.readLine());
System.out.println("Enter the value of side 3:");
sc1=Double.parseDouble(din.readLine());
ta1=mo.area(sa1,sb1,sc1);
System.out.println("Area of the Triangle:"+ta1);
}
catch(Exception e)
{
System.out.println("Input Error");
}
}
}
Output
Program : Constructor Overloading
import java.io.*;
import java.lang.*;
import java.math.*;
class Conov
{
int side;
float length,breadth;
double side1,side2,side3;
Conov()
{
side=0;
length=0;
breadth=0;
side1=0.0;
side2=0.0;
side3=0.0;
}
Conov(int a)
{
side=a;
}
Conov(float l,float b)
{
length=l;
breadth=b;
}
Conov(double x,double y,double z)
{
side1=x;
side2=y;
side3=z;
}
void sarea()
{
int sa=side*side;
System.out.println("Area of the Square="+sa);
}
void rarea()
{
float ra=length*breadth;
System.out.println("Area of the rectangle="+ra);
}
void tarea()
{
double s,ta;
s=(side1+side2+side3)/2;
ta=Math.sqrt(s*(s-side1)*(s-side2)*(s-side3));
System.out.println("Area of the triangle="+ta);
}
}
class Su1
{
public static void main(String args[])
{
Conov co1,co2,co3,co4;
co1=new Conov();
DataInputStream din;
din=new DataInputStream(System.in);
try
{
int s;
System.out.println("Enter the value of side:");
s=Integer.parseInt(din.readLine());
co2=new Conov(s);
co2.sarea();
float l1,b1;
System.out.println("Enter the value of length:");
l1=Float.parseFloat(din.readLine());
System.out.println("Enter the value of breadth:");
b1=Float.parseFloat(din.readLine());
co3=new Conov(l1,b1);
co3.rarea();
double x1,y1,z1;
System.out.println("Enter the value of side1:");
x1=Double.parseDouble(din.readLine());
System.out.println("Enter the value of side2:");
y1=Double.parseDouble(din.readLine());
System.out.println("Enter the value of side3:");
z1= Double.parseDouble(din.readLine());
co4=new Conov(x1,y1,z1);
co4.tarea();
}
catch(Exception e)
{
System.out.println("Input Error");
}
}
}
Output
Post a Comment
Thanks for your comments. Please be visit and given positive review on our site.