Welcome to Java Examples

Take a cup of tea and Let's Start programming

Design a class named Fan to represent a fan. The class contains:
- Three constants named SLOW, MEDIUM and FAST with values 1,2 and 3 to denote the fan speed.
- An int data field named speed that specifies the speed of the fan (default SLOW).
- A boolean data field named f_on that specifies whether the fan is on(default false).
- A double data field named radius that specifies the radius of the fan (default 4).
- A data field named color that specifies the color of the fan (default blue).
- A no-arg constructor that creates a default fan.
- A parameterized constructor initializes the fan objects to given values.
- A method named display() will display description for the fan. If the fan is on, the display() method displays speed, color and radius. If the fan is not on, the method returns fan color and radius along with the message “fan is off”.
Write a test program that creates two Fan objects. One with default values and the other with medium speed, radius 6, color brown, and turned on status true. Display the descriptions for two created Fan objects.


class Fan
{
public static final int SLOW=1,MEDIUM=2,FAST=3;
int speed;
boolean f_on;
double radius;
String color;

Fan()
{
speed=SLOW;
f_on=false;
radius=4;
color="blue";
}

Fan(int speed,double radius,String color,boolean f_on)
{
this.speed=speed;
this.radius=radius;
this.color=color;
this.f_on=f_on;
}

void display()
{
if(f_on==true)
{
System.out.println("Fan is on \n the speed is ="+speed+"\n the color is ="+color+"\n the radius is ="+radius);
}
else
{
System.out.println("Fan is off \n the color of fan is ="+color+"\n the radius of fan is ="+radius);
}
}

public static void main(String [] args)
{
Fan obj = new Fan();
Fan obj1 = new Fan(MEDIUM,6,"brown",true);
obj.display();
obj1.display();

}
}


Output

0 comments :

Post a Comment