0

I am trying to create a program where you can change a fans speed, color, radius, and whether you can turn the fan on or off, I have the fan class working fine, except for the toString() method, for some reason when I set some values in the test program, it just defaults to the regular values, any help is accepted.

Thank you.

public class Fan {
final int SLOW = 1;
final int MEDIUM = 2;
final int FAST = 3;

public int speed = SLOW;
public boolean on = false;
public double radius = 5;
public String color = new String("blue");
//fan on
boolean fanOn() {
   on = true;
   return on;
}
//fan off
boolean fanOff() {
   on = false;
   return on;
}
//sets fan speed
String setSpeed(String speed) {
if (speed == "SLOW"){
   speed = String.valueOf(SLOW);
} else if (speed == "MEDIUM") {
   speed = String.valueOf(MEDIUM);
} else if (speed == "FAST") {
   speed = String.valueOf(FAST);
} else {
   speed = "Please enter 'SLOW', 'MEDIUM' or 'FAST' to change the speed of the fan.";
}
   return speed;
}
//sets custom radius
double setRadius(double rad) {
   rad = radius;
   return rad;
}
//sets custom color
String setColor(String colorType) {
   return colorType;
}
//toString() method
public String toString() {
   return ("Speed: " + speed + "\nRadius: " + radius + "\nColor: " + "\nOn: " + on);
 }
}

//test program

 public class TestFan {
 public static void main(String[] args) {
   Fan fan1 = new Fan();
   fan1.setColor("green");
   fan1.setSpeed("FAST");
   fan1.setRadius(3.5);
   fan1.fanOff();
   System.out.println(fan1.toString());

 }
}

This just outputs:

Speed: 1
Radius: 5.0
Color: 
On: false
Justin
  • 65
  • 1
  • 9
  • `speed == "SLOW"` is not how `String` comparison works in Java, you need to use something like `"SLOW".equals(speed)` – MadProgrammer Nov 18 '14 at 23:44
  • 1
    You should also consider using a `enum` instead – MadProgrammer Nov 18 '14 at 23:44
  • Thank you for your input, it's an intro java course, so we have not covered enum yet; however, the toString() method is still not working. @MadProgrammer – Justin Nov 18 '14 at 23:46
  • The method `setSpeed` takes a parameter called `speed` which is masking the `speed` instance field, change the parameter to something like `newSpeed`, you will get some compile errors, but you want this, because `speed = String.valueOf(FAST);` is a good indication of the problem, it should just be `speed = FAST`... – MadProgrammer Nov 18 '14 at 23:51

1 Answers1

0
public class Fan {
final int SLOW = 1;
final int MEDIUM = 2;
final int FAST = 3;

public int speed = SLOW;
public boolean on = false;
public double radius = 5;
public String color = new String("blue");
//fan on
void fanOn() {
   on = true;
   return on;
}
//fan off
void fanOff() {
   on = false;
   return on;
}
//sets fan speed
void setSpeed(String speed) {
   this.speed=speed;
}
//sets custom radius
double setRadius(double rad) {
   rad = radius;
   return rad;
}
//sets custom color
void setColor(String colorType) {
   color = colorType;
}
//toString() method
public String toString() {
   return ("Speed: " + speed + "\nRadius: " + radius + "\nColor: " + "\nOn: " + on);
 }
}

//test program

 public class TestFan {
 public static void main(String[] args) {
   Fan fan1 = new Fan();
   fan1.setColor("green");
   fan1.setSpeed("FAST");
   fan1.setRadius(3.5);
   fan1.fanOff();
   System.out.println(fan1.toString());

 }
}
syntagma
  • 23,346
  • 16
  • 78
  • 134