Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Purpose: Develop the concept of using objects. 1. Follow the directions in the p

ID: 668705 • Letter: P

Question

Purpose: Develop the concept of using objects. 1. Follow the directions in the program and make the requested code enhancements and modifications. 2. Each class should have its own file. 3. Demonstrate to the lab-instructor that your program works correctly. 4. Submit a hardcopy of your Java source file to the lab-instructor. //************************************************************** //************************************************************** public class BoxDriver { public static void main( String[] args ) { Box box1 = new Box( ); // this references the no-arg constructor Box box2 = new Box( 20., 30., 40., "green" ); // Instantiate an object of type Box and name it box3. Set the dimensions // to be height = 18, width = 36, depth = 18. The color is blue. // Instantiate an object of type Box using the copy constructor. Name the object // box4. box4 should be a deep copy of box3. // Change the box color for box1 to red: // Change the width dimension of box1 to 24. : // Display the surface area of each box and label your output by box color. // Display the total surface area of all the combined boxes: // Display the volume of each box and label the box by color: // Display the total storage volume of all the combined boxes: // Change the color of box4 to yellow and change its height to 15. // Use the Box toString method to show the state of each object. } // end main } // end class BoxDriver // declaration of The Box Class header line { // These are the declarations for the private instance fields: private String color_ ; private double height_ ; private double width_ ; private double ; // Complete this declaration correctly public Box( ) // This is the no-argument constructor. { // Set the instance fields here: // 1. Set the box color to white. // 2. Set each dimension of the box to 12.0; } // end of the no-argument constructor // Complete the next constructor; it should have formal input parameters // for the height, width, depth, and color respectively. public Box( // fill in the required code here ) { // set the instance fields using the input parameters from the header line. } // end the constructor with the formal input parameters // Write a copy constructor for the Box class here: // Write accessor-methods for each private instance field (getter-methods). // Write mutator-methods for each private instance field (setter-methods). // Write separate methods for computing the area and the volume. // Write a method named toString to return all the instance fields as a String. // Use concatenation to build the String. }// end Box class declaration

Explanation / Answer


public class BoxDriver {

   public static void main( String[] args ) {
       Box box1 = new Box( ); // this references the no-arg constructor
       Box box2 = new Box( 20.0, 30.0, 40.0, "green" );
       // Instantiate an object of type Box and name it box3. Set the dimensions
       // to be height = 18, width = 36, depth = 18. The color is blue.
      
       Box box3 = new Box(18, 36, 18, "blue");
      
       // Instantiate an object of type Box using the copy constructor. Name the object // box4. box4 should be a deep copy of box3.
       Box box4 = new Box(box3);
       // Change the box color for box1 to red: // Change the width dimension of box1 to 24.
       box1.setColor("red");
       box1.setWidth(24);
      
       // Display the surface area of each box and label your output by box color.
      
       System.out.println(" Box : "+box1.getColor()+" Total Surface Area: "+ box1.totalSurfaceArea());
       //If you mean lateral surface area use the following method
       //System.out.println(" Box : "+box1.getColor()+" Lateral Surface Area: "+ box1.lateralSurfaceArea());
      
       System.out.println(" Box : "+box2.getColor()+" Total Surface Area: "+ box2.totalSurfaceArea());
       System.out.println(" Box : "+box3.getColor()+" Total Surface Area: "+ box3.totalSurfaceArea());
       System.out.println(" Box : "+box4.getColor()+" Total Surface Area: "+ box4.totalSurfaceArea());
      
       // Display the total surface area of all the combined boxes
      
       System.out.println(" Total of Surface Areas of all the boxes: "+
                           (box1.totalSurfaceArea()+box2.totalSurfaceArea()+box3.totalSurfaceArea()+box4.totalSurfaceArea()));
      
       // Display the volume of each box and label the box by color: /
       System.out.println(" Box : "+box1.getColor()+" Volume: "+ box1.volume());
       System.out.println(" Box : "+box2.getColor()+" Volume: "+ box2.volume());
       System.out.println(" Box : "+box3.getColor()+" Volume: "+ box3.volume());
       System.out.println(" Box : "+box4.getColor()+" Volume: "+ box4.volume());
      
       // Display the total storage volume of all the combined boxes
       System.out.println(" Total of volumes of all the boxes: "+
                (box1.volume()+box2.volume()+box3.volume()+box4.volume()));

      
       // Change the color of box4 to yellow and change its height to 15.
       box4.setColor("yellow");
       box4.setHeight(15);
       // Use the Box toString method to show the state of each object
       System.out.println(box1.toString());
       System.out.println(box2.toString());
       System.out.println(box3.toString());
       System.out.println(box4.toString());
      
   }
}


public class Box {

   // Box is a cuboid or rectangular solid
  
   private String color ;
   private double height ;
   private double width ;
   private double depth;
   // Complete this declaration correctly
   public Box() {
       //   This is the no-argument constructor.
       // Set the instance fields here:
       // 1. Set the box color to white.
       this.color = "white";
       // 2. Set each dimension of the box to 12.0; }
       this.height = 12.0;
       this.width = 12.0;
       this.depth = 12.0;
       // end of the no-argument constructor
   }
  
   // Complete the next constructor; it should have formal input parameters
   // for the height, width, depth, and color respectively.
   public Box( double height, double width, double depth, String color) {
       // set the instance fields using the input parameters from the header line.
       this.color = color;
       this.height = height;
       this.width = width;
       this.depth = depth;
   }
  
   // copy constructor
    Box(Box b) {
        //System.out.println("Copy constructor called");
       this.color = b.color;
       this.height = b.height;
       this.width = b.width;
       this.depth = b.depth;
    }
  
   // Write separate methods for computing the area and the volume
  
   public double volume() {
       //volume = lbh;
       return height * width * depth;
   }
  
   public double totalSurfaceArea() {
  
          // total surface area = 2 (lb+bh+lh)
       return (2.0 * (height * width + width * depth + height * depth));
    }
  
   public double lateralSurfaceArea() {
      
          // total surface area = 2 (lb+bh+lh), where l=length/height, b = breadth/width, h = height;
       return (2.0 * (height * width + width * depth ));
}
    // Write a method named toString to return all the instance fields as a String.

   public String getColor() {
       return color;
   }

   public void setColor(String color) {
       this.color = color;
   }

   public double getHeight() {
       return height;
   }

   public void setHeight(double height) {
       this.height = height;
   }

   public double getWidth() {
       return width;
   }

   public void setWidth(double width) {
       this.width = width;
   }

   public double getDepth() {
       return depth;
   }

   public void setDepth(double depth) {
       this.depth = depth;
   }
  
   // Use concatenation to build the String. }/
  
   public String toString() {
      
       return " Box - Color : "+color+" height: "+height+" width: "+width+" depth: "+depth
               +" Total surface area: "+this.totalSurfaceArea()+" Volume: "+this.volume();
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at drjack9650@gmail.com
Chat Now And Get Quote