Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Design a Fan class that represents a simple window fan. This class contains:

ID: 3600736 • Letter: 1

Question

1. Design a Fan class that represents a simple window fan. This class contains: Three public static constants ("final" variables) named SLOW, MEDIUM, and FAST with the values 1, 2, and 3, respectively. a. b. int data field named "speed" that specifies the fan speed (the Aprivate default is SLOW) Aprivate boolean data field named "on" that specifies whether the fan is currently on (the default value is false) c. d. Aprivate double data field named "radius" that specifies the radius of the fan (the default value is 5) String data field named "color" that specifies the color of the Aprivate fan (the default value is "blue") e. f. Accessor and mutator methods ("getters" and "setters") for all four data fields A constructor that does not take any arguments, and creates a fan with all default values h. A public method named tostring() that does not take any arguments and returns a String with a description of the fan. If the fan is on, the method returns the fan speed, color, and radius as one combined String. If the fan is off, the method returns the fan color, radius, and the String "fan is off" as one combined String. Draw a UML class diagram for this class and then implement it. Write a test program that creates two Fan objects. Set the first fan's speed to maximum, its radius to 10, and its color to yellow, and then turn it on. Set the second fan's speed to medium, its radius to 5, and its color to blue, then turn it off. Display the objects by invoking their tostring) methods.

Explanation / Answer

public class Fan {

public static final int SLOW=1;

public static final int MEDIUM=2;

public static final int FAST=3;

private int speed;

private boolean on;

private double radius;

private String color;

public int getSpeed() {

return speed;

}

public void setSpeed(int speed) {

this.speed = speed;

}

public boolean isOn() {

return on;

}

public void setOn(boolean on) {

this.on = on;

}

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 Fan() {

speed=0;

on=false;

radius=0.0;

color=null;

}

@Override

public String toString() {

if(on==true)

return "Fan speed is "+speed+" fan color is "+color+" fan radius is "+radius;

else

return "Fan speed is "+speed+" fan color is "+color+" fan radius is "+radius+" fan id off";

}

}

public class TestFan {

public static void main(String[] args) {

Fan firstFan=new Fan();

Fan secondFan=new Fan();

firstFan.setSpeed(Fan.FAST);

firstFan.setColor("Yellow");

firstFan.setRadius(10);

firstFan.setOn(true);

secondFan.setSpeed(Fan.MEDIUM);

secondFan.setColor("Blue");

secondFan.setRadius(5);

secondFan.setOn(false);

System.out.println(firstFan);

System.out.println(secondFan);

}

}