Question

In: Computer Science

An equilateral triangle is a triangle whose sides are equal. You are to write a class...

An equilateral triangle is a triangle whose sides are equal. You are to write a class called Triangle, using filenames triangle.h and triangle.cpp, that will allow the creation and handling of equilateral triangles, whose sides are integers in the range 1-39.

Details:

1. The single constructor for the Triangle class should have 3 parameters: an integer size (required), which is the length of a side; a border character (optional, with a default of '#'); and a fill character (optional, with a default of '*'). If the size provided is less than 1, set the size to 1. If the size provided is greater than 39, set the size to 39. The class will need to provide internal storage for any member data that must be kept track of.
2. There should be member functions GetSize, Perimeter, and Area, which will return the size of a side, the perimeter of the triangle, and the area of the triangle, respectively. The first 2 should return integer results. The Area function should return its result as a double.
3. There should be member functions Grow and Shrink, which will increase or decrease (respectively) the size of the triangle's sides by 1, unless this would cause the size to go out of bounds (out of the 1-39 range); in the latter case, Grow and Shrink should make no change to the size

4. There should be member functions SetBorder and SetFill, which each allow a new border or fill character (respectively) to be passed in as a parameter. There is a chart of ASCII characters in an appendix of the textbook. The characters that should be allowed for the border or fill characters are any characters from the '!' (ascii 33) up through the '~' (ascii 126). If an attempt is made to set the border or fill characters to anything outisde the allowable range, the function should set the border or fill back to its original default (the ones listed for the constructor -- the border default is '#' and the fill default is '*').

5. There should be a member function called Draw that will display a picture of the triangle on the screen. You may assume that the cursor is already at the beginning of a line when the function begins, and you should make sure that you leave the cursor on the line following the picture afterwards (i.e. print a newline after the last line of the triangle). Use the border character to draw the border of the triangle, and use the fill character to draw the internal characters. Separate the characters on a line in the picture by a single space to make the triangle look more proportional (to approximate the look of an equilateral triangle). You may not use formatting functions like setw to draw the triangle. This must be handled with loops. (You will only print out the newline, spaces, the border character, and maybe the fill character on any given line).

6. Provide a member function called Summary that displays all information about a triangle: its size, perimeter, area, and a picture of what it looks like. When displaying the area (decimal data), always show exactly 2 decimal places. Your output should be in the exact same format as mine (seen in the linked sample run below)

t1 has size = 1 units.
# 

t2 has size = 7 units.
      ^ 
     ^ ^
    ^ * ^
   ^ * * ^
  ^ * * * ^
 ^ * * * * ^
^ ^ ^ ^ ^ ^ ^

t3 has size = 12 units.
           X 
          X X
         X O X
        X O O X
       X O O O X
      X O O O O X
     X O O O O O X
    X O O O O O O X
   X O O O O O O O X
  X O O O O O O O O X
 X O O O O O O O O O X
X X X X X X X X X X X X

t4 has size = 39 units.
                                      $ 
                                     $ $
                                    $ o $
                                   $ o o $
                                  $ o o o $
                                 $ o o o o $
                                $ o o o o o $
                               $ o o o o o o $
                              $ o o o o o o o $
                             $ o o o o o o o o $
                            $ o o o o o o o o o $
                           $ o o o o o o o o o o $
                          $ o o o o o o o o o o o $
                         $ o o o o o o o o o o o o $
                        $ o o o o o o o o o o o o o $
                       $ o o o o o o o o o o o o o o $
                      $ o o o o o o o o o o o o o o o $
                     $ o o o o o o o o o o o o o o o o $
                    $ o o o o o o o o o o o o o o o o o $
                   $ o o o o o o o o o o o o o o o o o o $
                  $ o o o o o o o o o o o o o o o o o o o $
                 $ o o o o o o o o o o o o o o o o o o o o $
                $ o o o o o o o o o o o o o o o o o o o o o $
               $ o o o o o o o o o o o o o o o o o o o o o o $
              $ o o o o o o o o o o o o o o o o o o o o o o o $
             $ o o o o o o o o o o o o o o o o o o o o o o o o $
            $ o o o o o o o o o o o o o o o o o o o o o o o o o $
           $ o o o o o o o o o o o o o o o o o o o o o o o o o o $
          $ o o o o o o o o o o o o o o o o o o o o o o o o o o o $
         $ o o o o o o o o o o o o o o o o o o o o o o o o o o o o $
        $ o o o o o o o o o o o o o o o o o o o o o o o o o o o o o $
       $ o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o $
      $ o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o $
     $ o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o $
    $ o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o $
   $ o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o $
  $ o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o $
 $ o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o $
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $

