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

s. (10 marks) You work for packaging company that makes boxes and cylindrical tu

ID: 3915586 • Letter: S

Question

s. (10 marks) You work for packaging company that makes boxes and cylindrical tubes for shipments. The classes n for you and your job is to show how they work. The Box class and the Tube class both that makes implement the Package interface shown below public interface Package public double area0 public double vol ume 0 w to compute the total area and volume of the shipping containers being used. Assume that each class has the essary constructors (box takes three doubles for length, width and height and a tube takes two doubles for a. (6 marks) Show h ow you can create an array that holds both tubes and boxes and how you can use a single for eight and radius). If you make any other assumptions be sure to state them.

Explanation / Answer

a) We can have array of interface Package
   Package[] data = new Package[10]
   Now we can create 10 objects of mix type.Some are boxes and and
   some are rectangles. We can store these objects in this array.

   Here i is the index in the for loop
   data[i] = new Box(12,13,14);
   or
   data[i] = new Tube(10,5);

b) If we want to have average diameter of all the tubes in the array. The problem
   we face is we don't know with the reference of interface object whether it
   is a Box or Tube to invoke the method of getDiameter().
   We can use instanceof to know the class type.

   int sum = 0;
   for (int i = 0; i<10; i++){
       if (data[i] instanceof Tube){
          Tube t = (Tube)data[i];
          sum = sum + t.getDiameter();
       }
   }
   double avgdiameter = sum/10.0