Apr 8, 2013

GWC CS G175 C++ Assignment 8

Requirement
Create a class rectangle.  The class should have data members length and width of type float, each of which defaults to 1.  The class should have member functions that calculate the perimeter() and the area() of the rectangle.  It should also have separate set() and get() functions for both the length and width (set_length(), get_length(), set_width(), get_width()).

The set_length() and set_width() functions should require the user to enter length or width data values greater than zero and less than or equal to 20.0.  These functions should examine the data entered by the user, and accept the data only if it is in the specified range (0 < data  <= 20.0).  If the data is not in this range, the user should be reminded of the allowed data range and prompted to enter the data again.  This cycle should repeat until the user enters a correct data value.  This can be accomplished by using a do while loop in each set() function.

Add a draw() function to your rectangle class that draws a solid rectangle of the correct length and width.  Since the length and width are floating point numbers, truncate the length and width values for the purpose of drawing the rectangle (for example, if length = 4.3 and width = 11.8, draw a rectangle that is 4 by 11 characters in size).  Truncation can be achieved by converting the floating point value to an integer value.  Use any character you like to draw the rectangle.  Here is an example of a rectangle drawn with + signs:

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

The functions get() , perimeter(), area() and draw() should be declared using the const keyword to ensure that these functions can not change private member data of the rectangle class.

Write a short demonstration program that uses the rectangle class you have created.  The program should create a rectangle object, then prompt the user to enter new values for the length and width.  Use the set() functions and the user’s entries to change the private data of the rectangle object you created.  Use the functions perimeter() and area() to display their results.  Finally, call the draw() function to display the resulting rectangle.
Disclaimer
  • There are many ways to solve a problem.
  • I do not post codes before the due date, since that does not help you.
  • Codes may be incorrect because I am also a learner like you. Use it at your discretion.
Code
#include <iostream>
#include <string>
using namespace std;
class Rectangle
{
// Declares instance variables
private:
float rectangleWidth;
float rectangleLength;
public:
// Constructs a rectangle.
Rectangle()
{
rectangleWidth = 1;
rectangleLength = 1;
}
// Sets the width of the rectangle.
void setWidth()
{
cout << "Please enter the width of rectangle: ";
float inputWidth;
cin >> inputWidth;
// Check the width input
while (inputWidth <= 0 || inputWidth > 20)
{
cout << "Please enter a value  > 0 and <= 20. Enter the width again: ";
cin >> inputWidth;
}
rectangleWidth = inputWidth;
}
// Gets the width of the rectangle.
float getWidth() const
{
return rectangleWidth;
}
// Sets the length of the rectangle.
void setLength()
{
cout << "Please enter the length of rectangle: ";
float inputLength;
cin >> inputLength;
// Checks the length input
while (inputLength <= 0 || inputLength > 20)
{
cout << "Please enter a value > 0 and <= 20. Enter the height again: ";
cin >> inputLength;
}
rectangleLength = inputLength;
}
// Gets the length of the rectangle.
float getLength() const
{
return rectangleLength;
}
// Calculates the perimeter of the rectangle.
float getPerimeter () const
{
return (rectangleWidth + rectangleLength)*2;
}
// Calculates the area of the rectangle.
float getArea() const
{
return rectangleWidth*rectangleLength;
}
// Draws a rectangle with a character.
void draw() const
{
for (int i = 0; i < (int)(getLength()); i++)
{
for (int j = 0; j < (int)(getWidth()); j++)
{
cout << "+";
}
cout << endl;
}
}
};
int main()
{
//// Instantiates a rectangle
Rectangle firstRectangle;
//// Inputs data
// Sets width firstRectangle.setWidth();
// Sets length
firstRectangle.setLength();
//// Outputs data.
// Prints perimeter
cout << "Perimeter of " << firstRectangle.getWidth() << " x " << firstRectangle.getLength() << " rectangle is: " << firstRectangle.getPerimeter() << endl;
// Prints area
cout << "Area of " << firstRectangle.getWidth() << " x " << firstRectangle.getLength() << " rectangle is: " << firstRectangle.getArea() << endl;
// Draws the rectangle.
cout << endl;
firstRectangle.draw();
cout << endl;
system("PAUSE");
return 0;
}

No comments:

Post a Comment