I am writing a program for my assignment, but for my defaultFan and toString methods I am getting an error stating "invalid method declaration; return type required. However I am unsure what how to resolve this. I tried putting void in front of the two methods and it worked but then I get errors stating I cannot assign variables to final variables slow, medium, and fast. I am not sure if this is correct. How would I fix this?
I also have a hard time using test programs. My professor wants us to use a test program that creates 2 fan objets; the first assign maximum speed, radius 10, color yellow and on status. and the second assign medium speed, radius 5 color blue and off status, and to display the fan objects by invoking their toString methods. Would it be possible for someone to explain how test programs work, and how I would go about creating one for this program. Here is my code:
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 + ".");
}
} }
Write your toString method like this
public String toString() {
String description = "";
if (fanOn = true) {
description += "The speed of the fan is " + speed
+ ", the color of the the fan is " + color
+ ", and the radius of the fan is " + radius + ".";
} else {
description += "The fan is off but the color is " + color
+ " and the radius is " + radius + ".";
}
return description;
}
I am not sure what you want to do with slow/medium/fast(seems a redundancy with speed). But if you want to modify it, don't declare it as final.
private int slow = 1;
private int medium = 2;
private int fast = 3;
You need a constructor for your test program. (by the way, you should name your class Fan)
public fan(int speed, double radius, String color, boolean fanOn ) {
this.speed = speed;
this.radius = radius;
this.color = color;
this.fanOn = fanOn;
}
Your test program should look like this.
public static void main(String args[]) {
fan fan1 = new fan(100, 100, "red", true);
fan fan2 = new fan(200, 200, "green", false);
}