Sunday 24 March 2013

A Java Program which show the Application of Constructors.


class Box
{
double width;
double height;
double depth;
//This is the Constructor for Box....
Box(double w,double h,double d)
{
width=w;
height=h;
depth=d;
}
//Compute and Reture Volume....
double volume()
{
return width*height*depth;
}
}
class BoxDemo
{
public static void main(String args[])
{
//Declare,Alocate and initialize Box Object
Box myBox1=new Box(10,20,15);
Box myBox2=new Box(3,6,9);
double vol;
//Get volume of First Box....
vol=myBox1.volume();
System.out.println("Box1 Volume is:-> "+vol);
//Get Volume of Second Box....
vol=myBox2.volume();
System.out.println("Box2 Volume is:-> "+vol);
}
}

No comments:

Post a Comment

How to change Background color by selecting color from Listview

activitymain.xml file::> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"     xm...