Define the Rcctanglc2D class that contains: Double data fields named xpos, ypos
ID: 3851555 • Letter: D
Question
Define the Rcctanglc2D class that contains: Double data fields named xpos, ypos (that specify the top left corner of the rectangle), width and height (assume that the rectangle sides arc parallel to the x and y axes. Sec example below). A no-arg constructor that creates a default rectangle with (0.0, 0.0) for (xpos, ypos0 and 1.0 for both width and height. A constructor that creates a rectangle with the specified xpos, ypos, width, and height. Get and set methods for all the instance variables. A method getArea() that returns the area of the rectangle. A method getPerimeter() that returns the perimeter of the rectangle. A method contains(x, y) that returns true if the specified point (x, y) is inside this rectangle. Sec Figure 1(a). A method contains(Point p) that returns true if the specified Point (see Exercise 1) has coordinates inside the rectangle. (This is also similar to Figure 1(a).) A method contains (Point p) that returns true if the specifie4d Point (see Exercise 1) has coordinates inside the rectangle. (This is also similar to Figure 1(A). A method contains(Rectangle2D r) that returns true if the specified Point (See Exercise 1) has coordinates inside the rectangle. (This is also similar to Figure 1(a)). A method contains(Rectangle 2D r) that returns true if the specified rectangle is inside this rectangle. See Figure 1(b). Note that the condition for contains is that all four corners of the second rectangle must be contained in the first. This means, that if the two rectangles are exactly identical, we still say that the second rectangle is contained within the other.Explanation / Answer
#include <iostream>
using namespace std;
//Point class for coordinates a and y
class Point
{
private:
double x,y;
public:
Point(double x,double y)//argument constructor
{
this->x = x;
this->y = y;
}
//get methods
double getX()
{
return x;
}
double getY()
{
return y;
}
};
// Rectangle class
class Rectangle2D
{
private:
double xpos,ypos,width,height;
public:
Rectangle2D() //default constructor
{
xpos = 0.0;
ypos = 0.0;
width = 1.0;
height = 1.0;
}
Rectangle2D(double xpos,double ypos,double width,double height)//argument constructor
{
this->xpos = xpos;
this->ypos = ypos;
this->width = width;
this->height = height;
}
//set and get methods for class variables
double getXpos()
{
return xpos;
}
void setXpos(double xpos)
{
this->xpos = xpos;
}
double getYpos()
{
return ypos;
}
void setYpos(double xpos)
{
this->ypos = ypos;
}
double getWidth()
{
return width;
}
void setWidth(double width)
{
this->width = width;
}
double getHeight()
{
return height;
}
void setHeight(double height)
{
this->height = height;
}
//area and perimeter methods
double getArea()
{
return width * height;
}
double getPerimeter()
{
return 2*(width + height);
}
// contains method check if x and y coordinates are inside the Rectangle2D
bool contains(double x,double y)
{
if(x >= xpos && x <= width && y>= ypos && y<= height )
return true;
else
return false;
}
// contains method check if Point class object p is inside the Rectangle2D
bool contains(Point p)
{
if(p.getX() >= xpos && p.getX() <= width && p.getY() >= ypos && p.getY() <= height )
return true;
else
return false;
}
//overloaded contains method check if one rectangle object is inside the other
bool contains(Rectangle2D r)
{
if(r.getXpos() >= xpos && r.getXpos() <= width && r.getYpos() >= ypos && r.getYpos() <= height )
return true;
else
return false;
}
};
int main()
{
Rectangle2D r1(1.0,0.5,3.0,2.0);
Rectangle2D r2(1.4,0.7,2.0,1.0);
Point p1(0.75,1.5);
cout<<"Rectangle r1 contains point p1 : ";
if(r1.contains(1.2,1.5) == 1)
cout<<"true";
else
cout<<"false";
cout<<" Rectangle r1 contains Rectangle r2 : ";
if(r1.contains(r2) == 1)
cout<<"true";
else
cout<<"false";
cout<<" Area of rectangle r1 = "<<r1.getArea()<<" square units";
cout<<" Perimeter of rectangle r1 = "<<r1.getPerimeter()<<" units";
return 0;
}
Output:
Rectangle r1 contains point p1 : true
Rectangle r1 contains Rectangle r2 : true
Area of rectangle r1 = 6 square units
Perimeter of rectangle r1 = 10 units
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.