Geometry classes reconsidered

Starting in Modeling geometry objects: Rectangles you implemented rectangle and circle related classes. You may have observed translation related parameters to be shape independent.

Our Rectangle and Circle instances are being described by width, height and radius. Implementing a drawing application requires arbitrary objects to be moved from one position to another e.g.:

Figure 481. Moving shapes Slide presentation Create comment in forum
Moving shapes

We might implement translation (move) related parameters and methods in both classes Rectangle and Circle independently. But since the underlying operation is indeed shape independent we choose a different approach using inheritance in the subsequent exercises.

exercise No. 155

Defining a Shape class hierarchy Create comment in forum

Q:

Our two geometric primitives circle and rectangle will need a reference point (x,y) in order to allow for defining move(...) operations. We choose the center of gravity both for rectangles and circles:

We thus need two additional parameters x and y representing an object's position. Consider the following inheritance diagram and implement the three Java classes Shape, Circle and Rectangle:

Create unit tests checking for correct implementation of the translation example from Figure 481, “Moving shapes ” and the all other methods.

Why is move(...) being defined returning an instance of class Shape?

Tip

Think of multiple / combined operations.

A:

Defining move(...) returning an instance of class Shape allows for operation chaining:

final Circle c = new Circle(1., 2., 3.0);
double p = c.move(1, 5).move(-3, 7).getPrimeter();