CLASS EXAMPLE

Class include method and variable

class box
{
double weight;
double height;
double depth;
void volume()
{
System.out.println(“Volume is “+weight*height*depth);
}
}
class boxdemo1
{
public static void main( String args[])
{
box myobj1 = new box();
box myobj2 = new box();
myobj1.weight=10;
myobj1.height=12;
myobj1.depth=10;
myobj2.weight=10;
myobj2.height=13;
myobj2.depth=10;

myobj1.volume();
myobj2.volume();
}
}

Method returning a value

class box
{
double weight;
double height;
double depth;
double volume()
{
return weight*height*depth;
}

}
class boxdemo2
{
public static void main( String args[])
{
box myobj1 = new box();
box myobj2 = new box();
double vol1,vol2;
myobj1.weight=10;
myobj1.height=12;
myobj1.depth=10;
myobj2.weight=10;
myobj2.height=13;
myobj2.depth=10;
vol1=myobj1.volume();
vol2=myobj2.volume();
System.out.println(“Volume of box1 is ” + vol1);
System.out.println(“Volume of box1 is ” + vol2);
}
}

Parameterized method

class box
{
double weight;
double height;
double depth;
double volume()
{
return weight*height*depth;
}
void setdime(double w,double h ,double d )
{
weight = w;
height= h;
depth = d;
}
}
class boxdemo3
{
public static void main( String args[])
{
box myobj1 = new box();
box myobj2 = new box();
double vol1,vol2;
myobj1.setdime(10,12,10);
myobj2.setdime(10,13,10);
vol1=myobj1.volume();
vol2=myobj2.volume();
System.out.println(“Volume of box1 is ” + vol1);
System.out.println(“Volume of box1 is ” + vol2);
}
}

 

Share

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.