Design a C++ class names Fan to reperesent a fan. The class contains: Three cons
ID: 3663865 • Letter: D
Question
Design a C++ class names Fan to reperesent a fan. The class contains:
Three constants names SLOW, MEDIUM, and FAST with values 1, 2, and 3 to denote the fan speed.
A private int data field named speed that specifies the speed of the fan (default SLOW).
A private boolean data field named on that specifies whether the fan is on (default false).
A private double data field names radius that specifies the radius of the fan (default 5).
A string data field named color that specifies the color of the fan (default blue).
The get and set methods for all four data fields. Write constructors and destructors as needed that will help create Fan objects.
Write a test program that creates two Fan objects. Assign maximum speed 30, radius 10, color yellow, and turn it on. This is the first object. Assign medium speed, radius 5, color blue, and turn it off. This is second object. Display ie print the objects by invoking their get() methods.
Explanation / Answer
class Fan
{
public static final int SLOW=1,MEDIUM=2,FAST=3;
int speed;
boolean f_on;
double radius;
String color;
Fan()
{
speed=SLOW;
f_on=false;
radius=4;
color="blue";
}
Fan(int speed,double radius,String color,boolean f_on)
{
this.speed=speed;
this.radius=radius;
this.color=color;
this.f_on=f_on;
}
void display()
{
if(f_on==true)
{
System.out.println("Fan is on the speed is ="+speed+" the color is ="+color+" the radius is ="+radius);
}
else
{
System.out.println("Fan is off the color of fan is ="+color+" the radius of fan is ="+radius);
}
}
public static void main(String [] args)
{
Fan obj = new Fan();
Fan obj1 = new Fan(MEDIUM,6,"brown",true);
obj.display();
obj1.display();
}
}