Design a class named Fan (Fan.java) to represent a fan. The class contains: An i
ID: 3805013 • Letter: D
Question
Design a class named Fan (Fan.java) to represent a fan. The class contains: An int field named speed that specifies the speed of the fan. A boolean field named fanStatus that specifies whether the fan is on (default false). A double field named radius that specifies the radius of the fan (default 5). A string field named color that specifies the color of the fan (default blue). A no-arg (default) constructor that creates a default fan. A constructor that creates a fan with four parameters: speed, fanStatus, radius, and color. A method named toString() that returns a string description for the fan. The method returns the fan speed, color, radius, and fan status (either on or off) in one combined string. Write a TestFan program that asks the user to enter the fan speed, radius, fan status, and color. The program creates a fan object by using the constructor with four parameters. Then it should display the fan description by invoking their toString() method.Explanation / Answer
Fan.java
public class Fan {
// Attributes of Fan Class
private int speed;
private boolean fanStatus = false;
private double radius = 5;
String color = "blue";
// default Constructor
public Fan() {
}
// Parameterized Constructor
public Fan(int speed, boolean fanStatus, double radius, String color) {
this.speed = speed;
this.fanStatus = fanStatus;
this.radius = radius;
this.color = color;
}
// Setters and Getters
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public boolean isFanStatus() {
return fanStatus;
}
public void setFanStatus(boolean fanStatus) {
this.fanStatus = fanStatus;
}
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;
}
// toString method used to return String data of Fan
@Override
public String toString() {
String onOrOff;
if (isFanStatus())
;
else
;
if (isFanStatus()) { // 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;
}
}
}
___________________
TestFan.java
import java.util.Scanner;
public class TestFan {
public static void main(String[] args) {
// Declaring variables
int speed;
boolean fanStatus;
double radius;
String color;
// Scanner object is used to get the inputs entered by the user
Scanner sc = new Scanner(System.in);
// Getting the inputs entered by the user
System.out.print("Enter the speed of the fan :");
speed = sc.nextInt();
System.out.print("Enter the Fan Status :");
fanStatus = sc.nextBoolean();
System.out.print("Enter the radius of the fan :");
radius = sc.nextDouble();
System.out.print("Enter the color of the fan :");
color = sc.next();
// Creating the Fan class object by passing the user inputs as arguments
Fan f = new Fan(speed, fanStatus, radius, color);
// Displaying the Fan class object Info
System.out.println(f.toString());
}
}
___________________
Output#1:
Enter the speed of the fan :3
Enter the Fan Status :true
Enter the radius of the fan :6.5
Enter the color of the fan :green
A 6.5 inch green fan at a speed of 3
_____________
Output#2:
Enter the speed of the fan :2
Enter the Fan Status :false
Enter the radius of the fan :4.0
Enter the color of the fan :orange
A 4.0 inch orange fan; fan is off
_____________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.