Question
please write in Java and include the two classes
Question 2: Fan.java and va Design a class named Fan (Fanjava) 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 toString0 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.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

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())
           onOrOff = "on";
       else
           onOrOff = "off";

       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

Add a comment
Know the answer?
Add Answer to:
please write in Java and include the two classes Design a class named Fan (Fan.java) to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • THE FOLLOWING PROGRAM IS NEEDED IN JAVA LANGUAGE Design a class torepresent account, include the following...

    THE FOLLOWING PROGRAM IS NEEDED IN JAVA LANGUAGE Design a class torepresent account, include the following members: -Data Members a)Name of depositor-Stringb)Account Number –intc)Type of account –Boolean d)Balance amount -doublee)AnnualInterestrate -double Methods: -(a)To assign initial values (use constructor)(b)To deposit an amount.(c)TO withdraw amount with the restriction the minimum balance is 50 rs. Ifyouwithdraw amount reduced the balance below 50 then print the error message.(d)Display the name and balance of the account.(e)Get_Monthly_intrestRate() -Return the monthly interestrate whichis nothing but Annualintrestrate/12. Annualinterestrate...

  • 1. Please write the following program in Python 3. Also, please create a UML and write...

    1. Please write the following program in Python 3. Also, please create a UML and write the test program. Please show all outputs. (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...

  • C++ FanClass.cpp Design a class named Fan to represent a fan. The class contains: Private section:...

    C++ FanClass.cpp Design a class named Fan to represent a fan. The class contains: Private section: An integer data field name speed that specifies the speed of the fan (1, 2, 3 or custom) A bool data field named on that specified whether the fan is on by default. A double data field named radius that specifies the radius of the fan A string data field named color that specifies the color of the fan. Public section: - Declare the...

  • Introduction to Java:Design a class named Triangle that extends GeometricObject.

    Please include comments in the program .The program must be able to be compiled.Design a class named Triangle that extends GeometricObject. This class contains:* Three double data fields named side1, side2, and side3 with default values 1.0 to denote the three sides of a triangle.* A no-arg constructor that creates a default triangle.* A constructor that creates a triangle with the specified side1, side2, and side3.* The accessor methods for all three data fields.* A method named getArea() that returns...

  • Language is JAVA. Clarification for the Shape class (highlighted): The following requirements specify what fields you...

    Language is JAVA. Clarification for the Shape class (highlighted): The following requirements specify what fields you are expected to implement in your Shape class; - A private String color that specifies the color of the shape - A private boolean filled that specifies whether the shape is filled - A private date (java.util.date) field dateCreated that specifies the date the shape was created Your Shape class will provide the following constructors; - A two-arg constructor that creates a shape with...

  • Help please 1) Define a Java class for defining the activity of a TV set. The...

    Help please 1) Define a Java class for defining the activity of a TV set. The class contains A private int data field named channel that specifies what channel is currently set for the tuner (channels range from 1 to 120, default is 1) a. b. A private int data field named volumeLevel that specifies the volume level of the TV (range is 1 to 7; default is 1) A private boolean data field named on that determines whether the...

  • JAVA design a class named Rectangle to represent a rectangle. The class contains: A method named getPerimeter() that returns the perimeter. Two double data fields named width and height that specify...

    JAVA design a class named Rectangle to represent a rectangle. The class contains: A method named getPerimeter() that returns the perimeter. 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 the specified width and height. A method named getArea() that returns the area of this rectangle. design...

  • Following the example of the Circle class in Section 8.2, design a class named Rectangle to...

    Following the example of the Circle class in Section 8.2, 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 the specified width and height. ■ A method named getArea() that returns the...

  • (The Account class) Design a class named Account that contains: A private int data field named...

    (The Account class) Design a class named Account that contains: A private int data field named id for the account (default 0). A private double data field named balance for the account (default 0). A private double data field named annualInterestRate that stores the current interest rate (default 0). Assume all accounts have the same interest rate. A private Date data field named dateCreated that stores the date when the account was created. A no-arg constructor that creates a default...

  • Problem 2 (36 pts): You are going to design a class named Computer. The class contains:...

    Problem 2 (36 pts): You are going to design a class named Computer. The class contains: A string field named manufacturer. A string field named serialNumber. A double field named speed (measured in Gigahertz). A constructor that has a parameter for each data field and sets the data fields with these values. A no-arg constructor that sets string fieldsto and numeric fields to 0. Mutator and Accessor methods for each data field. A method toString) that returns each field value...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT