Question

In: Computer Science

Design a class named Fan to represent a fan. The class contains: ■ Three constants named...

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.

Solutions

Expert Solution

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.


Related Solutions

Python Please (The Fan class) Design a class named Fan to represent a fan. The class...
Python Please (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. ■ A private bool data field named on that specifies whether the fan is on (the default is False). ■ A private float data field named radius that...
c++ E2b: Design a class named Rectangle to represent a rectangle. The class contains:
using c++E2b: Design a class named Rectangle to represent a rectangle. The class contains:(1) Two double data members named width and height which specifies the width and height of the rectangle .(2) A no-arg constructor that creates a rectangle with width 1 and height 1.(3) A constructor that creates a rectangle with the specified width and height .(4) A function named getArea() that returns the area of this rectangle .(5) A function named getPerimeter() that returns the perimeter of this...
Design a class named GeoPoint to represent a point with x- and y-coordinates. The class contains:  ...
Design a class named GeoPoint to represent a point with x- and y-coordinates. The class contains:   The data fields x and y that represent the coordinates with gette and setter methods. A no-arg constructor that creates a point (0, 0).   A constructor that constructs a point with specified coordinates. The method equals(GeoPoint p) that returns true if two GeoPoint objects have the same x- and y-coordinates. Write a test program that creates an array of GeoPoint objects. The size of...
python Design a class named IP_address to represent IP address objects. The IP_addressclass contains the following...
python Design a class named IP_address to represent IP address objects. The IP_addressclass contains the following A number of instance variables/fields to store a table of data. You can design them by your own. A constructor that creates a table with the following: a list of data. ip address a integer to indicate the number of elements in the sum_list/freq_list/average_list A get_ip_address() method that returns the ip address For example, consider the following code fragment: ip_key = '192.168.0.24' data_list =[(0,...
Python Design a class named IP_address to represent IP address objects. The IP_addressclass contains the following...
Python Design a class named IP_address to represent IP address objects. The IP_addressclass contains the following A number of instance variables/fields to store a table of data. You can design them on your own. A constructor that creates a table with the following: a list of data. IP address an integer to indicate the number of elements in the sum_list/freq_list/average_list A get_ip_address() method that returns the IP address For example, consider the following code fragment: ip_key = '192.168.0.24' data_list =[(0,...
Python Design a class named IP_address to represent IP address objects. The IP_addressclass contains the following...
Python Design a class named IP_address to represent IP address objects. The IP_addressclass contains the following A number of instance variables/fields to store a table of data. You can design them on your own. A constructor that creates a table with the following: a list of data. IP address an integer to indicate the number of elements in the sum_list/freq_list/average_list A get_ip_address() method that returns the IP address For example, consider the following code fragment: ip_key = '192.168.0.24' data_list =[(0,...
Design a class named Message to represent a sentence or phrase. The class will contain: •...
Design a class named Message to represent a sentence or phrase. The class will contain: • a private string data field to hold the sentence or phrase. • A no-arg constructor with an empty string message. • A constructor that create a message object with the specified string sentence or phrase. • Accessor and mutator (getter/setter) for string data field. • A method named getVowels ( ) that returns the number of vowels in a sentence or phrase. • A...
Following the example of Circle class, design a class named Rectangle to represent a rectangle. The...
Following the example of Circle class, design a class named Rectangle to represent a rectangle. The class contains: Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height. A no-arg constructor that creates a default rectangle. A constructor that creates a rectangle with specified width and height A method name getWidth() return the value of width A method named getHeight() returns value of...
Following the example of Circle class, design a class named Rectangle to represent a rectangle. The...
Following the example of Circle class, design a class named Rectangle to represent a rectangle. The class contains: Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height. A no-arg constructor that creates a default rectangle. A constructor that creates a rectangle with specified width and height A method name getWidth() return the value of width A method named getHeight() returns value of...
In Java, design a class named MyInteger. The class contains: An int data field named value...
In Java, design a class named MyInteger. The class contains: An int data field named value that stores the int value represented by this object. A constructor that creates a MyInteger object for the specified int A get method that returns the int Methods isEven(), isOdd(), and isPrime() that return true if the value is even, odd, or prime, respectively. Static methods isEven(int), isOdd(int), and isPrime(int) that return true if the specified value is even, odd, or prime, respectively. Static...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT