Java黑皮书9.8

题目

(The Fan class) Design a class named Fan to represent a fan, The class contains

Three constants named SLOw, MEDIUM, and FAST with the values l, 2, and 3 to denote the fan speed.

A private int data field named speed that specifies the speed of the fan (the

default is SLOW).

A private boolean data field named on that specifies whether the fan is on (the391159default is false).

A private double data field"named radius that specifies the radius of the fan(the default is 5).

A string data field named color that specifies the color of the fan (the defaultis blue).

The accessor and mutator methods for all four data fields.

A no-arg constructor that creates a default fan.

A method named toString() that returns a string description for the fan. If the fan is on, the method returns the fan speed, color, and radius in one com-bined string. If the fan is not on, the method returns the fan color and radius along with the string “fan is off, in one combined string.

Draw the UML diagram for the class and then implement the class. Write a test program that creates two Fan objects. Assign maximum speed, radius 10, colorye11ow, and turn it on to the first object. Assign medium speed, radius 5, color blue, and turn it off to the second object, Display the objects by invoking their toString method.

(风扇类Fan)设计一个名为Fan 的类来表示一个风扇。这个类包括:

三个名为SLOW、MEDIUM和FAST而值为1、2和3的常量,表示风扇的速度。

一个名为speed的int类型私有数据域,表示风扇的速度(默认值为SLOW)。

一个名为 on 的 boolean 类型私有数据域,表示风扇是否打开(默认值为 fa1se)。

一个名为radius 的 double类型私有数据域,表示风扇的半径(默认值为5)。

一个名为 co1or的string类型数据域,表示风扇的颜色(默认值为 blue)。

这四个数据域的访问器和修改器。

一个创建默认风扇的无参构造方法。

一个名为toString()的方法返回描述风扇的字符串。如果风扇是打开的,那么该方法在一个组合的字符串中返回风扇的速度、颜色和半径。如果风扇没有打开,该方法就会返回一个由“fan is off”和风扇颜色及半径组合成的字符串。

画出该类的 UML图并实现这个类。编写一个测试程序,创建两个Fan 对象。将第一个对象设置为最大速度、半径为 10、颜色为ye11ow、状态为打开。将第二个对象设置为中等速度、半径为5、颜色为 b1ue、状态为关闭。通过调用它们的 tostring 方法显示这些对象。

代码

Fan.java

public class Fan {
    public final static int SLOW = 1;
    public final static int MUDIUM = 2;
    public final static int FAST = 3;

    private int speed;
    private boolean on;
    private double radius;
    private String color;

    Fan() {
        speed = 1;
        on = false;
        radius = 5;
        color = "Blue";
    }

    int getSpeed() {
        return speed;
    }

    void setSpeed(int newSpeed) {
        this.speed = newSpeed;
    }

    boolean getOn() {
        return on;
    }

    void setOn(boolean newOn) {
        this.on = newOn;
    }

    double getRadius() {
        return radius;
    }

    void setRadius(double newRadius) {
        this.radius = newRadius;
    }

    String getColor() {
        return color;
    }

    void setColor(String newColor) {
        this.color = newColor;
    }

    @Override
    public String toString() {
        if (on == false)
            return "Fan is off. " + "\nSpeed: " + getSpeed()+"\nColor: " + getColor() + "\nRadius: " + getRadius();
        else
            return "Fan is on. "+"\nSpeed: " + getSpeed() + "\nColor: " + getColor() + "\nRadius: " + getRadius();
    }
}

Test.java

public class Test {
    public static void main(String[] args) {
        Fan fan1 = new Fan();
        fan1.setSpeed(Fan.FAST);
        fan1.setRadius(10);
        fan1.setColor("yellow");
        fan1.setOn(true);
        System.out.println("Fan1: \n" + fan1.toString());

        Fan fan2 = new Fan();
        fan2.setSpeed(Fan.MUDIUM);
        fan2.setRadius(50);
        fan2.setColor("blue");
        System.out.println("Fan2: \n" + fan2.toString());
    }
}

UML图

结果

  • 30
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值