Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Coded in C++ implement and use the methods for a class called Rectangle. Rectangle class This class represents a simple rectangle shape. Data Members The

Coded in C++

implement and use the methods for a class called Rectangle.

Rectangle class

This class represents a simple rectangle shape.

Data Members

The data members for the class are:

an integer that holds the height of the rectangle

an integer that holds the width of the rectangle

Constructors

This class has two constructors. The default constructor (the one that takes no arguments) should initialize both the height and width of the rectangle to 1. This should be done by calling the setDimension method that is described below.

The other constructor for the class should initialize the data members using the passed in arguments. It takes 2 arguments: an integer that holds the initial height of the rectangle and an integer that holds the initial width of the rectangle. As with the default constructor, the initialization should be done by passing the arguments to the setDimension method.

Methods

void setDimensions( int newHeight, int newWidth )

This is a public method that changes the dimensions of a Rectangle object. It takes 2 arguments: an integer that holds the new height of the rectangle and an integer that holds the new width of the rectangle. It returns nothing.

If the passed in height argument (newHeight in the header above) is less than 1, then the height data member should be set to 1. Otherwise, the passed in height argument should be used to change the height data member. If the passed in width argument (newWidth in the header above) is less than 1, then the width data member should be set to 1. Otherwise, the passed in width argument should be used to change the width data member.

int getHeight()

This is a public method that returns the height of the rectangle. It takes no argument. It returns an integer, which is the height data member.

int getWidth()

This is a public method that returns the width of the rectangle. It takes no argument. It returns an integer, which is the width data member.

void getDimensions( int &heightRef, int &widthRef )

This is a public method that passes back, using reference arguments, the height and width of the rectangle. It takes two arguments: a reference to an integer to pass back the height and a reference to an integer to pass back the width of the rectangle. It returns nothing.

void draw()

This is a public method that displays the rectangle "drawn" in hashtags (pound signs). It takes no argument and returns nothing.

For example, if a Rectangle object has height of 4 and a width of 12, then this method would display:

############ # # # # ############ 

void display()

This is a public method that displays a rectangle by displaying the dimensions (height and width) and drawing the rectangle in hashtags. It takes no argument and returns nothing.

This method should call the draw method to draw the rectangle in hashtags.

For example, if a Rectangle object has height of 4 and a width of 12, then this method would display:

Height: 4 Width: 12 ############ # # # # ############ 

bool isSquare()

This is a public method that determines if a rectangle is a square. It takes no argument. It returns a boolean value.

A rectangle is considered a square if the height and width are equal. If the height is equal to the width, then return true. Otherwise, return false.

bool isEqual( Rectangle otherRect )

This is a public method that determines if two Rectangle objects are equal. It takes 1 argument: a Rectangle object that will be used in the comparison. It returns a boolean value.

Two Rectangle objects are considered equal if their heights and widths are equal. If the current instance's height is equal to the passed in Rectangle object's (otherRect in the header above) height AND the current instance's width is equal to the passed in Rectangle object's width, then return true. Otherwise, return false.

The following condition will compare the current instance's height with the passed in Rectangle object's height:

 height == otherRect.height |____| |______________| | | current passed in Rectangle's instance height data height member data member 

Driver Program

A driver program that will test all of the methods can be found at:

-----------------------------------------------------------------------------------------------------------------------------------------------------------

