我正在为我的作业编写一个程序,但是对于我的 defaultFan 和 toString 方法,我收到一条错误消息,指出“方法声明无效;需要返回类型。但是我不确定如何解决这个问题。我尝试将 void 放在两者前面方法并且它有效,但随后我收到错误消息,指出我无法将变量分配给最终变量慢速、中等和快速。我不确定这是否正确。我将如何解决这个问题?
我也很难使用测试程序。我的教授希望我们使用一个创建 2 个粉丝对象的测试程序;第一个指定最大速度,半径 10,颜色黄色和状态。第二个分配中等速度,半径5颜色蓝色和关闭状态,并通过调用它们的toString方法来显示风扇对象。有人可以解释测试程序是如何工作的,以及我将如何为这个程序创建一个。这是我的代码:
public class fan {
private final int slow = 1;
private final int medium = 2;
private final int fast = 3;
private int speed;
private boolean fanOn;
private double radius;
private String color;
public void defaultFan( )
{
int speed = 1;
boolean fanOn = false;
double radius = 5;
String color = "blue";
}
public fan(final int slow, final int medium, final int fast, int
speed, boolean fanOn, double radius, String color) {
this.slow = slow;
this.medium = medium;
this.fast = fast;
this.speed = speed;
this.fanOn = fanOn;
this.radius = radius;
this.color = color;
}
public final int getSlow(){
return slow;
}
public final int getMedium() {
return medium;
}
public final int getFast() {
return fast;
}
public int getSpeed() {
return speed;
}
public boolean getfanOn() {
return fanOn;
}
public double getradius() {
return radius;
}
public String getcolor() {
return color;
}
public void setSlow(final int slow) {
this.slow = slow;
}
public void setMedium(final int medium) {
this.medium = medium;
}
public void setFast(final int fast) {
this.fast = fast;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public void setFanOn(boolean fanOn) {
this.fanOn = fanOn;
}
public void setRadius(double radius) {
this.radius = radius;
}
public void setColor(String color) {
this.color = color;
}
public void toString() {
if(fanOn = true ) {
System.out.println("The speed of the fan is " + speed + ", the color
of the the fan is " + color + ", and the radius of the fan is " +
radius + ".");
}
else {
System.out.println("The fan is off but the color is " + color +"
and the radius is " + radius + ".");
}
} }