java program of Box using constructor overloading

//Constructor overloading using Box for no parameter for one parameter and for all parameters.
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.
//-1 to indicate an uninitialized box.
//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());
vol is 3000.0
Vol is -1.0
vol is 343.0

Leave a Comment

Pin It on Pinterest

Share This
Open chat