How do i call volume from getVolume into isBigbox method to write an If statement that checks if Box vlomue bigger than or equals to 500?
As you read in the title I am trying to use the volume in getVolume method inside the isBigbox method to check if the volume is smaller than or equals to 500 in which it will print a statement that its big box else it will show it is a small box
public class Box {
private double length;
private double width;
private double height;
private String color;
public Box(double length, double width, double height, String color) {
this.length = length;
this.width = width;
this.height = height;
this.color = color;
}
// Overload constructor 1
public Box() {
this(1, 1, 1, "without color");
}
// Overload constructor 2
public Box(double length, double width, double height) {
this(1, 2, 3, "blue");
}
// Overload constructor 3
public Box(double width, double height, String color) {
this(5, 5, 8, "red");
}
// Standard getters and setters omitted for brevity
public void getVolume(double volume) {
volume = this.height * this.width * this.height;
System.out.println("The Volume of the box is:" volume);
}
public void isBigBox(Box b) {
//
}
}
CodePudding user response:
You call the method getVolume
but it does not return anything (void
). You should split the methods - than you can call them directly:
public double getVolume() {
return this.height * this.width * this.length;
}
public void printVolume() {
System.out.println("The Volume of the box is:" this.getVolume());
}
In isBigBox
you can then simple check the volume:
public boolean isBigBox() {
return this.getVolume() > 500;
}
CodePudding user response:
You will need to make variable called volume in the class Box and make the constructor calculate the volume. you need something like this:
public class Box {
private double length;
private double width;
private double height;
private String color;
public Box(double length, double width, double height,String color){
this.length=length;
this.width=width;
this.height=height;
this.color=color;
}
//
public double getVolume() {
return hight*width*length;
}
//
public boolean isBigBox(){
return (getVolume() >= 500);
}
}