JAVA空调遥控器类与测试
时间: 2023-06-10 07:08:49 浏览: 104
以下是一个简单的Java空调遥控器类的示例代码,包括一些基本的功能,例如设置温度,调节风速和开关机。
```java
public class AirConditioner {
private int temperature;
private int fanSpeed;
private boolean isOn;
public AirConditioner() {
temperature = 25;
fanSpeed = 1;
isOn = false;
}
public void setTemperature(int temperature) {
if (temperature < 16 || temperature > 30) {
System.out.println("Invalid temperature!");
return;
}
this.temperature = temperature;
System.out.println("Temperature set to " + temperature + " degrees Celsius.");
}
public void setFanSpeed(int fanSpeed) {
if (fanSpeed < 1 || fanSpeed > 3) {
System.out.println("Invalid fan speed!");
return;
}
this.fanSpeed = fanSpeed;
System.out.println("Fan speed set to " + fanSpeed + ".");
}
public void turnOn() {
isOn = true;
System.out.println("Air conditioner turned on.");
}
public void turnOff() {
isOn = false;
System.out.println("Air conditioner turned off.");
}
}
```
以下是一个简单的测试类,用于演示如何使用空调遥控器类:
```java
public class AirConditionerTest {
public static void main(String[] args) {
AirConditioner airConditioner = new AirConditioner();
airConditioner.turnOn();
airConditioner.setTemperature(20);
airConditioner.setFanSpeed(2);
airConditioner.turnOff();
}
}
```
输出:
```
Air conditioner turned on.
Temperature set to 20 degrees Celsius.
Fan speed set to 2.
Air conditioner turned off.
```
当我们调用`turnOn()`方法时,空调被打开并初始温度设置为25度,风速设置为1. `setTemperature()`和`setFanSpeed()`方法用于设置温度和风速,并且会在控制台上打印相应的消息。`turnOff()`方法关闭空调。在测试类中,我们创建了一个新的AirConditioner对象并对其进行操作,最后将其关闭。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)