QUESTION

C++ program

int main() is given in the link in overview

Overview

For this assignment, implement the methods for a class called Rectangle.

int main() has already been written for this assignment. It is available for download from Blackboard or by using the following link:

http://faculty.cs.niu.edu/~byrnes/csci240/pgms/240pgm9.cpp

Rectangle class

This class represents a simple rectangle shape. The Rectangle class definition should be placed at the top of the CPP file. The methods should be implemented after the closing curly brace for main().

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 2. 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

display

This is a public method that displays a rectangle by displaying the dimensions (height and width) of the rectangle.

It takes no argument and returns nothing.

For example, if a Rectangle object has a height data member with the value 4 and a width data member with the value 10, the method should display:

Height: 4 Width: 10

getHeight

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

getWidth

This is a public method that returns the width of the rectangle. It takes no argument. It returns an integer: 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.

Since the method is using reference arguments, the body of the method should contain two simple assignment statements. Each assignment statement should assign the appropriate data member to the corresponding reference argument.

setHeight

This is a public method that changes the height of a Rectangle object.

It takes one argument: an integer that holds the new height of the rectangle. It returns nothing.

If the passed in height argument is less than 2, the height data member should be set to 2. Otherwise, the passed in height argument should be used to change the height data member.

setWidth

This is a public method that changes the width of a Rectangle object.

It takes one argument: an integer that holds the new width of the rectangle. It returns nothing.

If the passed in width argument is less than 2, the width data member should be set to 2. Otherwise, the passed in width argument should be used to change the width data member.

setDimensions

This is a public method that changes the dimensions of a Rectangle object.

It takes two 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 is less than 2, the height data member should be set to 2. Otherwise, the passed in height argument should be used to change the height data member.

If the passed in width argument is less than 2, the width data member should be set to 2. Otherwise, the passed in width argument should be used to change the width data member.

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 data member is equal to the width data member, 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 data member is equal to the passed in Rectangle object's (otherRect in the header above) height data member AND the current instance's width data member is equal to the passed in Rectangle object's width data member, then return true. Otherwise, return false.

The "current instance" is the object that is on the left side of the dot notation when the isEqual method is called. In the method body, using the data member names with no extra notation will access the current instance.

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

bool isEqual( int otherHeight, int otherWidth )

This is a public method that determines if two Rectangle objects are equal. It takes 2 argumenta: an integer that holds the height of a Rectangle object and an integer that holds the width of a Rectangle object. It returns a boolean value.

As with the previous version of the isEqual method, two Rectangle objects are considered equal if their heights and widths are equal. For this version, the passed in integers should be compared with the corresponding data members.

draw

This is a public method that displays the rectangle "drawn" in asterisks.

It takes no argument and returns nothing.

For example, if a Rectangle object has height of 4 and a width of 10, this method should display:

**********

* *

* *

**********

Think about using a nested loop in this method. The outer loop will control how many rows are displayed for the rectangle. The inner loop will control how many columns are displayed for the rectangle. Inside of the inner loop, use a decision statement that displays either an asterisk or a space. At the end of the outer loop, display a newline character.

draw

This is a public method that displays the rectangle "drawn" in a specified character.

It takes one argument: a character that should be used to "draw" the rectangle. It returns nothing.

For example, if the passed-in character argument is a '%' and a Rectangle object has height of 5 and a width of 4, this method should display:

%%%%

% %

% %

% %

%%%%

Program Requirements

  1. Each method must have a documentation box like a function.
  2. Hand in a copy of the CPP file that includes the class definition and methods using Blackboard.

Output

*** Test 1: display all the Rectangle objects ***

Rectangle 1 -- Height: 15 Width: 24

Rectangle 2 -- Height: 17 Width: 6

Rectangle 3 -- Height: 2 Width: 2

Rectangle 4 -- Height: 3 Width: 2

Rectangle 5 -- Height: 3 Width: 23

*** Test 2a: display ONLY the height of the first Rectangle object ***

The height of Rectangle 1 is 15

*** Test 2b: display ONLY the width of the second Rectangle object ***

The width of Rectangle 2 is 6

*** Test 3a: change ONLY the height of the third Rectangle object ***

Rectangle 3 -- Height: 31 Width: 2

*** Test 3b: change ONLY the height of the third Rectangle object with invalid value ***

Rectangle 3 -- Height: 2 Width: 2

*** Test 4a: get the dimensions of the fourth Rectangle object ***

The height of Rectangle 4 is 3

The width of Rectangle 4 is 2

*** Test 4b: set the dimensions of the fourth Rectangle object with 27 x 27 ***

After changing the dimensions...

The height of Rectangle 4 is 27

The width of Rectangle 4 is 27

*** Test 4c: set the dimensions of the fourth Rectangle object with -47 x -47 ***

After changing the dimensions with negative numbers...

The height of Rectangle 4 is 2

The width of Rectangle 4 is 2

*** Test 5: are the second and fourth Rectangle objects squares? ***

Rectangle 2 is NOT a square.

Rectangle 4 is a square.

*** Test 6a: are the fourth and third Rectangle objects equal? ***

Rectangle 4 is equal to Rectangle 3.

*** Test 6b: are the fourth and fifth Rectangle objects equal? ***

Rectangle 4 is NOT equal to Rectangle 5.

*** Test 6c: is the second Rectangle object equal to a rectangle with height 6 and width 24? ***

Rectangle 2 is NOT equal to a rectangle with height 6 and width 24.

*** Test 6d: is the second Rectangle object equal to a rectangle with height 17 and width 6? ***

Rectangle 2 is equal to a rectangle with height 17 and width 6.

*** Test 7: draw the second and fourth Rectangle objects ***

Rectangle 2 object

Height: 17 Width: 6

******

* *

* *

* *

* *

* *

* *

* *

* *

* *

* *

* *

* *

* *

* *

* *

******

Rectangle 4 object

Height: 7 Width: 16

++++++++++++++++

+ +

+ +

+ +

+ +

+ +

++++++++++++++++

Public Answer

BDGP9B The First Answerer