Here is the GeometricObject: // GeometricObject.java: The abstract GeometricObje
ID: 663803 • Letter: H
Question
Here is the GeometricObject:
// GeometricObject.java: The abstract GeometricObject class
public abstract class GeometricObject {
private String color = "white";
private boolean filled;
/**Default construct*/
protected GeometricObject() {
}
/**Construct a geometric object*/
protected GeometricObject(String color, boolean filled) {
this.color = color;
this.filled = filled;
}
/**Getter method for color*/
public String getColor() {
return color;
}
/**Setter method for color*/
public void setColor(String color) {
this.color = color;
}
/**Getter method for filled. Since filled is boolean,
so, the get method name is isFilled*/
public boolean isFilled() {
return filled;
}
/**Setter method for filled*/
public void setFilled(boolean filled) {
this.filled = filled;
}
/**Abstract method findArea*/
public abstract double getArea();
/**Abstract method getPerimeter*/
public abstract double getPerimeter();
}
Explanation / Answer
/**
* Enhanced Circle class with comparable interface implemented.
*
* UML Diagram, per assignment:
* +--------------------------------------------------------------+
* | GeometricObject |
* | + p color |
* | + p filled |
* | + p dateCreated |
* | + m getColor |
* | + m setColor |
* | + m isFilled |
* | + m setFilled |
* | + m getDateCreated |
* | + m toString |
* | |
* | Comparable |
* | + m compareTo |
* | |
* | Circle |
* | - [extends GeometricObject] |
* | - [implements Comparable] |
* | + p radius |
* | + o compareTo |
* | + o equals |
* | + m getRadius |
* | + m setRadius |
* | + m setArea |
* | + m getArea |
* | + m getDiameter |
* | + m getPerimeter |
* | + m printCircle |
* +--------------------------------------------------------------+
*/
public class Circle extends GeometricObject implements Comparable {
private double radius;
/**
* Default constructor.
*/
public Circle() {
}
/**
* Constructor.
*
* @param double initial size of the object's radius.
*/
public Circle(double rad) {
this.radius = rad;
}
/**
* Accessor.
*
* @return double the object member, this.radius
*/
public double getRadius() {
return this.radius;
}
/**
* Mutator for the object member this.raius.
*
* @param double the new radius for this object.
*/
public void setRadius(double rad) {
this.radius = rad;
}
/**
* Compute the area of this object.
*
* @return double the Circle's area.
*/
public double getArea() {
return this.radius * this.radius * Math.PI;
}
/**
* Compute the diameter of this object.
*
* @return double the diameter of this Circle.
*/
public double getDiameter() {
return 2 * this.radius;
}
/**
* Compute the perimeter of this object.
*
* @return double the perimeter of this Circle.
*/
public double getPerimeter() {
return 2 * this.radius * Math.PI;
}
/**
* Implementation of equals().
* NOTE: Based on the Circles' radius.
*/
public boolean equals(Object obj) throws Error {
//@TODO: is there a not_null() built-in in Java?
//if (obj instanceof Circle && ((Circle) obj).radius != null) {
if (obj instanceof Circle) {
if (((Circle) obj).radius == this.radius) {
return true;
}
else {
return false;
}
}
else {
java.util.Formatter err = new java.util.Formatter();
err.format("You cannot compare the value of two non-like objects; must use an object of the %s Class, not the %s Class.",
this.getClass().getName(), obj.getClass().getName());
throw new Error(err.toString());
}
}
/**
* Implementation of compareTo().
* NOTE: Based on the Circles' computed area.
*/
public int compareTo(Object obj) throws Error {
if (obj instanceof Circle) {
double thisArea = this.getArea();
double otherArea = ((Circle) obj).getArea();
if (thisArea == otherArea) {
//objects are the same size
return 0;
}
else if (thisArea < otherArea) {
//this object is smaller
return -1;
}
else {
//this object is larger
return 1;
}
}
else {
java.util.Formatter err = new java.util.Formatter();
err.format("You cannot compare the value of two non-like objects; must use an object of the %s Class, not the %s Class.",
this.getClass().getName(), obj.getClass().getName());
throw new Error(err.toString());
}
}
/**
* Print the text information about this object, an even _higher_ level
* toString().
*/
public void printCircle() {
System.out.printf("This circle was created: %s and the radius is: %.2f",
getDateCreated(), this.radius);
}
//main to main compiler happy
public static void main(String argv[]) {
}
}
}
public class ObjectCompare {
/**
* Returns the largest object in an array of objects.
*
* @param array a set of Objects to be searched
* @return Object the largest object in the set.
*/
public static Object max(Object[] a) throws Error {
Object largest = a[0];// = new Object();
//process each object
for (int i = 0; i < a.length; i++) {
//sanity check:
if (!(a[i] instanceof Comparable)) {
java.util.Formatter err = new java.util.Formatter();
err.format("Object #%.0f of the array being queried is not a child of Comparable.", ++i);
throw new Error(err.toString());
}
//code
if (((Comparable) a[i]).compareTo(largest) > 0) {
largest = a[i];
}
}
return largest;
}
}
public class TestCircle {
//@TODO: find out boolean syntax for Formatter
public static void main(String argv[]) {
double baseRad = 4;
System.out.printf("Testing compareTo() of two Circle object. ");
Circle circ = new Circle(baseRad);
System.out.printf(" called circ1 = Circle(%.0f); circ2 = Circle(4). ", baseRad);
Circle sameObj = new Circle();
System.out.println(" circ1.compareTo(circ2) = "+ circ.compareTo(sameObj));
System.out.printf(" called circ1 = Circle(%.0f); circ2 = Circle(3). ", baseRad);
Circle diffObj = new Circle();
System.out.println(" circ1.compareTo(circ2) = "+ circ.compareTo(diffObj));
System.out.printf("Testing equals() of Circle object, circ1, and Object object. ");
Object genericObj = new Object();
circ.compareTo(genericObj);
}
}
public class ObjectTest {
public static void main(String[] argv) {
int i;
int aSize = 10;
//test strings
String[] obj1 = new String[aSize];
System.out.printf("Testing with strings ");
for (i = 0; i < obj1.length; i++) {
obj1[i] = "" + i;
System.out.printf(" ...creating string object #%d of %d: %s ", i+1, obj1.length, obj1[i].toString());
}
Object stringTest = ObjectCompare.max(obj1);
System.out.printf(" largest object is: %s ", stringTest.toString());
//test integers
Integer [] obj2 = new Integer[aSize];
System.out.printf("Testing with integers ");
for (i = 0; i < obj2.length; i++) {
obj2[i] = i;
System.out.printf(" ...creating integer object #%d of %d: %d ", i+1, obj2.length, obj2[i]);
}
Integer intTest = (Integer) ObjectCompare.max(obj2);
System.out.printf(" largest object is: %s ", intTest.toString());
//test dates
System.out.printf("Testing with dates ");
java.util.Date[] obj3 = new java.util.Date[aSize];
for (i = 0; i < obj3.length; i++) {
obj3[i] = new java.util.Date();
System.out.printf(" ...creating Date object #%d of %d: %s (epoch: %d) ", i+1, obj3.length, obj3[i].toString(), (int) obj3[i].getTime());
}
java.util.Date dateTest = (java.util.Date) ObjectCompare.max(obj3);
System.out.printf(" largest object is: %s ", (String) dateTest.toString());
System.out.printf(" largest object is: %d ", (int) dateTest.getTime());
}
}