. You just got hired to be a software developer for \'\'Boxes are Us, Inc.\'\'!
ID: 3761008 • Letter: #
Question
. You just got hired to be a software developer for ''Boxes are Us, Inc.''! Congratulations! YOU quickly learn that this company makes cardboard boxes of all shapes and sizes. They hired you to help create the new version of their inventory management system. They complain that the old version of the system only lets them enter whole numbers (3,6,9, etc.) for box dimensions, and they would like to also use decimal numbers (3.5, 6.6,9.4, etc.). You could just write a new Box class that uses floats to store the values and that might work for now, but you know a template Box class that decides at runtime what type to use would be best! Now, your task is to code a Template Box class named Box. It needs to encapsulate a Length, Width, and Height property. It also needs to be able to calculate Its volume (Length * Width * Height). If the fields are passed as ints, then the volume function should return an int. If the fields are passed as floats, then the volume function should return float.Explanation / Answer
public class Shape
{
protected double width;
protected double height;
public Shape(double width, double height)
{
this.width = width;
this.height = height;
}
public double getWidth()
{
return width;
}
public void setWidth(double width)
{
this.width = width;
}
public double getHeight()
{
return height;
}
public void setHeight(double height)
{
this.height = height;
}
public double getArea()
{
return width * height;
}
}
public class Box extends Shape
{
protected double length;
public Box (double width, double height, double depth)
{
super (width, height);
this.depth = depth;
}
public double getLength(){
return length;
}
public void setLength(double length){
this.length = length;
}
public double getVolume(){
return width * height * length;
}
}
public class TestRectangle {
public static void main(String[] args) {
Shape rectangle = new Shape(3,6);
Box box = new Box(9);
System.out.println(" A rectangle " + rectangle.toString());
System.out.println("The area is " + rectangle.getArea())
System.out.println("The volume is " + box.getVolume());
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.