Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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

ID: 3665896 • Letter: #

Question

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

Three constants named SLOW, MEDIUM, and FAST with 1, 2, and 3 to denote the fan speed.

An int data field named speed that specifies the speed of the fan (default SLOW)

A boolean data field named isOn that specifies whether the fan is on (default false)

A double data field named 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)

A no-argument constructor that creates a default fan.

Another constructor that creates a fan with specified values for speed, on/off, radius and color.

The accessor and mutator methods for all four data fields.

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 fan color and radius along the string “fan is off” in one combined string.

JAVA PROGRAMMING

What to turn in:

Fan.java

FanDriver.java

Explanation / Answer

/**
* @author Srinivas Palli
*
*/
public class Fan {

   // constants
   final static int SLOW = 1, MEDIUM = 2, FAST = 3;

   int speed;
   boolean isOn;
   double radius;
   String color;

   /**
   *
   */
   public Fan() {
       // TODO Auto-generated constructor stub
       speed = SLOW;
       isOn = false;
       radius = 5d;
       color = "blue";

   }

   /**
   * @param speed
   * @param isOn
   * @param radius
   * @param color
   */
   public Fan(int speed, boolean isOn, double radius, String color) {
       super();
       this.speed = speed;
       this.isOn = isOn;
       this.radius = radius;
       this.color = color;
   }

   /**
   * @return the speed
   */
   public int getSpeed() {
       return speed;
   }

   /**
   * @param speed
   * the speed to set
   */
   public void setSpeed(int speed) {
       this.speed = speed;
   }

   /**
   * @return the isOn
   */
   public boolean isOn() {
       return isOn;
   }

   /**
   * @param isOn
   * the isOn to set
   */
   public void setOn(boolean isOn) {
       this.isOn = isOn;
   }

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

   /**
   * @param radius
   * the radius to set
   */
   public void setRadius(double radius) {
       this.radius = radius;
   }

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

   /**
   * @param color
   * the color to set
   */
   public void setColor(String color) {
       this.color = color;
   }

   /*
   * (non-Javadoc)
   *
   * @see java.lang.Object#toString()
   */
   @Override
   public String toString() {
       if (isOn) {
           return "speed=" + speed + ", radius=" + radius + ", color=" + color
                   + "";
       } else {
           return "speed=" + speed + ", radius=" + radius + ", color=" + color
                   + " fan is off";

       }
   }

}

/**
* @author Srinivas Palli
*
*/
public class FanDriver {

   /**
   * @param args
   */
   public static void main(String[] args) {

       //using default constructor
       Fan fan1 = new Fan();
       System.out.println("Default Fan1:" + fan1.toString());

       //using parameterised constructor
       Fan fan2 = new Fan(Fan.FAST, true, 7d, "Green");
       System.out.println("Prameterised Fan2:" + fan2.toString());

   }
}

OUTPUT:

Default Fan1:speed=1, radius=5.0, color=blue fan is off
Prameterised Fan2:speed=3, radius=7.0, color=Green