Java语言程序设计 例题13.6(ComparableCircle类)例题13.7()

*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;
	}
}

结果如下:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

差劲的厉害了

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值