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 x,y,height,width;
Rectangle()
{
x=0;
y=0;
height=1;
width=1;
}
Rectangle(double x,double y,double height,double width)
{
this.x=x;
this.y=y;
this.height=height;
this.width=width;
}
double getArea()
{
return height*width;
}
double getPerimeter()
{
return 2*(height+width);
}
boolean point(double x,double y)
{
double pointX = x;
double pointY = y;
if (pointX < (this.x + (this.width)) && pointX > (this.x - (this.width)) &&
pointY < (this.y + (this.height)) && pointY > (this.y - (this.height)))
return true;
else
return false;
}
public static void main(String [] args)
{
Rectangle obj=new Rectangle();
Rectangle obj1=new Rectangle(5,2,10,50);
System.out.println("For default constructor");
System.out.println("Area = "+obj.getArea());
System.out.println("perimeter = "+obj.getPerimeter());
System.out.println("point inside rectangle ="+obj.point(0,0));
System.out.println("For default constructor");
System.out.println("Area = "+obj1.getArea());
System.out.println("perimeter = "+obj1.getPerimeter());
System.out.println("point inside rectangle ="+obj1.point(5,2));
}
}
output
0 comments :
Post a Comment