In: Computer Science
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 defaultis 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. 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 defaultis 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.
I have designed class for Fan with all the constant and data members.
Fan class:-
In this class, I have declared three costants using final keyword::
1> final int SLOW = 1;
2> final int MEDIUM = 2;
3> final int FAST = 3;
Fan class also contain speed, color, radius and boolean "on" data member. All the data members intialized in non-arg counstructor with its default values.
public String toString():- This method returns fan information on the basis on fan status either on or off.
// This class represent FanTest
class Fan{
/*
create contants using final keywod for the FanTest class
which denotes fan speed
*/
final int SLOW = 1;
final int MEDIUM = 2;
final int FAST = 3;
// private data members
// store the fan speed
private int speed;
// store the status of fan
private boolean on;
// store the radius of fan
private double radius;
// stores the color of fan
String color;
/*
accessor and mutator
for above 4 data members
*/
// get method which returns the speed
public int getSpeed() {
return speed;
}
// set method which sets the value for speed
public void setSpeed(int speed) {
this.speed = speed;
}
// get method which returns the boolean status of fan whether is on or not
public boolean isOn() {
return on;
}
// set method which sets the on value
public void setOn(boolean on) {
this.on = on;
}
// get method which returns the speed of the fan
public double getRadius() {
return radius;
}
// set method which sets the value of radius
public void setRadius(double radius) {
this.radius = radius;
}
// get method which returns color of fan
public String getColor() {
return color;
}
// set method which sets the color of fan
public void setColor(String color) {
this.color = color;
}
// non-arg constructot for FanTest class
public Fan() {
// set default "speed" which is slow
speed = SLOW;
// set default value false for "on" data member
on = false;
// set default value 5 for fan radius
radius = 5;
// set fan color blue bydefault
color = "blue";
}
/**
* This method return fan information
* @return fan information
*/
@Override
public String toString() {
// if fan is on then return string with fan speed, color and radius
if(on){
return "Fan speed : "+speed+", Fan color : "+color+", Fan radius : "+radius;
}
// if an is off then return string with color, radius and "fan is off"
else{
return "Fan color : "+color+", Fan radius : "+radius+", fan is off";
}
}
}
I have attached one java program which demonstrate the Fan class with all the data members.
FanTest.java:-
// This class represent FanTest
class Fan{
/*
create contants using final keywod for the FanTest class
which denotes fan speed
*/
final int SLOW = 1;
final int MEDIUM = 2;
final int FAST = 3;
// private data members
// store the fan speed
private int speed;
// store the status of fan
private boolean on;
// store the radius of fan
private double radius;
// stores the color of fan
String color;
/*
accessor and mutator
for above 4 data members
*/
// get method which returns the speed
public int getSpeed() {
return speed;
}
// set method which sets the value for speed
public void setSpeed(int speed) {
this.speed = speed;
}
// get method which returns the boolean status of fan whether is on or not
public boolean isOn() {
return on;
}
// set method which sets the on value
public void setOn(boolean on) {
this.on = on;
}
// get method which returns the speed of the fan
public double getRadius() {
return radius;
}
// set method which sets the value of radius
public void setRadius(double radius) {
this.radius = radius;
}
// get method which returns color of fan
public String getColor() {
return color;
}
// set method which sets the color of fan
public void setColor(String color) {
this.color = color;
}
// non-arg constructot for FanTest class
public Fan() {
// set default "speed" which is slow
speed = SLOW;
// set default value false for "on" data member
on = false;
// set default value 5 for fan radius
radius = 5;
// set fan color blue bydefault
color = "blue";
}
/**
* This method return fan information
* @return fan information
*/
@Override
public String toString() {
// if fan is on then return string with fan speed, color and radius
if(on){
return "Fan speed : "+speed+", Fan color : "+color+", Fan radius : "+radius;
}
// if an is off then return string with color, radius and "fan is off"
else{
return "Fan color : "+color+", Fan radius : "+radius+", fan is off";
}
}
}
/*
This class create an object of
fan class and test the methods
*/
public class FanTest{
public static void main(String args[]){
// create an object of FanTest class with non-arg constructor
Fan fan1 = new Fan();
// now set the status of fan using setOn() method
fan1.setOn(true);
// now get the fan status using isOn() method
boolean status = fan1.isOn();
if(status){
System.out.println("Fan is on");
}else{
System.out.println("Fan is off");
}
// display the fan information using toString() method
System.out.println("Fan info : "+fan1.toString());
}
}
Output:-
1> When fan is on.
2> When fan is off.
I hope you will understand the above program and how to create class for fan.
Do you feel needful and useful then please upvote me.
Thank you.