(Enhancing Class Rectangle) Create a more sophisticated Rectangle class than the one you created in Exercise 9.20. This class stores only the Cartesian coordinates of the four corners of the rectangle. The constructor calls a set function that accepts four sets of coordinates and verifies that each of these is in the first quadrant with no single x- or y-coordinate larger than 20.0. The set

func- tion also verifies that the supplied coordinates do, in fact, specify a rectangle. Provide member func- tions that calculate the length, width, perimeter and area. The length is the larger of the two dimensions. Include a predicate function square that determines whether the rectangle is a square.

What will be an ideal response?


```
#ifndef POINT_H
#define POINT_H

class Point
{
public:
Point( double = 0.0, double = 0.0 ); // default constructor

// set and get functions
void setX( double );
void setY( double );
double getX();
double getY();
private:
double x; // 0.0 <= x <= 20.0
double y; // 0.0 <= y <= 20.0
}; // end class Point

#endif
```
```
// Member-function definitions for class Point.
#include "Point.h" // include definition of class Point

Point::Point( double xCoord, double yCoord )
{
setX( xCoord ); // invoke function setX
setY( yCoord ); // invoke function setY
} // end Point constructor

// set x coordinate
void Point::setX( double xCoord )
{
x = ( xCoord >= 0.0 && xCoord <= 20.0 ) ? xCoord : 0.0;
} // end function setX

// set y coordinate
void Point::setY( double yCoord )
{
y = ( yCoord >= 0.0 && yCoord <= 20.0 ) ? yCoord : 0.0;
} // end function setY

// return x coordinate
double Point::getX()
{
return x;
} // end function getX

// return y coordinate
double Point::getY()
{
return y;
} // end function getY
```
```
#ifndef RECTANGLE_H
#define RECTANGLE_H

#include "Point.h" // include definition of class Point

class Rectangle
{
public:
// default constructor
Rectangle( Point = Point( 0.0, 1.0 ), Point = Point( 1.0, 1.0 ),
Point = Point( 1.0, 0.0 ), Point = Point( 0.0, 0.0 ) );

// sets x, y, x2, y2 coordinates
void setCoord( Point, Point, Point, Point );
double length(); // length
double width(); // width
void perimeter(); // perimeter
void area(); // area
bool square(); // square
private:
Point point1;
Point point2;
Point point3;
Point point4;
}; // end class Rectangle

#endif
```
```
// Member-function definitions for class Rectangle.
#include
#include
#include
#include "Rectangle.h" // include definition of class Rectangle
using namespace std;

Rectangle::Rectangle( Point a, Point b, Point c, Point d )
{
setCoord( a, b, c, d ); // invokes function setCoord
} // end Rectangle constructor

void Rectangle::setCoord( Point p1, Point p2, Point p3, Point p4 )
{
// Arrangement of points
// p4.........p3
// . .
// . .
// p1.........p2

// verify that points form a rectangle
if ( ( p1.getY() == p2.getY() && p1.getX() == p4.getX()
&& p2.getX() == p3.getX() && p3.getY() == p4.getY() ) )
{
point1 = p1;
point2 = p2;
point3 = p3;
point4 = p4;
} // end if
else
{
cout << "Coordinates do not form a rectangle!\n"
<< "Use default values.\n";
point1 = Point( 0.0, 1.0 );
point2 = Point( 1.0, 1.0 );
point3 = Point( 1.0, 0.0 );
point4 = Point( 0.0, 0.0 );
} // end else
} // end function setCoord

double Rectangle::length()
{
double side1 = fabs( point4.getY() - point1.getY() ); // get side1
double side2 = fabs( point2.getX() - point1.getX() ); // get side2
double length = ( side1 > side2 ? side1 : side2 );
return length;
} // end function length

double Rectangle::width()
{
double side1 = fabs( point4.getY() - point1.getY() ); // get side1
double side2 = fabs( point2.getX() - point1.getX() ); // get side2
double width = ( side1 < side2 ? side1 : side2 );
return width;
} // end function width

void Rectangle::perimeter()
{
cout << fixed << "\nThe perimeter is: " << setprecision( 1 )
<< 2 * ( length() + width() ) << endl;
} // end function perimeter

void Rectangle::area()
{
cout << fixed << "The area is: " << setprecision( 1 )
<< length() * width() << endl;
} // end function area

bool Rectangle::square()
{
return length() == width();
} // end function square
```
```
#include
#include "Rectangle.h" // include definition of class Rectangle
using namespace std;

int main()
{
Point w( 1.0, 1.0 );
Point x( 5.0, 1.0 );
Point y( 5.0, 3.0 );
Point z( 1.0, 3.0 );
Point j( 0.0, 0.0 );
Point k( 1.0, 0.0 );
Point m( 1.0, 1.0 );
Point n( 0.0, 1.0 );
Point v( 99.0, -2.3 );

Rectangle rectangles[ 4 ]; // array stores four rectangles

// output rectangles
for ( int i = 0; i < 4; i++ )
{
cout << "Rectangle" << i + 1 << ":\n";

switch ( i ) // initialize four different rectangles
{
case 0: // first rectangle
rectangles[ i ] = Rectangle( z, y, x, w );
break;
case 1: // second rectangle
rectangles[ i ] = Rectangle( j, k, m, n );
break;
case 2: // third rectangle
rectangles[ i ] = Rectangle( w, x, m, n );
break;
case 3: // fourth rectangle
rectangles[ i ] = Rectangle( v, x, y, z );
break;
} // end switch

cout << "length = " << rectangles[ i ].length();
cout << "\nwidth = " << rectangles[ i ].width();
rectangles[ i ].perimeter();
rectangles[ i ].area();
cout << "The rectangle "
<< ( rectangles[ i ].square() ? "is" : "is not" )
<< " a square.\n";
} // end for
} // end main
```
Rectangle1:
length = 4
width = 2
The perimeter is: 12.0
The area is: 8.0
The rectangle is not a square.
Rectangle2:
length = 1.0
width = 1.0
The perimeter is: 4.0
The area is: 1.0
The rectangle is a square.
Rectangle3:
Coordinates do not form a rectangle!
Use default values.
length = 1.0
width = 1.0
The perimeter is: 4.0
The area is: 1.0
The rectangle is a square.
Rectangle4:
Coordinates do not form a rectangle!
Use default values.
length = 1.0
width = 1.0
The perimeter is: 4.0
The area is: 1.0
The rectangle is a square.

Computer Science & Information Technology

You might also like to view...

When you delete elements of a picture, you can no longer display the original picture with all its elements.

Answer the following statement true (T) or false (F)

Computer Science & Information Technology

Specify queries (a), (b), (c), (e), (f), (i), and (j) of Exercise 6.16 in both tuple and domain relational calculus.

What will be an ideal response?

Computer Science & Information Technology

Review the integrated commercial provider email configuration information for iOS, Android, and Windows Phone devices.

What will be an ideal response?

Computer Science & Information Technology

Installation of an operating system is a common task today for a support specialist during a PC installation.

Answer the following statement true (T) or false (F)

Computer Science & Information Technology