Pages

Tuesday, 30 August 2016

Chapter 9 Exercise 8, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY.

*9.8 (The Fan class) 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 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 com-
bined 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.


public class Fan {

    public static final int SLOW = 1;
    public static final int MEDIUM = 2;
    public static final int FAST = 3;

    private int mSpeed;
    private boolean mOn;
    private double mRadius;
    private String mColor;

    public Fan() {
        mSpeed = 1;
        mRadius = 5;
        mColor = "white";
    }

    public int getSpeed() {
        return mSpeed;
    }

    public void setSpeed(int speed) {
        mSpeed = speed;
    }

    public boolean isOn() {
        return mOn;
    }

    public void setOn(boolean on) {
        mOn = on;
    }

    public double getRadius() {
        return mRadius;
    }

    public void setRadius(double radius) {
        mRadius = radius;
    }

    public String getColor() {
        return mColor;
    }

    public void setColor(String color) {
        mColor = color;
    }

    public String toString() {
        if (isOn()) {
            return "Speed: " + getSpeed() + " Color: " + getColor() + " Radius: " + getRadius();
        } else {
            return "Color: " + getColor() + " Radius: " + getRadius() + ". The fan is OFF.";
        }
    }

}


public class Exercise_08 {

    public static void main(String[] args) {

        Fan fan1 = new Fan();
        fan1.setSpeed(Fan.FAST);
        fan1.setOn(true);
        fan1.setRadius(10);
        fan1.setColor("yellow");

        Fan fan2 = new Fan();
        fan2.setSpeed(Fan.MEDIUM);
        fan2.setRadius(5);
        fan2.setColor("blue");
        fan2.setOn(false);

        System.out.println("Fan #1: "+fan1.toString());
        System.out.println("Fan #2: "+fan2.toString());
    }
}

No comments :

Post a Comment