Write a class named Fan to model fans. The properties are speed, on, radius, and
ID: 3864647 • Letter: W
Question
Write a class named Fan to model fans. The properties are speed, on, radius, and color. You need to provide the accessor methods for the properties, and the toString method for returning a string consisting of all the string values of all the properties in this class. Suppose the fan has three fixed speeds. Use constants 1, 2, and 3 to denote slow, medium, and fast speeds.
The outline of the class is given as follows:
public class Fan {
public final int SLOW = 1;
public final int MEDIUM = 2;
public final int FAST = 3;
private int speed = SLOW;
private boolean on = false;
private double radius = 5;
private String color = "white";
public Fan ( ) { }
public Fan (int speed, boolean on, double radius, String color) { }
public int getSpeed ( ) { }
public void setSpeed (int speed) { }
public boolean isOn ( ) { }
public void setOn ( ) { }
public double getRadius ( ) { }
public void setRadius (double radius ) { }
public String getColor ( ) { }
public void setColor ( String color ) { }
public String toString ( ) { }
}
Write a client program (test application) to test the Fan class. In the client program, create a Fan object. Assign maximum speed, radius 10, color yellow, and turn it on. Display the object by invoking its toString method.
You also need to test this program.
Explanation / Answer
FanTest.java
public class FanTest {
public static void main(String[] args) {
Fan f = new Fan();
f.setSpeed(100);
f.setColor("yellow");
f.setOn();
f.setRadius(10);
System.out.println(f.toString());
}
}
Fan.java
public class Fan {
public final int SLOW = 1;
public final int MEDIUM = 2;
public final int FAST = 3;
private int speed = SLOW;
private boolean on = false;
private double radius = 5;
private String color = "white";
public Fan ( ) { }
public Fan (int speed, boolean on, double radius, String color) {
this.speed = speed;
this.on = on;
this.radius=radius;
this.color=color;
}
public int getSpeed ( ) {
return speed;
}
public void setSpeed (int speed) {
this.speed= speed;
}
public boolean isOn ( ) {
return on;
}
public void setOn ( ) {
this.on = true;
}
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 ( ) {
return "Speed: "+speed+" Radius: "+radius+" Color: "+color+" Turn On: "+on;
}
}
Output:
Speed: 100
Radius: 10.0
Color: yellow
Turn On: true
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.