Question 2

(b) 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;
public static final int MEDIUM = 2;
public static final int FAST = 3;
int speed;
boolean f_on;
double radius;
String color;
Fan()
{
speed = SLOW;
f_on = false;
radius = 4;
color = "blue";
}
Fan(int s,boolean f, double r, String c)
{
speed = s;
f_on = f;
radius = r;
color = c;
}
public void display()
{
if(f_on)
{
System.out.println("Speed of Fan : "+speed);
System.out.println("Radius of Fan : "+radius);
System.out.println("Color of Fan : "+color);
}
else
{
System.out.println("Fan is Off.");
System.out.println("Radius of Fan : "+radius);
System.out.println("Color of Fan : "+color);
}
}
}
class Q2bFanClassH2E
{
public static void main(String args[])
{
Fan f1 = new Fan();
f1.display();
Fan f2 = new Fan(2,true,6,"brown");
f2.display();
}
}
(b) Define the Rectangle class that contains:
Two double fields x and y that specify the center of the rectangle, the data field width and height ,
A no-arg constructor that creates the default rectangle with (0,0) for (x,y) and 1 for both width and height.
A parameterized constructor creates a rectangle with the specified x,y,height and width.
A method getArea() that returns the area of the rectangle.
A method getPerimeter() that returns the perimeter of the rectangle.
A method contains(double x, double y) that returns true if the specified point (x,y) is inside this rectangle.
Write a test program that creates two rectangle objects. One with default values and other with user specified values. Test all the methods of the class for both the objects.
class Rectangle
{
double centerX,centerY,width,height;
double x1,x2,x3,x4,y1,y2,y3,y4;
Rectangle()
{
centerX=centerY=0;
width=height=1;
}
Rectangle(double x,double y,double w,double h)
{
centerX=x;
centerY=y;
width=w;
height=h;
}
double getArea()
{
return (width*height);
}
double getPerimeter()
{

return ( 2*(width+height) );
}
boolean contains(double x,double y)
{
x1=x3= ( centerX - (width/2) );
x2=x4= ( centerX + (width/2) );
y1=y2= (centerY + (height/2));
y3=y4= (centerY - (height/2));
if( (x > x1 && x < x4) && (y < y1 && y > y3))
return true;
else
return false;
}
}
class RectangleH2E
{
public static void main(String args[])
{
Rectangle r1Obj = new Rectangle();
Rectangle r2Obj = new Rectangle(200,200,100,50);
System.out.println("Rectangle 1 : Area "+r1Obj.getArea());
System.out.println("Rectangle 1 : Perimeter "+r1Obj.getPerimeter());
System.out.println("Rectangle 1 : Contains (3,2) "+r1Obj.contains(3,2));
System.out.println();
System.out.println("Rectangle 2 : Area "+r2Obj.getArea());
System.out.println("Rectangle 2 : Perimeter "+r2Obj.getPerimeter());
System.out.println("Rectangle 2 : Contains (160,190) "+r2Obj.contains(160,190));
}
}

Make Comments..!!


Oops!! No posts from user.

Download Android App