Question: class Point { public: double x; double y; }; 7. Given the denition of the geometric Point class above add a method named distance: class
class Point {
public:
double x;
double y;
};
7. Given the denition of the geometric Point class above add a method named distance:
class Point {
// Other details omitted
// Returns the distance from this point to the // parameter p double distance(Point p) {
// Details go here
} };
that returns the distance between the point on whose behalf the method is called and the parameter p.
8. Consider the following C++ code:
#include
class IntPoint { public: int x; int y;
IntPoint(int x, int y): x(x), y(y) {}
};
class Rectangle {
IntPoint corner; // Location of the rectangle's lower-left corner
int width; // The rectangle's width
int height; // The rectangle's width public: Rectangle(IntPoint pt, int w, int h):
corner((pt.x < -100) ? -100 : (pt.x > 100 ? 100 : pt.x),
(pt.y < -100) ? -100 : (pt.y > 100 ? 100 : pt.y)),
width((w < 0) ? 0 : w), height((h < 0) ? 0 : h) {} int perimeter() {
return 2*width + 2*height;
} int area() {
return width * height;
} int get_width() {
return width;
} int get_height() {
return height;
} // Returns true if rectangle r overlaps this
// rectangle object. bool intersect(Rectangle r) {
// Details omitted
}
// Returns the length of a diagonal rounded to the nearest
// integer. int diagonal() {
// Details omitted
} // Returns the geometric center of the rectangle with
// the (x,y) coordinates rounded to the nearest integer.
IntPoint center() {
// Details omitted
} bool is_inside(IntPoint pt) {
// Details omitted
}
}; int main() {
Rectangle rect1(IntPoint(2, 3), 5, 7),
rect2(IntPoint(2, 3), 1, 3),
rect3(IntPoint(2, 3), 15, 3),
rect4(IntPoint(2, 3), 5, 3);
std::cout << rect1.get_width() << ' ';
std::cout << rect1.get_height() << ' ';
std::cout << rect2.get_width() << ' ';
std::cout << rect2.get_height() << ' ';
std::cout << rect3.get_width() << ' ';
std::cout << rect3.get_height() << ' ';
std::cout << rect4.get_width() << ' ';
std::cout << rect4.get_height() << ' ';
std::cout << rect1.get_perimeter() << ' ';
std::cout << rect1.get_area() << ' ';
std::cout << rect2.get_perimeter() << ' ';
std::cout << rect2.get_area() << ' ';
std::cout << rect3.get_perimeter() << ' ';
std::cout << rect3.get_area() << ' ';
std::cout << rect4.get_perimeter() << ' ';
std::cout << rect4.get_area() << ' ';
}
(b) With regard to a Rectangle objects lower-left corner, what are the minimum and maximum values allowed for the x coordinate? What are the minimum and maximum values allowed for the y coordinate?
(e) What happens when a client attempts to create a Rectangle object with parameters that are outside the acceptable ranges?
(f) Implement the diagonal method.
(g) Implement the center method.
(i) Implement the is_inside method.
(j) Complete the following function named corner:
IntPoint corner (Rectangle r) {
// Details go here
};
that returns the lower-left corner of the Rectangle object r passed to it. You may not modify theRectangle class.
9. Develop a Circle class that, like the Rectangle class above, provides methods to compute perimeter and area. The Rectangle instance variables are not appropriate for circles; specically, circles do have corners, and there is no need to specify a width and height. A center point and a radius more naturally describe a circle. Build your Circleclass appropriately.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
