The following code is for a class named Box The class Box in
The following code is for a class named Box. The class Box includes a constructor method Box, and a method getVolume().
I need you to develop a java class named MatchBox. Your class MatchBox must extend the class Box and in addition to the attributes width, height, and depth that are defined in the class Box, MatchBox must add a new attribute weight. The getVolume method must both report the values of width, height, depth, and weight, but must also calculate and report the volume by multiplying height by width by depth. The class MatchBox must also add the method calculateWeight() that will calculate weight based upon the volume assuming that the volume is a quantity of water which weighs .03611 pounds per cubic inch. Also method calculateWeight should show the result like this: weight of MatchBox is X.
The new class must include a main method that creates a new MatchBox object, calls the getVolume method and reports the results by printing the following items to the screen (where the X is replaced by the calculated value) Assume that the value of width is 5, height is 10, and the depth is 3. The output should look like the following with X replaced with the appropriate calculated value.
width of MatchBox is X
 height of MatchBox is X
 depth of MatchBox is X
 weight of MatchBox is X
 Volume is: X
Solution
MatchBoxMain.java
import java.util.Scanner;
 public class MatchBoxMain {
  
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println(\"Enter Width: \");
        double width = scan.nextDouble();
        System.out.println(\"Enter Height: \");
        double heigh = scan.nextDouble();
        System.out.println(\"Enter Depth: \");
        double depth = scan.nextDouble();
        MatchBox m = new MatchBox(width,heigh,depth);
        m.calculateWeight();
        m.getVolume();
}
}
MatchBox.java
 public class MatchBox extends Box{
   double weight;
    public MatchBox(double w, double h, double d){
        super(w,h,d);
    }
    public void calculateWeight(){
        weight = 0.03611 * width * height * depth;
    }
    void getVolume() {
    System.out.println(\"width of MatchBox is \"+width);
    System.out.println(\"height of MatchBox is \"+height);
    System.out.println(\"depth of MatchBox is \"+depth );
    System.out.println(\"weight of MatchBox is \"+weight);
    super.getVolume();
    }
}
Box.java
public class Box {
 
 double width;
 double height;
 double depth;
 
 // This is an empty constructor
 Box() {
 }
 
 Box(double w, double h, double d) {
 width = w;
 height = h;
 depth = d;
 }
 
 void getVolume() {
 System.out.println(\"Volume is : \" + width * height * depth);
 }
 }
Output:
Enter Width:
 5
 Enter Height:
 5
 Enter Depth:
 6
 width of MatchBox is 5.0
 height of MatchBox is 5.0
 depth of MatchBox is 6.0
 weight of MatchBox is 5.4165
 Volume is : 150.0


