Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Working with super in java

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;}
}
like image 364
HoodCoolege Avatar asked Apr 11 '16 14:04

HoodCoolege


4 Answers

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 Rectangles 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. Cubes and Rectangles 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 Rectangles are Points.

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 Points.

see

+--
| |
--+

If you have the opposite corner Points (marked here by the +) you can draw your Rectangle.

In light of this, perhaps you should also refactor Rectangle to have a pair of Points as members.

something like:

public class Rectangle {
    private Point firstCorner;
    private Point secondCorner;

    ...
}
like image 133
Blake Yarbrough Avatar answered Nov 15 '22 20:11

Blake Yarbrough


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 ;)

like image 40
hris.to Avatar answered Nov 15 '22 18:11

hris.to


The superclass of Rectangle is Cube not Point. Using super() you can call the constructor of Cube not the constructor of Point

like image 2
Plebios Avatar answered Nov 15 '22 20:11

Plebios


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.

like image 2
Prudhvi Avatar answered Nov 15 '22 18:11

Prudhvi