Write a program that will create a list of axis-aligned rectangles dened in a 2D
ID: 3533887 • Letter: W
Question
Write a program that will create a list of axis-aligned rectangles dened in a 2D space,
i.e. the x-y space. An axis-aligned rectangle has two vertical sides (left and right) that
are parallel to the y-axis and two horizontal sides (top and bottom) that are parallel to
the x-axis.(See www:wikipedia:org for a formal dention). Each rectangle is dened by the
(x, y) location of its bottom left corner, its length (along the x-axis), and its height (along
1the y-axis). In your program, each rectangle will also have its own unique name. After
the description of each rectangle is input from the user and stored in a list (vector of class
Rectangle), your program will compute each rectangle's area, perimeter, and midpoint. In
addition, you will scale each rectangle by a factor of 2 about its midpoint.
Implement the following algorithm in the main function. Implement the algorithm one
function at a time, writing the function, checking that it compiles and runs properly and
then implement the next function.
i. Display welcome banner
ii. WHILE user input is invalid /* Prompt and read rst rectangle's name*/
iii. Display "Try again! "
iv. IF user input is not 'stop'
v. Extract rectangle name from user input
vi. Prompt for bottom left point
vii. Prompt for length and height
viii. Add new rectangle to the rectangle list
ix. WHILE user input is not 'stop'
x. Display "Thank you! "
xi. WHILE user input is invalid /* Prompt and read next rectangle's name */
xii. Display "Try again! "
xiii. IF user input is not 'stop'
xiv. Extract rectangle name from user input
xv. Prompt for bottom left point
xvi. Prompt for length and height
xvii. Add new rectangle to the rectangle list
xviii. IF the rectangle list is not empty
xix. Display all rectangles in the rectangle list
xx. ELSE
xxi. Display that no rectangles are in the list
2In the code template you will nd two class denitions. The class Point has two data
members to represent an (x, y) location. This will be used to represent the bottom left
corner of a rectangle and its midpoint. The class attributes and member functions for the
class Point are already completed for you. You do not need to make any changes to
this class.
The class Rectangle has four data members: name, class Point object, length, and height.
The class Point object stores the bottom left corner location of a class Rectangle object. The
member functions are also dened for you, but you will have to write the correct code for
each one. I have indicated these places with // replace with your code. Do not add
or remove any data members or member functions of the class Rectangle.
1. All functions should be written AFTER the main procedure.
2. All function prototypes should be written for each function and placed BEFORE the
main procedure.
3. Each function should have a comment explaining what it does.
4. Each function parameter should have a comment explaining the parameter.
5. You will write the following functions and implement the main function using the
algorithm above. The function descriptions specify the exact input parameters and
return types for the functions. You must follow the specications outlined here to
solve your homework. Though, you should carefully decide whether input parameters
are passed by value or by reference.
6. Write a function which displays the welcome banner. The function has no input pa-
rameters and has a return type of void. Use this in step (i) of the algorithm.
7. Write a function that prompts and reads from the user the name of the rectangle or the
word stop and validates this user input. The user will enter the name for a rectangle
by typing the keyword rec followed by a single space and then the name, e.g., when the
user enters rec john (this is valid input), the name of the rectangle will be assigned
to john. Note that just typing john is invalid input. If the user types stop then
this indicates that the user does not want to enter any more rectangles. This is valid
input. The user is not allowed to input a name for a rectangle that is already used for
a previously entered rectangle. This is invalid input. Any other input is also invalid.
This function has a return type of bool and returns true only if the user input entered
was valid.
The function has ve input parameters. A string that holds the prompt that asks for
the name of the rectangle. A string that holds an error message to display if the user
types invalid input. A string that holds an error message to display if the user types
a name that is already being used. A string that will pass back the user input, i.e.
3whatever the user entered. Lastly, a vector of rectangles, i.e. a list, containing all
rectangles entered so far. Use this function in steps (ii) and (xi).
8. Write a function that prompts and reads the x and y coordinates of the bottom left
corner of a rectangle. The function has return type of void. It has three input param-
eters. A string that holds the prompt that asks for user input. A double for passing
back the entered x coordinate. A double for passing back the entered y coordinate.
This function is used in steps (vi) and (xv).
9. Write a function that prompts and reads the length and height of a rectangle. The
function has return type of void. It has three input parameters. A string that holds
the prompt that asks for user input. A double for passing back the length. A double
for passing back the height. This function should continue prompting the user until
positive values are ented for both values. Use this function in steps (vii) and (xvi).
10. Write a function that adds a rectangle to the back of a vector of rectangles. First, the
function will set the values (name, location, length, and height) of the rectangle into
a class Rectangle object. Second, it will add this object to the back of the vector of
rectangles. The function has a return type of void. It has six input parameters. A
string holding the name of the rectangle. A double holding the x coordinate of the
bottom left corner. A double holding the y coordinate of the bottom left corner. A
double holding the length. A double holding the height. Lastly, a vector of rectangles
containing all rectangles entered so far. Use this function in steps (viii) and (xvii).
11. Write a function that displays all the rectangles in the vector of rectangles. This
function has a return type of void. It has a single input parameter which is a vector
of rectangles containing all rectangles entered so far. Use this function in step (xix).
12. Implement the set and get member functions of the class Rectangle (see examples
found in the lecture notes and textbook reading).
13. Implement the member function area() of the class Rectangle that computes the area
of the rectangle object. Use the appropriate class attributes of the class Rectangle.
14. Implement the member function perimeter() of the class Rectangle that computes
the perimeter of the rectangle object. Use the appropriate class attributes of the class
Rectangle.
15. Implement the member function midPoint() of the class Rectangle that computes
and returns the midpoint of the rectangle object (returned as a class Point object).
The midpoint is the (x, y) point located at the center of a rectangle.
16. Implement the member function scaleBy2 of the class Rectangle that scales the
rectangle object by a factor of 2 around its midpoint. Note that the length and height
4of the rectangle are doubled and the bottom left corner location may change. Though,
the midpoint should not change, since we are scaling about this point. For example, if
a rectangle has a bottom left corner location of (1, 1), a length of 2, and a height of 4,
its new bottom left corner location is (0, -1), its new length is 4, and its new height is
8. The midpoint is still at its original location (2, 3). This function must change the
rectangle object's class attributes to re
ect the scaled up rectangle.
17. Implement the member function display() of the class Rectangle that displays all
information about a rectangle object. Call this function within the function you are
writing for step (xix) of the algorithm above.
18. Be sure to modify the header comments "File", "Created by", "Creation Date", and
"Synopsis" at the top of the le. Each synopsis should contain a brief description of
what the program does.
19. Be sure that there is a comment documenting each variable.
20. Be sure that your if statements, for and while loops and blocks are properly indented.
21. Check your output against the output from the solution executable provided.
Explanation / Answer
This the code
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Point
{
private:
double px;
double py;
public:
void setX(const double x);
void setY(const double y);
double getX() const;
double getY() const;
};
class Rectangle
{
private:
string name;
Point blPoint;
double length, height;
public:
// member functions
void setName(const string & inName);
void setBottomLeft(const double x, const double y);
void setDimensions(const double inLength, const double inHeight);
string getName() const;
Point getBottomLeft() const;
double getLength() const;
double getHeight() const;
double area() const;
double perimeter() const;
Point midPoint() const;
void scaleBy2();
void display() const;
};
// FUNCTION PROTOTYPES GO HERE:
void display_banner();
bool read_valid_name(const string & prompt, const string & errMessage,
const string & errDuplicate,
string & name, const vector<Rectangle> & nameList);
void read_point(const string & prompt, double & x, double & y);
void read_dimensions(const string & prompt, double & length, double & height);
void add_rec(const string & name, const double & x, const double & y,
const double & length, const double & height,
vector<Rectangle> & list);
void display_rectangles(vector<Rectangle> & list);
// MAIN FUNCTION GO HERE
int main()
{
display_banner();
string promptA1=" Enter the name of the first rectangle: ";
string promptA2="Enter the name of the next rectangle: ";
string comment;
string promptB="'s bottom left x and y coords: ";
string promptC="'s length and height: ";
string errName="Invalid input. Type 'rec' following by the name or 'stop' if done.";
string errDuplicate="Invalid input. There is already a rectangle by that name. Enter another name";
string name;
bool good;
double x,y,length,height;
vector<Rectangle> R;
do{
good=read_valid_name(promptA1,errName,errDuplicate,name,R);
}while(good!=true);
while(name!="stop"){
read_point("Enter "+name+promptB,x,y);
read_dimensions("Enter "+name+promptC,length,height);
add_rec(name,x,y,length,height,R);
comment=" Thank you! ";
do{
cin.clear();cin.ignore();
good=read_valid_name(comment+promptA2,errName,errDuplicate,name,R);
comment=" Try again! ";
}while(good!=true);
}
display_rectangles(R);
return 0;
}
// FUNCTION DEFINITIONS GO HERE:
void display_banner(){
cout<<"Welcome! Create your own list of rectangles. "
<<"You will be asked to provide information about each rectangle in your list by name."
<<" Type the word 'stop' for the rectangle name when you are done. ";
return;
}
bool read_valid_name(const string & prompt, const string & errMessage,const string & errDuplicate,
string & name, const vector<Rectangle> & nameList)
{
cout<<prompt;
getline(cin,name,' ');
if(name=="stop"){
return true;
}
if(name.substr(0,4)!="rec "){//if it doesnt start wit 'rec ', show error message
cout<<errMessage<<endl;
return false;
}
if(isalpha(name[4])==0){
cout<<errMessage<<endl;
return false;
}
name=name.substr(4,name.length());
/*std::vector<Rectangle>::iterator itr;
for ( itr = nameList.begin(); itr != nameList.end(); ++itr ){//check for duplicates
*/
if(nameList.size()>0){
for(int i=0;nameList[i].getName()!=nameList.back().getName();i++){
//for(int i=0;i<nameList.size();i++){
//if(itr->getName()==name){
if(nameList[i].getName()==name){
cout<<errDuplicate<<endl;
return false;
}
}
}
return true;
}
void read_point(const string & prompt, double & x, double & y){
cout<<prompt;
cin>>x>>y;
return;
}
void read_dimensions(const string & prompt, double & length, double & height){
cout<<prompt;
cin>>length;
cin>>height;
return;
}
void add_rec(const string & name, const double & x, const double & y,
const double & length, const double & height, vector<Rectangle> & list){
Rectangle temp;
temp.setName(name);
temp.setDimensions(length,height);
temp.setBottomLeft(x,y);
list.push_back(temp);
return;
}
void display_rectangles(vector<Rectangle> & list){
cout<<" You have "<<list.size()<<" rectangle(s) in your list: ";
for(int i=0;i<list.size();i++){
//itr->display();
cout<<"Rectangle '"<<list[i].getName()<<"': ";
list[i].display();
cout<<" After scale by 2: ";
list[i].scaleBy2();
list[i].display();
cout<<endl;
}
}
// CLASS MEMBER FUNCTION DEFINITIONS GO HERE:
///Point
void Point::setX(const double x){
px=x;
}
void Point::setY(const double y){
py=y;
}
double Point::getX()const{
return px;
}
double Point::getY()const{
return py;
}
///Rectangle
void Rectangle::setName(const string & inName){
name=inName;
}
void Rectangle::setBottomLeft(const double x, const double y){
blPoint.setX(x);
blPoint.setY(y);
}
void Rectangle::setDimensions(const double inLength, const double inHeight){
if(inLength>0)
length=inLength;
else{
cout<<" Length must be greater than 0. Length set to 1 ";
length=1;
}
if(inHeight>0)
height=inHeight;
else{
cout<<" Height must be greater than 0. Height set to 1 ";
height=1;
}
return;
}
string Rectangle::getName() const{return name;}
Point Rectangle::getBottomLeft() const{return blPoint;}
double Rectangle::getLength() const{return length;}
double Rectangle::getHeight() const{return height;}
double Rectangle::area() const{return length*height;}
double Rectangle::perimeter() const{return 2*(length+height);}
Point Rectangle::midPoint() const{
Point temp;
temp.setX(blPoint.getX()+(length/2.0));
temp.setY(blPoint.getY()+(height/2.0));
return temp;
}
void Rectangle::scaleBy2(){
Point temp;
blPoint.setX(blPoint.getX()-(length/2.0));
blPoint.setY(blPoint.getY()-(height/2.0));
}
void Rectangle::display() const{
Point temp=midPoint();
cout<<"Location is ("<<blPoint.getX()<<", "<<blPoint.getY()<<")"
<<", length is "<<length<<", height is "<<height
<<"; Area is "<<area()<<", perimeter is "<<perimeter()
<<", midpoint is located at ("
<<temp.getX()<<", "<<temp.getY()<<")" <<endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.