Q:

(The Fan class) Design a class named Fan to represent a fan. The class contains:

0

(The Fan class) Design a class named Fan to represent a fan. The class contains:

Three constants named SLOW, MEDIUM, and FAST with the values 1, 2, and 3 to denote the fan speed.
A private int data field named speed that specifies the speed of the fan (the default is SLOW).
A private boolean data field named on that specifies whether the fan is on (the default is false).
A private double data field named radius that specifies the radius of the fan (the default is 5).
A string data field named color that specifies the color of the fan (the default is blue).
The accessor and mutator methods for all four data fields.
A no-arg constructor that creates a default fan.
A method named toString() that returns a string description for the fan. If the fan is on, the method returns the fan speed, color, and radius in one combined string. If the fan is not on, the method returns the fan color and radius along with the string “fan is off” in one combined string.

Draw the UML diagram for the class and then implement the class. Write a test program that creates two Fan objects. Assign maximum speed, radius 10, color yellow, and turn it on to the first object. Assign medium speed, radius 5, color blue, and turn it off to the second object. Display the objects by invoking their toString method.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

/**********************************************************************************
* (The Fan class) Design a class named Fan to represent a fan. The class contains:*
*                                                                                 *
* ■ Three constants named SLOW, MEDIUM, and FAST with the values 1, 2, and 3 to   *
*   denote the fan speed.                                                         *
* ■ A private int data field named speed that specifies the speed of the fan (the *
*   default is SLOW).                                                             *
* ■ A private boolean data field named on that specifies whether the fan is on    *
*   (the default is false).                                                       *
* ■ A private double data field named radius that specifies the radius of the fan *
*   (the default is 5).                                                           *
* ■ A string data field named color that specifies the color of the fan           *
*   (the default is blue).                                                        *
* ■ The accessor and mutator methods for all four data fields.                    *
* ■ A no-arg constructor that creates a default fan.                              *
* ■ A method named toString() that returns a string description for the fan. If   *
*   the fan is on, the method returns the fan speed, color, and radius in one     *
*   combined string. If the fan is not on, the method returns the fan color and   *
*   radius along with the string “fan is off” in one combined string.             *
*                                                                                 *
* Draw the UML diagram for the class and then implement the class. Write a test   *
* program that creates two Fan objects. Assign maximum speed, radius 10, color    *
* yellow, and turn it on to the first object. Assign medium speed, radius 5,      *
* color blue, and turn it off to the second object. Display the objects by        *
* invoking their toString method.                                                 *
/*********************************************************************************/

public class Exercise_09_08 {
	/** Main method */
	public static void main(String[] args) {
		final int SLOW = 1;		// Fan speed slow
		final int MEDIUM = 2;	// Fan speed medium
		final int FAST = 3;		// Fan speed fast

		// Create two Fan objects
		Fan fan1 = new Fan();
		Fan fan2 = new Fan();

		// Assign maximum speed, radius 10, color yellow,  
		// and turn it on to the first object
		fan1.setSpeed(FAST);
		fan1.setRadius(10);
		fan1.setColor("yellow");
		fan1.turnOn();

		// Assign medium speed, radius 5, color blue,
		// and turn it off to the second object
		fan2.setSpeed(MEDIUM);
		fan2.setRadius(5);
		fan2.setColor("blue");
		fan2.turnOff();

		// Display the objects by invoking their toString method
		System.out.println(fan1.toString());
		System.out.println(fan2.toString());
	}
}

Fan.java

/***********************
*         Fan          *
* ---------------------*
* +SLOW: int           *
* ----------           *
* +MEDIUM: int         *
* ------------         *
* +FAST: int           *
* ----------           *
* -speed: int          *
* -on: boolean         *
* -radius: double      *
* +color: String       *
* +Fan()               *
* +setSpeed(int)       *
* +turnOn()            *
* +turnOff()           *
* +setRadius(double):  *
* +setColor(String):   *
* +getSpeed(): String  *
* +isOn(): boolean     *
* +getRadius(): double *
* +getColor(): String  *
* +toString(): String  *
***********************/

public class Fan {
	final static int SLOW = 1;
	final static int MEDIUM = 2;
	final static int FAST = 3;
	private int speed;
	private boolean on;
	private double radius;
	String color;

	/** Constructor that creates a default fan */
	Fan() {
		speed = SLOW;
		on = false;
		radius = 5;
		color = "blue";
	}

	/** Mutator methods */
	/** Sets speed */
	public void setSpeed(int newSpeed) {
		speed = newSpeed;
	}

	/** Sets fan on */
	public void turnOn() {
			on = true;
	}

	/** Sets fan off */ 
	public void turnOff() {
			on = false;
	}

	/** Sets color */
	public void setColor(String newColor) {
		color = newColor;
	}

	/** Sets radius */
	public void setRadius(double newRadius) {
		radius = newRadius;
	}

	/** Accessor methods */
	/** Return speed */
	public String getSpeed() {
		String s = "";
		switch (speed) {
			case SLOW: s = "SLOW"; break;
			case MEDIUM: s = "MEDIUM"; break;
			case FAST: s = "FAST";
		}
		return s;
	}

	/** Return on */
	public boolean isOn() {
		return on;
	}

	/** Return radius */
	public double getRadius() {
		return radius;
	}

	/** Return color */
	public String getColor() {
		return color;
	}

	/** Returns a string description for the fan */
	public String toString() {
		if (on == true) {
			return "\nFan speed: " + getSpeed() + ", color: " + color + 
					 ", radius: " + radius + "\n";
		}
		else{
			return "\nFan color: " + color + ", radius: " + radius +
					 "\nfan is off\n";
		}
	}
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now