106k views
0 votes
Write the code for the method getNewBox. The method getNewBox will return a GiftBox that has dimensions that are m times the dimensions of its GiftBox parameter, where m is a double parameter of the method.

For example, given the following code segment:
GiftBox gift = new Gift Box (3.0, 4.0, 5.0):
The call
getNewBox , 0, 5).
would return a GiftBox whose dimensions are: length = 1.5, width = 2.0, and height = 2.5 .

User Leiba
by
5.3k points

1 Answer

5 votes

Answer:

public class GiftBox{

private int length;

private int width;

private int height;

public GiftBox(double length, double width, double height) {

this.length = length;

this.width = width;

this.height = height;

}

public static GiftBox getNewBox(GiftBox giftBox, double m) {

return new GiftBox(m * giftBox.length, m * giftBox.width, m * giftBox.height);

}

private boolean fitsInside(GiftBox giftBox) {

if(giftBox.length < this.length && giftBox.width <this.width

&& giftBox.height < this.height) {

return true;

}

return false;

}

public static void main(String []args){

GiftBox giftBox = new GiftBox(3.0 , 4.0, 5.0);

GiftBox newGiftBox = getNewBox(giftBox, 0.5);

System.out.println("New Box length: " + newGiftBox.length);

System.out.println("New Box width: " + newGiftBox.width);

System.out.println("New Box height: " + newGiftBox.height);

GiftBox gift = new GiftBox(3.0 , 4.0, 5.0);

GiftBox other = new GiftBox(2.1 , 3.2, 4.3);

GiftBox yetAnother = new GiftBox(2.0 , 5.0, 4.0);

System.out.println(gift.fitsInside(other));

System.out.println(gift.fitsInside(yetAnother));

}

}

Step-by-step explanation:

The getNewBox is a public method in the GiftBox class in the Java source code above. It returns the GiftBox object instance increasing or multiplying the dimensions by the value of m of type double.

User Dgnin
by
5.7k points