You work for a packaging company that makes boxes and cylindrical tubes. The Box and Tube classes both implement the Measurable interface, which defines area() and volume() methods. You can create an array holding both Tubes and Boxes, then use a for loop to compute the total area and volume by calling the interface methods on each object. Sample code is provided implementing the Tube and Box classes with their area and volume calculations.
Measurable Interface Calculates Areas and Volumes of Boxes and Tubes
1. You still work for packaging company that makes boxes and
cylindrical tubes for shipments. The classes have been written
for you and your job is to show how they work. The box class
and the tube class both implement the Measurable interface.
Because of the Measure able interface both of the classes (box
and tube) have methods for area () and volume () each of which
returns the appropriate calculation. Show how you can create an
array that holds both tubes and boxes and how you can use a
single for loop to compute the total area and volume of the
shipping containers being used. Assume that each class has the
necessary constructors (box takes a single double and tube takes
two doubles). If you make any other assumptions be sure to
state them.
Solution
Interface Measurable:
interface Measurable {
public void tubeArea(double radius,double height);
public void tubeVolume(double radius,double height);
public void boxArea(double length,double width);
public void boxVolume(double length,double width,double
height);
}
Class Tube:
public class Tube implements Measurable{