For the Cube
class, i am trying to get rid of the error:
Cube.java:12: error: constructor Rectangle in class Rectangle cannot be applied to given types;
super(x, y);
^
required: int,int,double,double
found: int,int.......
I know each face of a Cube is a Rectangle whose length and width need to be the same as the side of a Cube but I am not sure what needs to be passed to the Rectangle constructor to make its length and width be the same as the side of a Cube.
also trying to calculate the volume which is the area of the rectangle times the length of the Cubes sides
This is the Cube class
// ---------------------------------
// File Description:
// Defines a Cube
// ---------------------------------
public class Cube extends Rectangle
{
public Cube(int x, int y, int side)
{
super(x, y);
side = super.area(); // not sure if this is right
}
public int getSide() {return side;}
public double area() {return 6 * super.area();}
public double volume() {return super.area() * side;}
public String toString() {return super.toString();}
}
and this is the rectangle class
// ---------------------------------
// File Description:
// Defines a Rectangle
// ---------------------------------
public class Rectangle extends Point
{
private int x, y; // Coordinates of the Point
private double length, width;
public Rectangle(int x, int y, double l, double w)
{
super(x, y);
length = l;
width = w;
}
public int getX() {return x;}
public int getY() {return y;}
public double getLength() {return length;}
public double getWidth() {return width;}
public double area() {return length * width;}
public String toString() {return "[" + x + ", " + y + "]" + " Length = " + length + " Width = " + width;}
}
The very construction of this Object seems to miss the idea of extension.
A Cube
is not a Rectangle
. A Cube
could be considered a composite of multiple Rectangle
s with spatial data for orientation, but then the rectangles should be members (read attributes/fields) of the Cube
.
To illustrate this point consider the difference between the following statements.
All cubes are rectangles.
All cats are animals.
you could conceivably create a Cat
object that extends the super class Animal
. Cube
s and Rectangle
s do not share this relationship.
consider refactoring your code to be something like:
public class Cube {
private List<Rectangle> faces:
....
}
To take this a step further, not all Rectangle
s are Point
s.
A point is one pair of x, y co-ordinates. In order to accurately draw a Rectangle
the minimum amount of information required is two Point
s.
see
+--
| |
--+
If you have the opposite corner Point
s (marked here by the +) you can draw your Rectangle
.
In light of this, perhaps you should also refactor Rectangle
to have a pair of Point
s as members.
something like:
public class Rectangle {
private Point firstCorner;
private Point secondCorner;
...
}
The problem is that Rectangle
constructor expects 4 arguments, and you are passing him only 2 when creating instance of Cube
. You should pass your length also:
public Cube(int x, int y, int side)
{
super(x, y, side, side);
}
If you think a bit it makes perfect sense - any rectangle needs origin x and y, and width and height. In case of cube width and height are the same, but you should still tell the rectangle what are their values ;)
The superclass of Rectangle
is Cube
not Point
. Using super()
you can call the constructor of Cube
not the constructor of Point
The number of arguments in your super(x, y)
doesn't match with the arguments in the constructor of class Rectangle
. When you explicitly invoke super class constructor you have to make sure that the super class has a constructor that matches.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With