java program of Box using constructor overloading

//Constructor overloading using Box for no parameter for one parameter and for all parameters.
import java.io.*;
import java.util.*;
class MyBox{
double length;
double depth;
double width;
//Constructor called when all three dimesions are apecified.
MyBox(double l,double d,double w){
length = l;
depth = d;
width = w;
}
//Defining method to calculate vol.
double vol1(){
return length*width*depth;
}
//Constructor when no parameters is initialized.
MyBox()
{
//-1 to indicate an uninitialized box.
width=-1;
depth=-1;
length=-1;
}
//Defining method to calculate vol.
double vol2(){
return -1;
}
//Constructor used when only one value is initialized.
MyBox(double l){
length = l;
}
//Defining method to calculate vol.
double vol3(){
return length*length*length;
}
}
class Box_Const_Overloading{
public static void main(String args[]){
MyBox Box1=new MyBox(10,20,15);
MyBox Box2=new MyBox();
MyBox Box3=new MyBox(7);
System.out.println("vol is "+Box1.vol1());
System.out.println("Vol is "+Box2.vol2());
System.out.println("vol is "+Box3.vol3());
}
}
OUT-
vol is 3000.0
Vol is -1.0
vol is 343.0

Comments

Popular posts from this blog

cpanel exam CPSP Answers

How to install zimbra collaboration suite 8.8.11 on CentOS 7

awstats installation