版本1: 模拟实现电风扇,可以调3档速度(慢速、中速、快速);开关按钮;定时吹风;描述风扇的扇叶大小、颜色等。 设计Fan类,属性包括:3个常量SLOW(1)、MEDIUM(2)、FAST(3)代表风扇的速度;1个int属性speed指定速度,默认值为SLOW;1个boolean属性on指定开关机,默认值false;1个double属性radius指定风扇扇叶大小;1个String属性color指定扇叶颜色,默认值为blue。方法包括这些属性的访问器、构造函数、重写Object类的toString()和equals()方法等。运行测试代码: public static void main(String[] args) { Fan1 fan1 = new Fan1(); fan1.setSpeed(Fan1.FAST); fan1.setRadius(10); fan1.setColor("yellow"); fan1.setOn(true); System.out.println(fan1.toString()); } 版本2: 修改版本1中Fan类,让其继承Pane类,并且把color属性设置为Color类型,默认属性为red。随机产生radius,取值范围为1-5;随机产生颜色,取值范围为red、blue、yellow、green、orange;根据color、radius属性值绘制风扇。运行如下图: 版本3: 让版本2中的风扇转起来。创建一个FanControl类包含以下内容:Start、Stop、Reverse按钮,用于开启、关闭、反转控制;一个滚动条控制速度。运行示例如下:
时间: 2024-03-26 12:37:40 浏览: 42
好的,我来一一解答你的问题。
1. 版本1中的Fan类实现了哪些属性和方法?
版本1中的Fan类实现了以下属性和方法:
- 属性:3个常量SLOW(1)、MEDIUM(2)、FAST(3)代表风扇的速度;1个int属性speed指定速度,默认值为SLOW;1个boolean属性on指定开关机,默认值false;1个double属性radius指定风扇扇叶大小;1个String属性color指定扇叶颜色,默认值为blue。
- 方法:包括这些属性的访问器(getter和setter方法)、构造函数、重写Object类的toString()和equals()方法等。
2. 版本2中的Fan类修改了哪些内容?
版本2中的Fan类修改了以下内容:
- 让Fan类继承自Pane类。
- 把color属性设置为Color类型,默认属性为red。
- 随机产生radius,取值范围为1-5;随机产生颜色,取值范围为red、blue、yellow、green、orange。
- 根据color、radius属性值绘制风扇。
3. 版本3中的FanControl类实现了哪些功能?
版本3中的FanControl类实现了以下功能:
- Start、Stop、Reverse按钮,用于开启、关闭、反转控制。
- 一个滚动条控制速度。
相关问题
拟实现电风扇,可以调3档速度(慢速、中速、快速);开关按钮;定时吹风;描述风扇的扇叶大小、颜色等。 设计Fan类,属性包括:3个常量SLOW(1)、MEDIUM(2)、FAST(3)代表风扇的速度;1个int属性speed指定速度,默认值为SLOW;1个boolean属性on指定开关机,
好的,根据您的需求,设计的Fan类如下:
```java
public class Fan {
private final int SLOW = 1;
private final int MEDIUM = 2;
private final int FAST = 3;
private int speed = SLOW;
private boolean on = false;
private double radius = 5;
private String color = "blue";
public Fan() {
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public boolean isOn() {
return on;
}
public void setOn(boolean on) {
this.on = on;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String toString() {
if (on) {
return "Fan speed: " + speed + ", color: " + color + ", radius: " + radius;
} else {
return "Fan is off" + ", color: " + color + ", radius: " + radius;
}
}
}
```
以上代码实现了风扇的速度调节、开关机、风扇扇叶大小、颜色等功能,并提供了toString方法以便于输出风扇的状态。您可以在其他类中实例化Fan类并调用其方法来操作风扇。
模拟实现电风扇,可以调3档速度(慢速、中速、快速);开关按钮;定时吹风;描述风扇的扇叶大小、颜色等。 设计Fan类,属性包括:3个常量SLOW(1)、MEDIUM(2)、FAST(3)代表风扇的速度;1个int属性speed指定速度,默认值为SLOW;1个boolean属性on指定开关机,默认值false;1个double属性radius指定风扇扇叶大小;1个String属性color指定扇叶颜色,默认值为blue。方法包括这些属性的访问器、构造函数、重写Object类的toString()和equals()方法等。运行测试代码: public static void main(String[] args) { Fan1 fan1 = new Fan1(); fan1.setSpeed(Fan1.FAST); fan1.setRadius(10); fan1.setColor("yellow"); fan1.setOn(true); System.out.println(fan1.toString()); }
好的,以下是模拟实现电风扇的代码:
```java
public class Fan {
// 风扇的速度常量
public static final int SLOW = 1;
public static final int MEDIUM = 2;
public static final int FAST = 3;
private int speed; // 风扇的速度,默认为慢速
private boolean on; // 风扇的开关状态,默认关闭
private double radius; // 风扇扇叶大小
private String color; // 风扇扇叶颜色
// 构造函数
public Fan() {
this.speed = SLOW;
this.on = false;
this.radius = 5;
this.color = "blue";
}
// 访问器
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public boolean isOn() {
return on;
}
public void setOn(boolean on) {
this.on = on;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
// 重写toString()方法,返回风扇的状态信息
@Override
public String toString() {
if (on) {
return "风扇速度为" + speed + ",颜色为" + color + ",扇叶大小为" + radius + ",风扇已经开启。";
} else {
return "颜色为" + color + ",扇叶大小为" + radius + "的风扇目前处于关闭状态。";
}
}
// 重写equals()方法,判断两个风扇是否相等
@Override
public boolean equals(Object obj) {
if (obj instanceof Fan) {
Fan otherFan = (Fan) obj;
return this.speed == otherFan.speed && this.on == otherFan.on
&& this.radius == otherFan.radius && this.color.equals(otherFan.color);
} else {
return false;
}
}
}
```
测试代码如下:
```java
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.toString());
}
```
输出结果为:
```
风扇速度为3,颜色为yellow,扇叶大小为10.0,风扇已经开启。
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)