This lab must be finished in the lab period. Write a class named Fan .docx
1. This lab must be finished in the lab period. Write a class named
Fan to represent a fan. The class contains the following
members: Three constants named SLOW, MEDIUM, and FAST
with the values 1, 2 and 3 to denote the fan speed. A private
integer data field named speed that specifies the speed of the
fan. The default speed 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
value is blue. The accessor and mutator methods for all the data
fields. A no-argument constructor to create a default fan. A
constructor with arguments. Accepts values for all the instance
variables and then initializes the instance variables to the given
values. A method named toString() that returns a string
description for the fan. A method called equals that compares
two instances of the Fan class. Return true if they are the same,
false otherwise. This method accepts one parameter of type Fan.
Draw the UML diagram for the class. Must show your UML
diagram to be checked off. Create a class called Fan.
Implement the class based on your UML diagram. Compile it.
Create a driver class to test the Fan class that you just created.
Create an array of Fan objects in the main method Fan[] fans =
new Fan[10] Call the method populate Call the method
isUnique Populate the array with objects of type Fan: (must
write a method called populate) Keep asking the user for the
information about each fan. Create the object of type Fan.
Store the reference to the object in the array. Output the
content of the array fans. (Must write a method called output
Compare the elements of the array to check if all the fans stored
in the array are unique or not. Must Write a method called
isUnique that returns a boolean
2. Solution
import java.util.*;
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;
String color;
public void setSpeed(int s)
{
speed=s;
}
public void setOn(boolean o)
{
on=o;
}
public void setRadius(double r)
{
3. radius=r;
}
public void setColor(String c)
{
color=c;
}
public String getColor()
{
return color;
}
public int getSpeed()
{
return speed;
}
public boolean getOn()
{
return on;
}
public double getRadius()
{
return radius;
}
public Fan()
{
on=false;
4. radius=5;
speed=SLOW;
}
public Fan(int s,boolean o,double r)
{
on=o;
speed=s;
radius=r;
}
public String toString()
{
return " Speed: "+speed +"tRadius: "+radius+"tIs
On: "+on;
}
public boolean equals(Fan f)
{
if(speed==f.speed && on==f.on && radius==f.radius)
return true;
else
return false;
}
}
public class HelloWorld{
public void display(Fan[] fans)
{
5. for(int i=0;i<fans.length;i++)
{
System.out.println(fans[i].toString());
}
}
public boolean isUnique(Fan[] fans)
{
for(int i=0;i<fans.length-1;i++)
{
if(fans[i].equals(fans[i+1]))
return false;
}
return true;
}
public Fan populate()
{
Fan f=new Fan();
Scanner read=new Scanner(System.in);
System.out.print("Enter speed of fan: ");
int speed=read.nextInt();
f.setSpeed(speed);
System.out.print("is Fan on?Enter 1 for on,any other
value for off?? ");
int on=read.nextInt();
if(on==1)