1. Define an abstract class Container that implements the interface Comparable<C
ID: 3634452 • Letter: 1
Question
1. Define an abstract class Container that implements the interface Comparable<Container>:• Container has a single attribute (a protected instance variable) double length
• It has abstract methods/behaviors (that is, just method signatures, no method bodies):
– public abstract double getSize();
– public abstract double getCost();
• It has these concrete (that is, fully implemented) methods/behaviors:
– public double getLength() – a getter method that returns the value of length
– public void print() – a method that prints the Container’s length, size, and cost
– public int compareTo(Container c) – see Chapter 12, section 10.5 for more info
• compareTo should compare two Containers based on their cost
• Note that it's OK to have concrete methods compareTo(…) & print() use abstract getters
2. Create two concrete subclasses of Container, Box and Tube, that define getCost() and getSize(), where getCost() returns the cost of packing and shipping and getSize() returns the size of the container as explained above. Box and Tube have additional instance variables and methods; write constructors for both of these subclasses that set all their instance variables:
– Private Box attributes: double width, double depth, and, inherited, double length
– Box methods: double getWidth(), double getDepth() + inherited Container methods
– Private Tube attributes: double radius and, inherited, double length
– Tube methods: double getRadius() + inherited Container methods
3. Write a separate TestContainer class that reads in information about 5 Container objects from the keyboard and stores them in a Container[] array of size 5. The main() method of TestContainer should prompt the user 5 times for the type of each Container, Box or Tube, and then prompt for all other appropriate information depending on the chosen Container type. Once you have read all the required information, use either a Box or a Tube constructor to create an object and store it into the Container array at the next index (0 through 4).
Explanation / Answer
Hi. part 2 of the question mentions how to implement getSize() and getCost() "as explained above", but nothing is above. It seems to be missing info on what the size stands for so I assumed it's computing the volume: length by width by depth for a bow and length by radius squared times Pi for the tube. As for cost, shipping and packaging are not mentioned as attributes nor constants so I just set the method to return a cost of size * $1.5 for box and size * $1 for tube. You'll have to modify that to fit with the missing instructions on how the cost is computed.
In the main method, I added 2 extra operations: sort the containers according to their compareTo implementation, and loop through them to display each one. But these are optional - I marked them as such with a comment.
public abstract class Container implements Comparable<Container> {
// attribute
protected double length;
// abstract methods
public abstract double getSize();
public abstract double getCost();
// concrete methods
public double getLength()
{
return length;
}
public void print()
{
System.out.println(" Length" + getLength()
+ " Size: " + getSize()
+ " Cost: " + getCost()
);
}
// implement compareTo from Comparable Interface
@Override
public int compareTo(Container c)
{
int comparison = 0;
if(getCost() < c.getCost())
comparison = -1;
if(getCost() == c.getCost())
comparison = 0;
if(getCost() > c.getCost())
comparison = 1;
return comparison;
}
}
public class Box extends Container {
private double width;
private double depth;
public Box(double len, double wid, double dep)
{
length = len;
width = wid;
depth = dep;
}
// Box getter methods
double getWidth()
{
return width;
}
double getDepth()
{
return depth;
}
// Container-inherited methods to implement
@Override
public double getSize() {
return (length*depth*width);
}
@Override
public double getCost() {
return (getSize() * 1.5);
}
}
public class Tube extends Container {
private double radius;
public Tube(double len, double rad)
{
length = len;
radius = rad;
}
// Tube-specific getter method
public double getRadius()
{
return radius;
}
// Container-inherited methods to implement
@Override
public double getSize() {
return (3.14*length*radius*radius);
}
@Override
public double getCost() {
return (getSize() * 1.0);
}
}
import java.util.Scanner;
public class TestContainer {
public static void main(String[] args) {
// scanners for text and numeric input
Scanner textInput = new Scanner(System.in);
Scanner numInput = new Scanner(System.in);
// array of 5 containers
Container[] array = new Container[5];
for(int i = 0; i < array.length; i++)
{
System.out.println("Information for Container " + (i+1));
System.out.print("Box or Tube? ");
String type = textInput.next();
if(type.equalsIgnoreCase("Box"))
{
System.out.print("Enter length: ");
double length = numInput.nextDouble();
System.out.print("Enter width: ");
double width = numInput.nextDouble();
System.out.print("Enter depth: ");
double depth = numInput.nextDouble();
Box box = new Box(length, width, depth);
array[i] = box;
}
else if(type.equalsIgnoreCase("Tube"))
{
System.out.print("Enter length: ");
double length = numInput.nextDouble();
System.out.print("Enter radius: ");
double radius = numInput.nextDouble();
Tube tube = new Tube(length, radius);
array[i] = tube;
}
else
{
System.out.println("Unsupported Container Type. Sorry!");
i--;
}
} // end of user input for-loop
// ********* OPTIONAL **************
// sort the containers by Cost
java.util.Arrays.sort(array);
// loop through the containers and display them
for(int i = 0; i < array.length; i++)
{
array[i].print();
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.