Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Name : Kyle Blanchard
- // Due : 11/7/2014
- // Class : CSCI-401
- // Assignment : Chapter 15 Homework: Enabling GeometricObject Comparable Interface
- // Contact : Kwblanchard@student.stcc.edu
- // package csci401;
- public class TestComparable {
- public static void main(String[] args) {
- Circle1 circle1 = new Circle1(5);
- Circle1 circle2 = new Circle1(4);
- Circle1 circle = (Circle1) GeometricObject1.max(circle1, circle2);
- System.out.println("The max circle's radius is " + circle.getRadius());
- System.out.println(circle);
- }
- }
- abstract class GeometricObject1 implements Comparable<GeometricObject1> {
- private String color = "white";
- private boolean filled;
- private java.util.Date dateCreated;
- protected GeometricObject1() {
- dateCreated = new java.util.Date();
- }
- protected GeometricObject1(String color, boolean filled) {
- dateCreated = new java.util.Date();
- this.color = color;
- this.filled = filled;
- }
- public String getColor() {
- return color;
- }
- public void setColor(String color) {
- this.color = color;
- }
- public boolean isFilled() {
- return filled;
- }
- public void setFilled(boolean filled) {
- this.filled = filled;
- }
- public java.util.Date getDateCreated() {
- return dateCreated;
- }
- public String toString() {
- return "created on " + dateCreated + "\ncolor: " + color
- + " and filled: " + filled;
- }
- public abstract double getArea();
- public abstract double getPerimeter();
- public static GeometricObject1 max(GeometricObject1 a, GeometricObject1 b) {
- return (a.getArea() > b.getArea() ? a : b);
- }
- }
- class Circle1 extends GeometricObject1 {
- private double radius;
- public Circle1() {
- }
- public Circle1(double radius) {
- this.radius = radius;
- }
- public double getRadius() {
- return radius;
- }
- public void setRadius(double radius) {
- this.radius = radius;
- }
- @Override
- public double getArea() {
- return radius * radius * Math.PI;
- }
- public double getDiameter() {
- return 2 * radius;
- }
- @Override
- public double getPerimeter() {
- return 2 * radius * Math.PI;
- }
- public String toString() {
- return "The radius is " + radius;
- }
- public int compareTo(GeometricObject1 other) {
- Circle1 circleB = (Circle1) other;
- if (this.radius > circleB.radius) {
- return 1;
- } else if (this.radius == circleB.radius) {
- return 0;
- } else
- return -1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement