*13.6 (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.
*13.6(ComparableCircle类)创建名为ComparableCircle的类,他继承自Circle 类,并实现Comparable接口。画出UML图并实现compareTo方法,使其根据面积比较两个圆。编写一个测试程序求出两个ComparableCircle实例对象的较大者,以及两个圆的较大者。
代码如下:
import java.util.*;
public class ComparableCircle extends Circle implements Comparable<ComparableCircle>
{
public ComparableCircle(double r) {
super(r);
// TODO 自动生成的构造函数存根
}
public ComparableCircle max(ComparableCircle o1, ComparableCircle o2)
{
if (o1.compareTo(o2) > 0)
{
return o1;
}
else
return o2;
}
@Override
public int compareTo(ComparableCircle o) {
// TODO 自动生成的方法存根
if (super.getArea() > o.getArea())
{
return 1;
}
else if (super.getArea() < o.getArea())
{
return -1;
}
else
return 0;
}
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("请输入两个半径:");
double r1 = input.nextDouble();
ComparableCircle o1 = new ComparableCircle(r1);
double r2 = input.nextDouble();
ComparableCircle o2 = new ComparableCircle(r2);
System.out.println(o1.compareTo(o2));
}
}
public class Unite13Test6
{
public static void main(String[] args)
{
}
}
class Circle
{
double r;
double area;
public Circle(double r)
{
this.r = r;
}
public double getArea()
{
return 2 * Math.PI * r;
}
}
结果如下:
*13.7 (The Colorable interface) Design an interface named Colorable with a void
method named howToColor(). Every class of a colorable object must implement
the Colorable interface. Design a class named Square that extends
GeometricObject and implements Colorable. Implement howToColor to
display the message Color all four sides.
Draw a UML diagram that involves Colorable, Square, and GeometricObject.
Write a test program that creates an array of five GeometricObjects. For each
object in the array, display its area and invoke its howToColor method if it is
colorable.
*13.7(Colorable类)创建名为Colorable的接口,其中有名为howToColor的void方法。可着色对象的每个类必须实现Colorable接口。设计一个名为Square的类,继承自GeometriObject类并实现Colorable接口。实现howToColor方法,显示一个消息Color all four sides(给所有的四条边着色)。
画出包含Colorable、Square和GeometriObject的UML图。编写一个测试程序,创建有五个GeometriObject对象的数组。对于数组中的每个对象而言,如果对象是可着色的,则调用其howToColor方法。
代码如下
import java.util.*;
public class Unite13Test7
{
public static void main(String [] args)
{
Scanner input = new Scanner(System.in);
GeometricObject[] squares = new Square[5];
for (int i = 0; i < 5; i++)
{
System.out.println(i + " : Square ");
System.out.print("\t输入颜色: ");
String color = input.next();
System.out.print("\t输入是否填充: ");
boolean filled = input.nextBoolean();
squares[i] = new Square(color, filled);
}
}
}
interface Coloarable
{
void howToColor();
}
class GeometricObject
{
private String color;
private boolean 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 class Square extends GeometricObject implements Coloarable
{
public Square(String color,boolean filled)
{
}
private double side;
public void howToColor()
{
System.out.println("Color all four");
}
public double getSide() {
return side;
}
public void setSide(double side) {
this.side = side;
}
}
结果如下: