/** * Author: Josiah Yoder et al. * Class: SE1021-031 * Lesson: Week 3, Day 2 */ package example3_2; /** * A Circle class * * Here we add the greaterThan method */ public class Circle extends Shape implements CanBeCompared { private double radius; /** * Construct a new Circle. * * @param centerX the x (horizontal) component of the center position * measured in pixels * @param centerY the y (vertical) component * measured in pixels * @param radius the radius of the circle * measured in pixels * * Please note: This example does not compile because * the Circle class can't make a default shape * (there is no default Shape constructor) * * @author Josiah Yoder * @version 2013-12-6 */ public Circle(double centerX, double centerY, double radius) { super(centerX, centerY); this.radius = radius; /* Potential Solutions: * 1. Make a default constructor * 2.a. (doesn't work) this keyword * 2.b. (works) Use super keyword to call constructor from previous class */ // Alternative set approaches: // this.setCenter(centerX,centerY); // super.setCenter(centerX,centerY); // setCenter(centerX,centerY); } /** * Prints "Circle with radius XX.X and center (XX.X, XX.X)" * @return */ public String toString() { // TODO: // 1. Implement this method (except for printing center) // 2. How to print the private center variables? // return "("+super.centerX+", "+" ..."; return "Circle with radius " + radius + " and " + ((Drawable)this).toString(); } public boolean equals(Object obj) { Circle c = (Circle) obj; // ERROR: Used obj (Object) which doesn't know' about centerX // return this.radius == obj.radius && this.centerX == obj.centerX // && this.centerY == obj.centerY; // Fix the error by using c, a reference that points // to the same place, but is of type "Circle reference" // instead of typ "Object reference" return this.radius == c.radius && this.centerX == c.centerX && this.centerY == c.centerY; // TODO: Do this the right way so we don't need access to the // parent class. } /** * One circle is greater than another if it * has a larger radius, diameter, area, etc. * * @param obj the other comparable object * @return */ @Override public boolean greaterThan(CanBeCompared obj) { Circle cOther = (Circle)obj; return this.radius > cOther.radius; } }