Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Laboratory: Create a class Rectangle, which stores only Cartesian coordinates of

ID: 3914308 • Letter: L

Question

Laboratory: Create a class Rectangle, which stores only Cartesian coordinates of four corners of a rectangle as arrays shown below: p4lx4.y4] p3lx3.y3] pllxl,y1] p2lx2,y2] The constructor calls a set function that accepts four sets of coordinates. The set function verifies that the supplied coordinates do, in fact, forma rectangle Provide member functions that calculate length, width, perimeter, and area. The length is larger of the two dimensions. Include a predicate function square that determines whether the rectangle is a square. Write a program for the above described rectangle class, compile it, execute it, and demonstrate to

Explanation / Answer

#include <iostream>

using namespace std;

class Rectangle

{

private:

int p1[2], p2[2], p3[2], p4[2];

public:

// constructor

Rectangle(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4)

{

p1[0] = x1;

p1[1] = y1;

p2[0] = x2;

p2[1] = y2;

p3[0] = x3;

p3[1] = y3;

p4[0] = x4;

p4[1] = y4;

set(p1, p2, p3, p4);

}

// checking if the coordinates are forming a rectangle or not

void set(int one[], int two[], int three[], int four[])

{

if (four[1]==three[1] && one[2]==two[2] && four[0]==one[0] && two[0]==three[0])

cout << "Valid Rectangle" << endl;

else

cout << "Invalid Rectangle" << endl;

}

// calculating the width

int calcLength()

{

return p1[1]-p4[1];

}

// calculating the width

int calcWidth()

{

return p2[0]-p1[0];

}

//calculating the area

int area()

{

return calcLength()*calcWidth();

}

// calculating the perimeter

int perimeter()

{

return 2*(calcLength()+calcWidth());

}

// TO check if it is a square

void isSquare()

{

if (calcLength() == calcWidth())

cout << "It's a square"<< endl;

else

cout << "It's not a square" << endl;

}

};

int main() {

// SAMPLE RUN

Rectangle a(0, 4, 6, 4, 6, 0, 0, 0);

cout << "Width is " << a.calcWidth() << endl;

cout << "Length is " << a.calcLength() << endl;

cout << "Area is " << a.area() << endl;

cout << "Perimeter is " << a.perimeter() << endl;

a.isSquare();

}

/*SAMPLE OUTPUT

Valid Rectangle

Width is 6

Length is 4

Area is 24

Perimeter is 20

It's not a square

*/