1. (The ComparableCircle class) Define a class named ComparableCircle that exten
ID: 3677181 • Letter: 1
Question
1. (The ComparableCircle class) Define a class named ComparableCircle
that extends Circle and implements Comparable. Draw the UML diagram and
implement the compareTo method to compare the circles on the basis of area.
Write a test class to find the larger of two instances of ComparableCircle objects.
2. (Enable Rectangle comparable) Rewrite the Rectangle class in Listing 13.3 to
extend GeometricObject and implement the Comparable interface. Override
the equals method in the Object class. Two Rectangle objects are equal
if their areas are the same. Draw the UML diagram that involves Rectangle,
GeometricObject, and Comparable.
Listing 13.3 is the following:
1 public class Rectangle extends GeometricObject {
2
3 private double width;
4 private double height;
5
6 public RectangleFromSimpleGeometricObject() {
7 }
8
9 public RectangleFromSimpleGeometricObject(
10 double width, double height) {
11 this.width = width;
12 this.height = height;
13 }
14
15 public RectangleFromSimpleGeometricObject(
16 double width, double height, String color, boolean filled) {
17 this.width = width;
18 this.height = height;
19 setColor(color);
20 setFilled(filled);
21 }
22
23 /** Return width */
24 public double getWidth() {
25 return width;
26 }
27
28 /** Set a new width */
29 public void setWidth(double width) {
30 this.width = width;
31 }
32
33 /** Return height */
34 public double getHeight() {
35 return height;
36 }
37
38 /** Set a new height */
39 public void setHeight(double height) {
40 this.height = height;
41 }
42
43 /** Return area */
44 public double getArea() {
45 return width * height;
46 }
47
48 /** Return perimeter */
49 public double getPerimeter() {
50 return 2 * (width + height);
51 }
52 }
Explanation / Answer
package Chapter_13;
import ToolKit.Circle2D;
import ToolKit.GeometricObject;
----------------------------------------------------------------------------------------------------------------------------------------------------------------
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.