Week 13 : Extra Programming Exercise
(Answer)
Square::Square()
{
length = 0;
}
void Square::set(int
side, char border, char fill)
{
length = side;
borderChar = border;
fillChar = fill;
}
void Square::draw() const
{
cout << endl;
cout << "The area of the square is " << area() << endl;
cout << "The perimeter of the square is " << perimeter() <<
endl;
cout << endl;
for(int i=0; i<length; i++)
{
for(int j=0; j<length; j++)
{
if ((i==0)||(i==length-1)||(j==0)||(j==length-1))
cout << borderChar;
else
cout << fillChar;
}
cout <<endl;
}
}
int Square::area() const
{
return (length * length);
}
int Square::perimeter()
const
{
return (length * 4);
}
int main()
{
Square s;
int length;
char ans;
cout << "Enter the length for the square: ";
cin >> length;
cout << "Do you want to set the border character and fill character?
";
cin >> ans;
if ((ans == 'Y') || (ans == 'y'))
s.set(length, 'O', 'o');
else
s.set(length, 'X', ' ');
s.draw();
return 0;
}
Circle::Circle()
{
diameter = 0;
}
void Circle::set(int
side, char border, char fill)
{
diameter = side;
borderChar = border;
fillChar = fill;
}
void Circle::draw() const
{
cout << endl;
cout << "The area of the circle is " << area() << endl;
cout << "The perimeter of the circle is " << perimeter() <<
endl;
cout << endl;
for(int i=0; i<diameter; i++)
{
for(int j=0; j<diameter; j++)
{
if (((i==0)&&(j==0))||((i==0)&&(j==diameter-1)) ||((i==diameter-1)&&(j==0))
||((i==diameter-1)&&(j==diameter-1)))
cout << ' ';
else if ((i==0)||(i==diameter-1)||(j==0)||(j==diameter-1))
cout << borderChar;
else
cout << fillChar;
}
cout <<endl;
}
}
double Circle::area()
const
{
return ((diameter/2.) * (diameter/2.) * 3.1416);
}
double Circle::perimeter()
const
{
return (diameter * 3.1416);
}
int main()
{
Circle c;
int diameter;
char ans;
cout << "Enter the diameter of the Circle: ";
cin >> diameter;
cout << "Do you want to set the border character and fill character?
";
cin >> ans;
if ((ans == 'Y') || (ans == 'y'))
c.set(diameter, 'O', 'o');
else
c.set(diameter, 'X', ' ');
c.draw();
return 0;
}
Rectangle::Rectangle()
{
height = 0;
width = 0;
}
void Rectangle::set(int
side_x, int side_y, char border, char fill)
{
width = side_x;
height = side_y;
borderChar = border;
fillChar = fill;
}
void Rectangle::draw()
const
{
cout << endl;
cout << "The area of the rectangle is " << area() <<
endl;
cout << "The perimeter of the rectangle is " << perimeter()
<< endl;
cout << endl;
for(int i=0; i<height; i++)
{
for(int j=0; j<width; j++)
{
if ((i==0)||(i==height-1)||(j==0)||(j==width-1))
cout << borderChar;
else
cout << fillChar;
}
cout <<endl;
}
}
int Rectangle::area()
const
{
return (width * height);
}
int Rectangle::perimeter()
const
{
return ((width + height) * 2);
}
int main()
{
Rectangle r;
int height;
int width;
char ans;
cout << "Enter the height and width of the rectangle: ";
cin >> height >> width;
cout << "Do you want to set the border character and fill character?
";
cin >> ans;
if ((ans == 'Y') || (ans == 'y'))
r.set(width, height, 'O', 'o');
else
r.set(width,height, 'X', ' ');
r.draw();
return 0;
}
#include <iostream>
using namespace std;
class RightTriangle{
public:
RightTriangle();
// sets side to length = 0
void set(int side, char border = 'X', char fill = ' ');
void draw() const;
double area() const;
// return the area of the Right Triangle
double perimeter() const; // return
the perimeter of the Right Triangle
private:
int length;
// length of side
char borderChar;
// the border character
char fillChar;
// the fill character
};
RightTriangle::RightTriangle()
{
length = 0;
}
void RightTriangle::set(int
side, char border, char fill)
{
length = side;
borderChar = border;
fillChar = fill;
}
void RightTriangle::draw()
const
{
cout << endl;
cout << "The area of the Triangle is " << area() << endl;
cout << "The perimeter of the Triangle is " << perimeter()
<< endl;
cout << endl;
for(int i=0; i<length; i++)
{
for(int j=0; j<length; j++)
{
if ((i==length-1)||(j==0)||(i==j))
cout << borderChar;
else if(j < i)
cout << fillChar;
else
cout << ' ';
}
cout <<endl;
}
}
double RightTriangle::area()
const
{
return (length*length) / 2.0;
}
double RightTriangle::perimeter()
const
{
return length + length + 1.414213*length;
}
int main()
{
RightTriangle t;
int length;
char ans;
cout << "Enter the side length of the Right Triangle: ";
cin >> length;
cout << "Do you want to set the border character and fill character?
";
cin >> ans;
if ((ans == 'Y') || (ans == 'y'))
t.set(length, 'O', 'o');
else
t.set(length, 'X', ' ');
t.draw();
return 0;
}
char model; char ans;
int length; int width;
cout << "Enter which model you want to modify: \n";
cout << "s for Square, c for Circle, r for Rectangle & t for
RightTriangle: ";
cin >> model;
switch(model){
case 's':
cout << "How long is the side? ";
cin >> length;
cout << "Do you want to set the border character and fill character?
";
cin >> ans;
if ((ans == 'Y') || (ans == 'y'))
s.set(length, 'O', 'o');
else
s.set(length, 'X', ' ');
s.draw();
break;
case 'c':
cout << "What's the diameter of the circle: ";
cin >> length;
cout << "Do you want to set the border character and fill character?
";
cin >> ans;
if ((ans == 'Y') || (ans == 'y'))
c.set(length, 'O', 'o');
else
c.set(length, 'X', ' ');
c.draw();
break;
case 'r':
cout << "Enter the height and width of the rectangle: ";
cin >> length >> width ;
cout << "Do you want to set the border character and fill character?
";
cin >> ans;
if ((ans == 'Y') || (ans == 'y'))
r.set(width, length, 'O', 'o');
else
r.set(width, length, 'X', ' ');
r.draw();
break;
case 't':
cout << "Enter the side length of the Right Triangle: ";
cin >> length;
cout << "Do you want to set the border character and fill character?
";
cin >> ans;
if ((ans == 'Y') || (ans == 'y'))
t.set(length, 'O', 'o');
else
t.set(length, 'X', ' ');
t.draw();
break;
default:
cout << "Wrong Input. No such Model!\n";
break;
}
return 0;
}