t1 now has size = 1 units.
t2 now has size = 6 units.
t3 now has size = 13 units.
t4 now has size = 39 units.
t2 has perimeter = 18 units.
t3 has perimeter = 39 units.
t2 has area = 15.5885 square units.

t3 has area = 73.1791 square units.

# 
t1 grows:
 # 
# #
... and grows:
  # 
 # #
# # #

t1 now has size = 6 units.
     ^ 
    ^ ^
   ^ * ^
  ^ * * ^
 ^ * * * ^
^ ^ ^ ^ ^ ^
t2 now looks like:
     @ 
    @ @
   @ - @
  @ - - @
 @ - - - @
@ @ @ @ @ @

t2 now looks like:
     # 
    # #
   # * #
  # * * #
 # * * * #
# # # # # #


Here is a summary on t3:
Size of triangle's side = 13 units.
Perimeter of triangle = 39 units.
Area of triangle = 73.18 units.
Triangle looks like:
            X 
           X X
          X O X
         X O O X
        X O O O X
       X O O O O X
      X O O O O O X
     X O O O O O O X
    X O O O O O O O X
   X O O O O O O O O X
  X O O O O O O O O O X
 X O O O O O O O O O O X
X X X X X X X X X X X X X

7. I am providing a sample driver program (called driver.cpp) that uses objects of type Triangle and illustrates sample usage of the member functions.

#include <iostream>
#include <iomanip>
#include "triangle.h"

using namespace std;

int main() 
{
  // create some Triangles
  Triangle t1( -5 ), t2( 7, '^' ), t3( 12, 'X', 'O' ), t4( 50 , '$' , 'o');
  // display original Triangles

  cout << "t1 has size = " << t1.GetSize() << " units.\n";
  t1.Draw();
  cout << "\nt2 has size = " << t2.GetSize() << " units.\n";
  t2.Draw();
  cout << "\nt3 has size = " << t3.GetSize() << " units.\n";
  t3.Draw();
  cout << "\nt4 has size = " << t4.GetSize() << " units.\n";
  t4.Draw();
  cout << '\n';

  t1.Shrink(); // demonstrate shrink
  t2.Shrink();
  t3.Grow(); // and grow
  t4.Grow();
  cout << "t1 now has size = " << t1.GetSize() << " units.\n";
  cout << "t2 now has size = " << t2.GetSize() << " units.\n";
  cout << "t3 now has size = " << t3.GetSize() << " units.\n";
  cout << "t4 now has size = " << t4.GetSize() << " units.\n";

  // demonstrate perimeter
  cout << "t2 has perimeter = " << t2.Perimeter() << " units.\n"; 
  cout << "t3 has perimeter = " << t3.Perimeter() << " units.\n"; 
  // and area
  cout << "t2 has area = " << t2.Area() << " square units.\n\n"; 
  cout << "t3 has area = " << t3.Area() << " square units.\n\n"; 

  t1.Draw();
  t1.Grow();               // show that fill character
  cout << "t1 grows:\n";   // appears only when size
  t1.Draw();               // is at least 3
  t1.Grow();
  cout << "... and grows:\n";
  t1.Draw();
  cout << '\n';

  t1 = t2; // demonstrate the default overload of the
  // assignment operator
  cout << "t1 now has size = " << t1.GetSize() << " units.\n";
  t1.Draw(); 

  // demonstrate the changing of border and fill characters
  t2.SetBorder('@');
  t2.SetFill('-');
  cout << "t2 now looks like:\n";
  t2.Draw();
  cout << '\n';
  t2.SetBorder('\n');    // illegal border
  t2.SetFill('\a');      // illegal fill
  cout << "t2 now looks like:\n";
  t2.Draw();
  cout << '\n';

  cout << "\nHere is a summary on t3:\n"; // demonstrate summary
  t3.Summary();

  return 0;
}

