Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Design a class in Java called Fan to represent a fan. The Fan class contains : 3 constants named SLOW, MEDIUM, FAST with values 1,

Design a class in Java called Fan to represent a fan. The Fan class contains :

3 constants named SLOW, MEDIUM, 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 (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 private string data field named color that specifies the color of the fan (default is blue)

Accessor and mutator methods for all four data fields.

A no-arg constructor

A parameter constructor

methods toString and equals

Question 1b: Write a test program that creates an array of Fan objects and fills the array with values. Iterate through the array and print whether any two fans are identical, using your equals method. Display the values in the array using the toString method.

Question 1c: Write a method that accepts a Fan object as a parameter and asks the user whether the fan should be turned on. If the user responds yes, then the fan is turned on.

Question 1d: Design and implement a CeilingFan class that inherits the fan class. The additional state should be the number of blades in the fan and the company of the fan. You should override both the equals and the toString methods.

I mostly need help with the last 3 questions, 1b, 1c and 1d.

But I found this for the rest of the code:

package Fan; public class Fan { static final int SLOW = 1; // static final attributes with constant alues static final int MEDIUM = 2; static final int FAST = 3; //Attributes of Fan Class private int speed=SLOW; private boolean isFanOn=false; private double radius=5; String color="blue"; //Constructor which stores default values public Fan(){ speed = 0; isFanOn = false; radius = 0.0; color = ""; } // toString method used to return String data of Fan public String toString(){ String onOrOff; if(isFanOn()) onOrOff="on"; else onOrOff="off"; if(isFanOn){ // Checking Whether Fan is on/off return "A "+this.getRadius()+" inch "+this.getColor()+" fan at a speed of "+this.getSpeed(); }else{ return "A "+this.radius+" inch "+this.getColor()+" fan; fan is "+onOrOff; } } //Setters and Getters public int getSpeed() { return speed; } public void setSpeed(int speed) { this.speed = speed; } public boolean isFanOn() {

return isFanOn; } public void setFanOn(boolean isFanOn) { this.isFanOn = isFanOn; } public double getRadius() { return radius; } public void setRadius(double radius) { this.radius = radius; } public String getColor() { return color; } public void setColor(String color) { this.color = color; }

public static void main(String[] args) { Fan fan1 = new Fan(); //Creating Fan object1 fan1.setSpeed(Fan.FAST); fan1.setRadius(10); fan1.setColor("Yellow"); fan1.setFanOn(true); System.out.println(fan1.toString()); Fan fan2 = new Fan(); //Creating Fan object2 fan2.setSpeed(Fan.MEDIUM); fan2.setRadius(5); fan2.setColor("Blue"); fan2.setFanOn(false); System.out.println(fan2.toString()); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

the blank cloud type cannot be combined with a prefix

Answered: 1 week ago

Question

Describe how to train managers to coach employees. page 404

Answered: 1 week ago

Question

Discuss the steps in the development planning process. page 381

Answered: 1 week ago