Why do I keep failing this test?
Test Cube(1.0, 1.0, 1.0) sets the type to "Cube",
width, length and height each to 1.0 Test feedback
Expected: Cube, 1.0, 1.0, 1.0
Yours: Rectangle, 1.0, 1.0, 1.0
I need the cube class to be able to set its type to Cube. Right now it seems to be setting itself to Rectangle. My main fills an Array of Shapes, I then have methods to count the different types of shapes according to the Type of each shape. How can I define Cube as a shape, when my rectangle class already extends shape, but I MUST have my Cube class extend rectangle. It must because I need to have access to the area as well and the length and width.
I also have a very simple interface that is implemented by my cube. It is just to find the volume. Can I somehow utilize my interface to override the type?
Can't find an answer on StackOverflow to this specific question.
Here is my Rectangle class
public class Rectangle extends Shape {
protected double width;
protected double length;
public Rectangle(double width, double length) {
super("Rectangle");
setWidth(width);
setLength(length);
}// end ctr
public final double getWidth () {return width; }
public final double getLength () {return length;}
public final void setWidth (double width) {
if (width < 0) {
System.out.println("Value could not be updated due to negative double.");
}else
this.width = width;
}// end width setter
public final void setLength (double length) {
if (length < 0) {
System.out.println("Value could not be updated due to negative double.");
}else
this.length = length;
}// end length setter
@Override
public double area() {
return length * width;
}// end area method
public double perimeter() {
return 2 * (length + width);
}// end perimeter method
@Override
public String toString() {
String str = "";
str += String.format("%10s", "Rectangle:") + " ";
str += "width: " + String.format("%.1f", width) + ", " + "length: " + String.format("%.1f", length);
str += ", " + "area: " + String.format("%.2f", area() ) + ", ";
str += "perimeter: " + String.format("%.2f", perimeter() );
return str;
}// end descriptor
}// end rect class
Here is my Cube class
public class Cube extends Rectangle implements Shape3D {
protected double height;
public Cube (double width, double length, double height) {
super(width, length);
setHeight(height);
}// end ctr
public final double getHeight () {return height;} // end get height
public final void setHeight (double height) {
if (height < 0) {
System.out.println("Value could not be updated due to negative double.");
}else
this.height = height;
}// end set height
@Override
public double volume () {
return super.area() * height;
} // end volume method
@Override
public String toString() {
String str = "";
str += String.format("%10s", "Cube:") + " ";
str += "width: " + String.format("%.1f", width) + ", " + "length: " + String.format("%.1f", length);
str += ", " + "area: " + String.format("%.2f", area() ) + ", ";
str += "perimeter: " + String.format("%.2f", perimeter() );
str += ", height: " + String.format("%.1f", height );
str += ", volume: " + String.format("%.1f", volume() );
return str;
}// end descriptor
}// end cube class
The Rectangle class comes from my Shape class,
public abstract class Shape {
protected String type;
public Shape (String type) {
setType(type);
}// end ctr
public final String getType () {return type; }
public final void setType (String type) {this.type = type;}
public abstract double area (); // end prototype
public double getArea () {return area();}
@Override
public String toString() {
String str = "";
str += type;
return str;
}// end descriptor
}// end shape class
Your cube class uses super to call rectangle's constructor which in turn calls the
Shape class's constructor with the string "Rectangle", basically as you have it, the cube constructor will not allow you to set its type of cube. You will need to explicitly use the setType
method.
You could in cube's constructor add the line
this.setType("Cube");
and it should work (have not tested).