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

We Pack N Ship 4 U packs and ships items using two kinds of containers: boxes an

ID: 3689271 • Letter: W

Question

We Pack N Ship 4 U packs and ships items using two kinds of containers: boxes and mailing tubes (cylinders). Rates are determined by the size of the container. The size of a box is the sum of its three dimensions: length, width, and depth, in inches. For a mailing tube, with radius r inches and length l, size is calculated as 2pir + l. The cost of packing and shipping a box is $0.35 times the size of the cube. For a tube, the cost is $0.25 times the size of the container. Define an abstract class Container with a single instance variable, double length, two abstract methods, double getsize() and double getCost(), and one getter method double getLength(). Container should also implement Comparable based on cost. Next, create two subclasses of Container, Box and Tube, that implement getCost() and getSize(), where getCost() returns the cost of packing and shipping and getSize() returns the size of a container, as previously described. Box needs additional instance variables width and depth, and Tube requires radius. Include getter methods for Box and Tube. Finally, implement a TestContainer class that accepts 10 Container objects and stores them in an array. For each container, TestContainer should ask whether the container is a box or a tube and prompt for the appropriate dimensions. Sort the 10 containers in ascending order by cost. Print the type of container, the dimensions of the container, and the cost, rounded to two decimal places.

Explanation / Answer

Hi I have implemented all classes and functionality and test class. Please enter your data and test it.

import java.util.Arrays;
import java.util.Scanner;

// container class
public abstract class Container implements Comparable<Container> {

   private double length;
  
   public Container(double length) {
       this.length = length;
   }

   public abstract double getSize();
  
   public abstract double getCost();
  
   public double getLength(){
       return length;
   }
  
   @Override
   public int compareTo(Container o) {
       if(this.getCost() < o.getCost())
           return -1;
       else if(this.getCost() > o.getCost())
           return 1;
       else return 0;
   }
}

// Box calss
class Box extends Container{
  
   private double width;
   private double depth;
  
   public Box(double length, double width, double depth){
       super(length);
       this.width = width;
       this.depth = depth;
   }
   @Override
   public double getSize() {
       return width+depth+getLength();
   }

   @Override
   public double getCost() {
       return 0.35*getSize();
   }
  
   public double getWidth(){
       return width;
   }
  
   public double getDepth(){
       return depth;
   }
}

// Tube class
class Tube extends Container{
  
   public Tube(double length, double radius) {
       super(length);
       this.radius = radius;
   }

   private double radius;

   @Override
   public double getSize() {
       return 2*Math.PI*radius + getLength();
   }

   @Override
   public double getCost() {
       return 0.25*getSize();
   }
  
   public double getRadius(){
       return radius;
   }
}

// Test class
class TestContainer{
  
   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
      
       // creating attay of Container object pof size 10
       Container[] containers = new Container[10];
       int choice;
      
       // reading user inputs
       for(int i=0; i<10; i++){
           System.out.print("Enter 1. Box, 2. Tube: ");
           choice = sc.nextInt();
           if(choice == 1){
               System.out.println("Enter length, width and depth: ");
               double length = sc.nextDouble();
               double width = sc.nextDouble();
               double depth = sc.nextDouble();
              
               containers[i] = new Box(length, width, depth); // creating Box object
           }
           else if(choice == 2){
               System.out.println("Enter length and radius: ");
               double length = sc.nextDouble();
               double radius = sc.nextDouble();
              
               //creating Tube objectt
               containers[i] = new Tube(length, radius);
           }
       }
      
       // sorting based on cost
       Arrays.sort(containers);
      
       //print array
       for(Container container: containers){
           System.out.println(container.getClass().getName());
           System.out.println("   Size: "+String.format(".2f", container.getSize()));
           System.out.println("   Cost: "+String.format(".2f", container.getCost()));
          
       }
   }
}

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