Advertisement
Giftednarwhals

hw # 13

Nov 7th, 2014
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.41 KB | None | 0 0
  1. // Name         :   Kyle Blanchard
  2. // Due          :   11/7/2014
  3. // Class        :   CSCI-401
  4. // Assignment       :   Chapter 15 Homework: Enabling GeometricObject Comparable Interface
  5. // Contact      :   Kwblanchard@student.stcc.edu
  6.  
  7. // package csci401;
  8.  
  9. public class TestComparable {
  10.  
  11.     public static void main(String[] args) {
  12.  
  13.         Circle1 circle1 = new Circle1(5);
  14.         Circle1 circle2 = new Circle1(4);
  15.  
  16.         Circle1 circle = (Circle1) GeometricObject1.max(circle1, circle2);
  17.  
  18.         System.out.println("The max circle's radius is " + circle.getRadius());
  19.         System.out.println(circle);
  20.  
  21.     }
  22. }
  23.  
  24. abstract class GeometricObject1 implements Comparable<GeometricObject1> {
  25.  
  26.     private String color = "white";
  27.     private boolean filled;
  28.     private java.util.Date dateCreated;
  29.  
  30.     protected GeometricObject1() {
  31.         dateCreated = new java.util.Date();
  32.     }
  33.  
  34.     protected GeometricObject1(String color, boolean filled) {
  35.         dateCreated = new java.util.Date();
  36.         this.color = color;
  37.         this.filled = filled;
  38.     }
  39.  
  40.     public String getColor() {
  41.         return color;
  42.     }
  43.  
  44.     public void setColor(String color) {
  45.         this.color = color;
  46.     }
  47.  
  48.     public boolean isFilled() {
  49.         return filled;
  50.     }
  51.  
  52.     public void setFilled(boolean filled) {
  53.         this.filled = filled;
  54.     }
  55.  
  56.     public java.util.Date getDateCreated() {
  57.         return dateCreated;
  58.     }
  59.  
  60.     public String toString() {
  61.         return "created on " + dateCreated + "\ncolor: " + color
  62.                 + " and filled: " + filled;
  63.     }
  64.  
  65.     public abstract double getArea();
  66.  
  67.     public abstract double getPerimeter();
  68.  
  69.     public static GeometricObject1 max(GeometricObject1 a, GeometricObject1 b) {
  70.         return (a.getArea() > b.getArea() ? a : b);
  71.     }
  72. }
  73.  
  74. class Circle1 extends GeometricObject1 {
  75.     private double radius;
  76.  
  77.     public Circle1() {
  78.     }
  79.  
  80.     public Circle1(double radius) {
  81.         this.radius = radius;
  82.     }
  83.  
  84.     public double getRadius() {
  85.         return radius;
  86.     }
  87.  
  88.     public void setRadius(double radius) {
  89.         this.radius = radius;
  90.     }
  91.  
  92.     @Override
  93.     public double getArea() {
  94.         return radius * radius * Math.PI;
  95.     }
  96.  
  97.     public double getDiameter() {
  98.         return 2 * radius;
  99.     }
  100.  
  101.     @Override
  102.     public double getPerimeter() {
  103.         return 2 * radius * Math.PI;
  104.     }
  105.  
  106.     public String toString() {
  107.         return "The radius is " + radius;
  108.     }
  109.  
  110.     public int compareTo(GeometricObject1 other) {
  111.         Circle1 circleB = (Circle1) other;
  112.         if (this.radius > circleB.radius) {
  113.             return 1;
  114.         } else if (this.radius == circleB.radius) {
  115.             return 0;
  116.         } else
  117.             return -1;
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement