第九章第八题(Fan类)(Fan class)

第九章第八题(Fan类)(Fan class)

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

    • 三个名为 SLOW、MEDIUM 和 FAST 而值为 1、2 和 3 的常量,表示风扇的速度。
    • 一个名为 speed 的 int 类型私有数据域,表示风扇的速度(默认值为 SLOW)。
    • 一个名为 on 的 boolean 类型私有数据域,表示风扇是否打开(默认值为 false)。
    • 一个名为 radius 的double 类型私有数据域,表示风扇的半径(默认值为 5)。
    • 一个名为 color 的 String 类型数据域,表示风扇的颜色(默认值为 blue)。
    • 这四个数据域的访问器和修改器。
    • 一个创建默认风扇的无参构造方法。
    • 一个名为 toString() 的方法返回描述风扇的字符串。如果风扇是打开的,那么该方法在一个组合的字符串中返回风扇的速度、颜色和半径。如果风扇没有打开,该方法就返回一个由 “fan is off” 和风扇颜色及半径组合的字符串。

    画出该类的UML图并实现这个类。编写一个测试程序,创建两个 Fan 对象。将第一个对象设置为最大速度、半径为 10、颜色为 yellow 、状态为打开。将第二个对象设置为中等速度、半径为 5、颜色为 blue 、状态为关闭。通过调用它们的 toString 方法显示这些对象。
    9.8 (Fan class) design a class named fan to represent a fan. This class includes:

    • Three constants named slow, medium, and fast with values of 1, 2, and 3 represent the speed of the fan.
    • A private data field of type int called speed represents the speed of the fan (the default is slow).
    • A boolean type private data field named on indicates whether the fan is on (the default is false).
    • A private data field of type double named radius represents the radius of the fan (the default is 5).
    • A string type data field named color represents the color of the fan (the default is blue).
    • Accessors and modifiers for these four data fields.
    • A nonparametric construction method for creating default fans.
    • A method called tostring() returns a string describing the fan. If the fan is on, the method returns the speed, color, and radius of the fan in a combined string. If the fan is not turned on, the method returns a string composed of “fan is off” and the fan color and radius.

    Draw the UML diagram of the class and implement the class. Write a test program and create two fan objects. Set the first object to maximum speed, radius 10, color yellow, and state on. Set the second object to medium speed, radius 5, color blue, and state off. Display these objects by calling their toString method.

  • 参考代码:

package chapter09;

public class Code_08 {
    
    
    public static void main(String[] args){
    
    
        Fan f = new Fan();
        f.setColor("red");
        System.out.println(f.toString());
    }
}
class Fan{
    
    
    final static int SLOW = 1,MEDIUM = 2,FAST = 3;
    private int speed = SLOW;
    private boolean on = false;
    private double radius = 5;
    String color = "blue";
    public int getSpeed(){
    
    
        return speed;
    }
    public void setSpeed(int newSpeed){
    
    
        if (newSpeed >= 1 && newSpeed <= 3 )
            speed = newSpeed;
        else System.out.println("你设置的数字有误,应该在1-3之间1");
    }
    public boolean getOn(){
    
    
        return on;
    }
    public void setOn(boolean newon){
    
    
        on = newon;
    }
    public String getColor(){
    
    
        return color;
    }
    public void setColor(String newcolor){
    
    
        color = newcolor;
    }
    Fan(){
    
    

    }
    public String toString(){
    
    
        return "fan's speed is " + speed + "\nfan's on is " + on + "\nfan's radius is " + radius + "\nfan's color is " + color;
    }
}
  • 结果显示:
fan's speed is 1
fan's on is false
fan's radius is 5.0
fan's color is red

Process finished with exit code 0

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/jxh1025_/article/details/109257009