HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

My self-study inheritance and sub-class exercise

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
studysubexerciseinheritanceandclassself

Problem

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 exercis

Solution

First of all, I'd change how the Point class functions because like David Harkness said, your Point class resembles that of a Line. I would have it representing two values (x, y) and include functions that would make it easier to find the area of a shape. Here's an example:

public class Point {

        public float x;
        public float y;

        public Point(float x, float y) {
            this.x = x;
            this.y = y;
        }

        public static float distanceTo(Point from, Point to) {
            // Distance formula
            return Math.sqrt(Math.pow(to.x - to.y, 2) + Math.pow(from.x - from.y, 2));
        }


Using that point class, I could implement it into a Shape class to centralize a getArea() function. To have different types of shapes, you could extend the Shape class from other classes and specify how many points each class or override the getArea() function.

Code Snippets

public class Point {

        public float x;
        public float y;

        public Point(float x, float y) {
            this.x = x;
            this.y = y;
        }

        public static float distanceTo(Point from, Point to) {
            // Distance formula
            return Math.sqrt(Math.pow(to.x - to.y, 2) + Math.pow(from.x - from.y, 2));
        }

Context

StackExchange Code Review Q#24687, answer score: 2

Revisions (0)

No revisions yet.