We know how to compute the distance of a rectangle and a dot to the origin. For circle, we compromise and compute the distance of the center to the origin. Because we will have this method for every shape, we notify the abstract class AShape of its availability. By defining an abstract method we require that every subclass implements this method. Therefore, we only need a purpose statement for the abstract method.
// compute the distance of this Shape to the origin abstract int distTo0();
We develop each method separately, but show them here concurrently.
AShape c1 = new Circle(new Posn(10, 20), 15); AShape c2 = new Circle(new Posn(40, 20), 10); AShape r1 = new Rect(new Posn(10, 20), 20, 40); AShape r2 = new Rect(new Posn(40, 20), 50, 10); AShape d1 = new Dot(new Posn(10, 20)); AShape d2 = new Dot(new Posn(40, 20)); ... c1.distTo0() -- expected: 30 c2.distTo0() -- expected: 60 r1.distTo0() -- expected: 30 r2.distTo0() -- expected: 60 d1.distTo0() -- expected: 30 d2.distTo0() -- expected: 60
There is a separate template for each of the three subclasses.
/* TEMPLATE for the Circle class: int distTo0() { ... this.center ... ... this.center.x ... ... this.center.y ... ... this.radius ... } */