Overview For this assignment, implement and use the methods for a class called R
ID: 3720495 • Letter: O
Question
Overview
For this assignment, 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:
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:
Driver Program
A driver program that will test all of the methods can be found at:
http://faculty.cs.niu.edu/~byrnes/csci240/pgms/240pgm10driver.cpp
Programming Requirements
Each method must have a documentation box like a function.
Hand in a copy of the driver program that includes the class definition and methods using Blackboard.
Output
Explanation / Answer
#include <iostream>
#include <iomanip>
#include<string>
using namespace std;
//Put the Rectangle class definition after this line
class Rectangle
{
private:
int height, width;
public:
Rectangle();
Rectangle(int h, int w);
void setDimensions(int newHeight, int newWidth);
int getHeight();
int getWidth();
void getDimensions(int &heightRef, int &widthRef);
void draw();
void display();
bool isSquare();
bool isEqual(Rectangle otherRect);
};
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();
system("pause");
return 0;
}
Rectangle::Rectangle()
{
setDimensions(1, 1);
}
Rectangle::Rectangle(int h, int w)
{
setDimensions(h, w);
}
void Rectangle::setDimensions(int newHeight, int newWidth)
{
if (newHeight < 0 || newWidth < 0)
{
height = 1;
width = 1;
}
else
{
height = newHeight;
width = newWidth;
}
}
int Rectangle::getHeight()
{
return height;
}
int Rectangle::getWidth()
{
return width;
}
void Rectangle::getDimensions(int &heightRef, int &widthRef)
{
heightRef = height;
widthRef = width;
}
void Rectangle::draw()
{
// Draw row of 'w' asterisks for top side
cout << string(width, '#') << endl;
// Draw sides: asterisk, width-2 spaces, asterisk
for (int j = 0; j < height - 2; ++j)
cout << '#' << string(width - 2, ' ') << '#' << endl;
// Draw row of 'w' asterisks for bottom side
cout << string(width, '#') << endl;
}
void Rectangle::display()
{
cout << "Height: " << height << " Width: " << width << endl;
draw();
}
bool Rectangle::isSquare()
{
if (height == width)
return true;
else
return false;
}
bool Rectangle::isEqual(Rectangle otherRect)
{
if (this->height == otherRect.height && this->width == otherRect.width)
return true;
else
return false;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.