#include #include using namespace std; //Put the Rectangle class definition after this line int main() { //Create 5 Rectangle objects using the two constructors Rectangle rectangle1(10, 32), rectangle2(21, 8), rectangle3, rectangle4(3, 33), rectangle5(3, 33); //Test 1: display the first rectangle object cout << "*** Test 1: display the first Rectangle object ***" << endl << endl; rectangle1.display(); //Test 2: display the height of the first rectangle object cout << endl << endl << endl << "*** Test 2: display ONLY the height of the first Rectangle object ***" << endl << endl; cout << "The height of the rectangle is " << rectangle1.getHeight() << endl; //Test 3: display the width of the second rectangle object cout << endl << endl << endl << "*** Test 3: display ONLY the width of the second Rectangle object ***" << endl << endl; cout << "The width of the rectangle is " << rectangle2.getWidth() << endl; //Test 4: getDimensions and setDimensions int rectHeight, rectWidth; cout << endl << endl << endl << "*** Test 4: get the dimensions of the third Rectangle object ***" << endl << endl; rectangle3.getDimensions( rectHeight, rectWidth ); cout << "The height of the rectangle is " << rectHeight << endl << "The width of the rectangle is " << rectWidth << endl << endl; rectangle3.setDimensions(12, 12); rectangle3.getDimensions( rectHeight, rectWidth ); cout << "After changing the dimensions..." << endl << endl << "The height of the rectangle is " << rectHeight << endl << "The width of the rectangle is " << rectWidth << endl << endl; //Test 5: isSquare cout << endl << endl << endl << "*** Test 5: are the third and fourth Rectangle objects squares? ***" << endl << endl; if( rectangle3.isSquare() ) cout << "The third rectangle is a square." << endl; else cout << "The third rectangle is NOT a square." << endl; if( rectangle4.isSquare() ) cout << endl << "The fourth rectangle is a square." << endl; else cout << endl << "The fourth rectangle is NOT a square." << endl; //Test 6: isEqual cout << endl << endl << endl << "*** Test 6a: are the fourth and first Rectangle objects equal? ***" << endl << endl; if( rectangle4.isEqual( rectangle1) ) cout << "The fourth rectangle is equal to the first rectangle." << endl; else cout << "The fourth rectangle is NOT equal to the first rectangle." << endl; cout << endl << endl << endl << "*** Test 6b: are the fourth and fifth Rectangle objects equal? ***" << endl << endl; if( rectangle4.isEqual( rectangle5) ) cout << "The fourth rectangle is equal to the fifth rectangle." << endl; else cout << "The fourth rectangle is NOT equal to the fifth rectangle." << endl; //Test 7: draw cout << endl << endl << endl << "*** Test 7: draw the fourth and fifth Rectangle objects ***" << endl << endl; cout << "The fourth Rectangle object" << endl; rectangle4.draw(); cout << endl << endl << "The fifth Rectangle object" << endl; rectangle5.draw(); return 0; } //Code the Rectangle constructors and methods after this line 

-----------------------------------------------------------------------------------------------------------------------------------------------------------

Programming Requirements

Each method must have a documentation box like a function.

Output

*** Test 1: display the first Rectangle object *** Height: 10 Width: 32 ################################ # # # # # # # # # # # # # # # # ################################ *** Test 2: display ONLY the height of the first Rectangle object *** The height of the rectangle is 10 *** Test 3: display ONLY the width of the second Rectangle object *** The width of the rectangle is 8 *** Test 4: get the dimensions of the third Rectangle object *** The height of the rectangle is 1 The width of the rectangle is 1 After changing the dimensions... The height of the rectangle is 12 The width of the rectangle is 12 *** Test 5: are the third and fourth Rectangle objects squares? *** The third rectangle is a square. This fourth rectangle is NOT a square. *** Test 6a: are the fourth and first Rectangle objects equal? *** The fourth rectangle is NOT equal to the first rectangle. *** Test 6b: are the fourth and fifth Rectangle objects equal? *** The fourth rectangle is equal to the fifth rectangle. *** Test 7: draw the fourth and fifth Rectangle objects *** The fourth Rectangle object ################################# # # ################################# The fifth Rectangle object ################################# # # #################################

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction To Constraint Databases

Authors: Peter Revesz

1st Edition

1441931554, 978-1441931559

More Books

Students also viewed these Databases questions

Question

4. What sales experience have you had?

Answered: 1 week ago