How to use the getvolume()
method defined by the Box
class in the boxweight
class? I know that I have to instantiate the Box class in order to use the method defined by it, but how?
class Box {
private int lenght;
private int breadth;
private int height;
int price;
Box(int l, int b, int h) {
lenght= l;
breadth= b;
height= h;
}
public Box(int p) {
price= p;
}
double getvolume() {
return lenght*height*breadth;
}
void setsize(int $l, int $b, int $h ) {
lenght= $l;
breadth= $b;
height= $h;
}
}
public class boxclassdemo {
public static void main(String[] args) {
Box mybox1=new Box(10,10,10);
Box mybox2=new Box(5,5,5);
Box mybox3=new Box(20);
System.out.println(mybox1.getvolume());
System.out.println(mybox2.getvolume());
System.out.println(mybox3.price);
}
}
The boxweight class:
public class boxweight {
int weight;
int length,breadth,height;
public static void main(String[] args) {
boxweight myboxx = new boxweight();
myboxx.weight= 25;
myboxx.length=10;
myboxx.breadth=20;
myboxx.height=30;
}
}