I'm self-studying Java and have a question about one of the end-of-chapter exercises. The chapter focus is on inheritance and the book hasn't officially introduced polymorphism so I'm trying to stay within those bounds.
The exercise is:
Write an inheritance hierarchy for classes Quadrilateral, Trapezoid, Parallelogram, Rectangle and Square. Use Quadrilateral as the superclass of the hierarchy. Create and use a Point class to represent the points in each shape. Make the hierarchy as deep(i.e., as many levels) as possible. Specify the instance variables and methods for each class. The private instance variables of Quadrilateral should be the x-y coordinate pairs for the four endpoints of the Quadrilateral. Write a program that instantiates objects of your classes and outputs each object's area(except Quadrilateral).
My classes are below and they compile and work OK.
Questions:
The exercise statement
The private instance variables of Quadrilateral should be the x-y coordinate pairs for the four endpoints of the Quadrilateral.
I put the x-y coordinate instance variables in my Point class because it seemed to fit better and if I didn't, what purpose would Point serve other than to hold the four points. With the way that I did it, it also calculates the distance between the points which seems to fit better to me. Comments?
I thought it would make more sense to use Point class inside Square directly instead of going through Quadrilateral like I did. I used Quadrilateral because each sub-class will need points. Taking Point out of Quadrilateral would make it have even less functionality than it does. Of course it doesn't make sense to add a class just for the sake of adding a class but I'm kind of in that position by trying to meet the exercise objectives. Comments?
Any other general feedback would be welcome. Does it look professional? Even though it's kind of small, would it be accepted in the corporate world?
package exercise9_8;
public class Point {
private int distanceX, distanceY;
private int x0, x1, y0, y1;
public Point( int x0, int x1, int y0, int y1 ){
this.x0 = x0;
this.x1 = x1;
this.y0 = y0;
this.y1 = y1;
setDistanceX();
setDistanceY();
}
private void setDistanceX(){
distanceX = x1 - x0;
}
public int getDistanceX(){
return distanceX;
}
private void setDistanceY(){
distanceY = y1 - y0;
}
public int getDistanceY(){
return distanceY;
}
}
package exercise9_8;
public class Quadrilateral {
Point point;
public Quadrilateral( int x0, int x1, int y0, int y1 ){
point = new Point( x0, x1, y0, y1 );
}
protected int getDistanceX(){
return point.getDistanceX();
}
protected int getDistanceY(){
return point.getDistanceX();
}
}
package exercise9_8;
public class Square extends Quadrilateral{
private int area;
public Square( int x0, int x1, int y0, int y1 ){
super( x0, x1, y0, y1 );
setArea();
}
private void setArea(){
area = super.getDistanceX() * super.getDistanceY();
}
public int getArea(){
return area;
}
}
package exercise9_8;
import org.testng.annotations.Test;
import static org.testng.Assert.*;
public class SquareTestNG {
@Test(enabled = true)
public void testGetNumberOfPoints(){
Square sq = new Square( 5, 3, 5, 3 );
assertEquals(sq.getArea(), 4);
}
}