Your class declaration and definition files must work with my main program, as-is (do not change my program to make your code work!). You are encouraged to write your own driver routines to further test the functionality of your class, as well. Most questions about the required behavior of the class can be determined by carefully examining my driver program and the sample execution. Keep in mind, this is just a sample. Your class must meet the requirements listed above in the specification -- not just satisfy this driver program. (For instance, I haven't tested every illegal fill character in this driver program -- I've just shown a sample). Your class will be tested with a larger set of calls than this driver program represents.

Solutions

Expert Solution


Related Solutions

If equilateral triangles are constructed on the sides of any triangle, prove that the distances between...
If equilateral triangles are constructed on the sides of any triangle, prove that the distances between the vertices of the original triangle and the opposite vertices of the equilateral triangles are equal.
Consider an equilateral triangle with sides measuring 1.0 m in length. At each point of the...
Consider an equilateral triangle with sides measuring 1.0 m in length. At each point of the triangle is a +2.0 ?C charge. Calculate the Coulomb force on each charge. Recall that forces are vectors and thus your answer will require a magnitude and direction for each of the three forces.
Find the volume V of a regular tetrahedron whose face is an equilateral triangle of side...
Find the volume V of a regular tetrahedron whose face is an equilateral triangle of side 8. Find the area of the horizontal cross-section A at the level z=4.
Moment of inertia for equilateral triangle
Three rods each of mass m and length I are joined together to form an equilateral triangle as shown in figure. Find the moment of inertia of the system about an axis passing through its centre of mass and perpendicular to the plane of triangle.
Write a program that accepts the lengths of three sides of a triangle as inputs. The...
Write a program that accepts the lengths of three sides of a triangle as inputs. The program output should indicate whether or not the triangle is a right triangle. Recall from the Pythagorean theorem that in a right triangle, the square of one side equals the sum of the squares of the other two sides. Use The triangle is a right triangle. and The triangle is not a right triangle. as your final outputs. An example of the program input...
3 charges, 8 µC each, are located on three vertices A, B, C of an equilateral triangle with sides 1 cm each.
  9). 3 charges, 8 µC each, are located on three vertices A, B, C of an equilateral triangle with sides 1 cm each. Another charge q is located at the mid point D of the side BC. Calculate q in micro Coulomb so that net force on the charge at A due to the charges at B, C and D is zero. 10). In a right angle triangle ABC, angle ABC is 90 Degree, AB = 2 m, and...
Java Write a class called Triangle that can be used to represent a triangle. Write a...
Java Write a class called Triangle that can be used to represent a triangle. Write a class called Describe that will interface with the Triangle class The Server • A Triangle will have 3 sides. It will be able to keep track of the number of Triangle objects created. It will also hold the total of the perimeters of all the Triangle objects created. • It will allow a client to create a Triangle, passing in integer values for the...
(PYTHON) Write aprogram that prompts user to enter three sides of a triangle....
(PYTHON) Write a program that prompts user to enter three sides of a triangle. The program should determine if the three sides can form a triangle. If the three sides can form a triangle, then determine the type of the triangle.There are three types of triangles: Equilateral triangle (all 3 sides are equal) Isosceles triangle (two sides are equal, the third side is of a different length) Scalene triangle (all 3 sides are of different lengths)   The program should...
Find the height of equilateral triangle if side is = 10in
Find the height of equilateral triangle if side is = 10in
1. Treacherous Triangle Trickery. Consider a charge distribution consisting of an equilateral triangle with a point...
1. Treacherous Triangle Trickery. Consider a charge distribution consisting of an equilateral triangle with a point charge q fixed at each of its vertices. Let d be the distance between the center of the triangle and each vertex, let the triangle’s center be at the origin, and let one of its vertices lie on the x-axis at the point x = −d. 1.1. Compute the electric field at the center of the triangle by explicitly computing the sum of the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT