Question
Design a class named Fan to represent a fan. The class contains: Three constants named SLOW, MEDIUM, and FAST with the values 1, 2,
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. The Fan class
VideoNote
M08_LIAN6521_09_SE_C08.qxd 2/2/12 10:21 PM Page 331
332 Chapter 8 Objects and Classes
■ A private int data field named speed 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 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 .
SAMPLE RUN:
A 10.0 inch yellow fan at a speed of 3 A 5.0 inch blue fan; fan is off
I posted this already, and someone answered the following, but it prints 1 instead of the.
CURRENT OUTPUT:
1 Yellow 10.0
Blue 5.0 fan is off
NEEDED OUTPUT:
A 10.0 inch yellow fan at a speed of 3
A 5.0 inch blue fan; fan is off
public class Fan {
static final int SLOW = 1; // static final attributes with constant alues
static final int MEDIUM = 1;
static final int FAST = 1;
//Attributes of Fan Class
int speed;
boolean isFanOnOff;
double radius;
String color;
//Constructor which stores default values
public Fan(){
speed = 0;
isFanOnOff = false;
radius = 0.0;
color = "";
}
// toString method used to return String data of Fan
public String toString(){
if(isFanOnOff){ // Checking Whether Fan is on/off
return this.getSpeed()+" "+this.getColor()+" "+this.getRadius();
}else{
return this.getColor()+" "+this.getRadius()+" "+" fan is off";
}
}
//Setters and Getters of Attributes defined earliier
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public boolean isFanOnOff() {
return isFanOnOff;
}
public void setFanOnOff(boolean isFanOnOff) {
this.isFanOnOff = isFanOnOff;
}
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 static void main(String[] args) {
Fan fan1 = new Fan(); //Creating Fan object1
fan1.setSpeed(Fan.FAST);
fan1.setRadius(10);
fan1.setColor("Yellow");
fan1.setFanOnOff(true);
System.out.println(fan1.toString());
Fan fan2 = new Fan(); //Creating Fan object2
fan2.setSpeed(Fan.MEDIUM);
fan2.setRadius(5);
fan2.setColor("Blue");
fan2.setFanOnOff(false);
System.out.println(fan2.toString());
}
}
Step by Step Solution
3.49 Rating (156 Votes )
There are 3 Steps involved in it
Step: 1
Solution Answer Hey Kindly finds your solution below Let me know if any issue Thanks Copy to code Fa...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started