Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a program that will create a list of axis-aligned rectangles defined in a 2D space, i.e. the x-y space. An axis-aligned rectangle has two

Write a program that will create a list of axis-aligned rectangles defined 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 defintion). Each rectangle is defined by the (x, y) location of its bottom left corner, its length (along the x-axis), and its height (along

1

the 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 rectangles 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.

Display welcome banner

WHILE user input is invalid /* Prompt and read first rectangles name*/

Display Try again!

IF user input is not stop

Extract rectangle name from user input

Prompt for bottom left point

Prompt for length and height

Add new rectangle to the rectangle list

WHILE user input is not stop

Display Thank you!

WHILE user input is invalid /* Prompt and read next rectangles name */

Display Try again!

IF user input is not stop

Extract rectangle name from user input

Prompt for bottom left point

Prompt for length and height

Add new rectangle to the rectangle list

IF the rectangle list is not empty

Display all rectangles in the rectangle list

ELSE

Display that no rectangles are in the list 2

In the code template you will find two class definitions. 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 defined 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.

All functions should be written AFTER the main procedure.

All function prototypes should be written for each function and placed BEFORE the

main procedure.

Each function should have a comment explaining what it does.

Each function parameter should have a comment explaining the parameter.

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 specifications outlined here to solve your homework. Though, you should carefully decide whether input parameters are passed by value or by reference.

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.

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

3

whatever 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).

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

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

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

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

Implement the set and get member functions of the class Rectangle (see examples found in the lecture notes and textbook reading).

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.

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.

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.

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

4

of 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 objects class attributes to reflect the scaled up rectangle.

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.

Be sure to modify the header comments File, Created by, Creation Date, and Synopsis at the top of the file. Each synopsis should contain a brief description of what the program does.

Be sure that there is a comment documenting each variable.

Be sure that your if statements, for and while loops and blocks are properly indented.

What I have so far

/*

File: rectangles.cpp

Created by: ??

Creation Date: ??

Synopsis: ??

*/

#include

#include

#include

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:

int main()

{

// Define your local variables, e.g. a vector of class Rectangle

// Display welcome banner

/* Prompt user for first rectangle or 'stop' */

// WHILE user input is invalid

// Display "Try again! "

// IF user input is not 'stop'

// Extract rectangle name from user input

// Prompt for bottom left point

// Prompt for length and height

// Add rectangle to the rectangle list

/* Prompt user for next rectangle or 'stop' */

// WHILE user input is not 'stop'

// Display "Thank you! "

// WHILE user input is invalid

// Display "Try again! "

// IF user input is not 'stop'

// Extract rectangle name from user

input

// Prompt for bottom left point

// Prompt for length and height

// Add rectangle to the rectangle list

// IF the rectangle list is not empty

// Display all rectangles in the rectangle list

// ELSE

// Display that no rectangles are in the list

return 0;

}

// FUNCTION DEFINITIONS GO HERE:

// CLASS MEMBER FUNCTION DEFINITINOS GO HERE:

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);

}

void Rectangle::setName(const string & inName)

{

// replace with your code

}

void Rectangle::setBottomLeft(const double x, const double y)

{

// replace with your code

}

void Rectangle::setDimensions(const double inLength, const double

inHeight)

{

// replace with your code

}

string Rectangle::getName() const

{

// replace with your code

}

Point Rectangle::getBottomLeft() const

{

// replace with your code

}

double Rectangle::getLength() const

{

// replace with your code

}

double Rectangle::getHeight() const

{

// replace with your code

}

double Rectangle::area() const

{

// replace with your code

}

double Rectangle::perimeter() const

{

// replace with your code

}

Point Rectangle::midPoint() const

{

// replace with your code

}

void Rectangle::scaleBy2()

{

// replace with your code

}

void Rectangle::display() const

{

// replace with your code

}

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

Step: 3

blur-text-image

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

Oracle 10g Database Administrator Implementation And Administration

Authors: Gavin Powell, Carol McCullough Dieter

2nd Edition

1418836656, 9781418836658

More Books

Students also viewed these Databases questions