Monday, January 20, 2014

Java Program to illustrate Constructor Overloading

with 0 Comment
import java.io.*;
import java.lang.*;
import java.text.*;
class box
{
double width,height,depth;
box()
{
width=2.2;
height=3.2;
depth=2.8;
}
box(double a)
{
width=a;
height=h;
depth=a;
}
box(double w,double h,double d)
{
width=w;
height=h;
depth=d;
}
box(box b)
{
width=b.width;
height=b.height;
depth=b.depth;
}
double volume()
{
return(width*height*depth);
}
}
class boxdemo
{
public static  void main(String args[])throws IOException,ParseException
{
double vol,w,h,d;
DecimalFormat df=new DecimalFormat("###.##");
DataInputStream x=new DataInputStream(System.in);
System.out.println("Constructor Overloading");
System.out.println("**************");
System.out.println("Volume of Box");
System.out.println("Using Default Constructor");
System.out.println("----------------------");
box b1=new box();
vol=b1.volume();
vol=df.parse(df.format(vol)).doubleValue();
System.out.println("Volume="+vol);
System.out.println("Using Parameterized Constructor(3 arguments)");
System.out.println("Enter the box of width");
w=Double.parseDouble(x.readLine());
System.out.println("Enter the box of height");
h=Double.parseDouble(x.readLine());
System.out.println("Enter the box of Depth");
d=Double.parseDouble(x.readLine());
box b2=new box(w,h,d);
vol=b2.volume();
vol=df.parse(df.format(vol)).doubleValue();
System.out.println("Volume="+vol);
System.out.println("Using Parametrized Constructor(1 argument)");
System.out.println("--------------------------------------");
System.out.println("Enter the one value");
w=Double.parseDouble(x.readLine());
box b3=new box(w);
vol=b3.volume();
vol=df.parse(df.format(vol)).doubleValue();
System.out.println("Volume="+vol);
System.out.println("Using Copy Constructor");
System.out.println("--------------------"):
box b4=new box(b2);
vol=b4.volume();
vol=df.parse(df.format(vol)).doubleValue();
System.out.println("Volume="+vol);
}
}


-------------
OUTPUT:
------------
Constructor Overloading
********************
Volume of Box
************
Using Default Constructor
--------------------------------
Volume=19.71

Using Parameterized Constructor(3 arguments)
---------------------------------------------------------
Enter the box of width
2.3
Enter the box of height
1.2
Enter the box of depth
5.3
Volume=14.63

Using Parametrized Constructor(1 argument)
------------------------------------------------------
Enter the one value
2.3
Volume=12.17

Using Copy Constructor
-----------------------------
Volume=14.63

0 comments:

Post a Comment

Powered by Blogger.

Blog Archive