1
Student Name: __________________
Class and Section __________________
Total Points (10 pts) __________________
Due: October 9, 2009 before the class
Project: Enabling GeometricObject comparable
CSCI 1302 Advanced Programming Principles
Armstrong Atlantic State University
Problem Description:
(Enabling GeometricObject comparable) Modify the
GeometricObject class to implement the Comparable
interface, and define a static max method in the
GeometricObject class for finding the larger of two
GeometricObject objects.
Design:
Draw the UML class diagram here
Coding: (Copy and Paste Source Code here. Format your code using Courier 10pts)
2
public class Test {
// Main method
public static void main(String[] args) {
// Create two comparable circles
Circle1 circle1 = new Circle1(5);
Circle1 circle2 = new Circle1(4);
abstract class GeometricObject1 implements Comparable {
// Implement it
}
// Circle.java: The circle class that extends GeometricObject
class Circle1 extends GeometricObject1 {
// Implement it
}
Submit the following items:
1. Print this Word file and Submit to me before the class on the due day.
Solution Code:
public class Test {
// Main method
// Construct a geometric object
protected GeometricObject1(String color, double weight) {
this.color = color;
this.weight = weight;
}
4
public static Comparable max(Comparable o1, Comparable o2) {
if (o1.compareTo(o2) > 0)
return o1;
else
return o2;
}
// Construct a circle with specified radius, weight, and color
public Circle1(double radius, String color, double weight) {
super(color, weight);
this.radius = radius;
}
// Getter method for radius
public double getRadius() {
return radius;
}
5
}
// Implement the compareTo method defined in Comparable
public int compareTo(Object o) {
if (getRadius() > ((Circle1)o).getRadius())
return 1;