A \"Triangle\" Data Type) Design and implement an ADT that represents a triangle
ID: 3791170 • Letter: A
Question
A "Triangle" Data Type) Design and implement an ADT that represents a triangle, The data for the ADT must include the three side lengths of the triangle. This data should be in the private section of the Triangle class that implements the ADT Include two initialization operations (the class's constructors) Triangle(): provide default values for the ADTs data. Use (3.4, 5 as the default sides. Triangle (const double sideA, const double sideB, const double sideC) uses client-supplied values for the ADTs data. Be sure to check and verify the validity of the triangle data (that is, that the 3 sides really do form a triangle) You must design your triangle object so represents a valid triangle. If the user attempts to problem, a Triangle with invalid sides, your constructor output a error message describing the and then finish constructing must sides to the default the object forcing the In addition to the two constructors, the Triangle ADT also must include the following operations (methods: access methods-return the values of each of the ADT's intermal data members (each the sides) e.g., Triangle h double getSideA() const; a "getter" method Triangle cpp double Triangle getsideA() const return(SideA); bool Right Triangle() const Determine whether the triangle is a right triangle (e.g., c a b or a c br etc.) Note that the triangle side lengths can be specified in any order. Be sure to check all legal variations. bool isEquilateralTriangle const Determine whether the triangle is equilateral (all sides equal) bool is Isosceles Triangle const Determine whether the triangle is isosceles (any two sides equal and different than the third side) double TriangleArea() const Compute and return the area of the triangle (e.g., it's easiest to use Heron's method, look it up in Wikipedia or another source) Notice that, for this lab, there are no methods to modify a triangle's side-length data members once it has been constructed. The only way to establish a triangle with specific side lengths is to construct one. However, it is possible to assign Triangle objects (e.g., which will create an exact copy. TriA-TriB:), You can also create and assign a Triangle object "on the fly" (e.g., TriA- Triangle( 3, 4, 5); For this exercise, you must create a project with 3 source-code files: a class header declaration file (e.g., triangle.h) Be sure that you carefully document the "interface" specifications for your class a class implementation file (e. g, triangle.cpp) a main program for testing out your class implementation. Create your own test program examples and data. Be certain to include enough details to demonstrate that "triangle" objects work properly. Use the example on the following page as a guideline and be sure to test at least the examples Turn in your source code listings and example runs that demonstrate the use of triangle objects, and s that your design and implementation work correctly.Explanation / Answer
//Triangle.h
class Triangle
{
public:
// Constructor method prototypes
Triangle();
Triangle(const double side1, const double side2, const double side3);
// Access method prototypes
double getSideA() const;
double getSideB() const;
double getSideC() const;
bool isRightTriangle() const;
bool isEquilateralTriangle() const;
bool isIsoscelesTriangle() const;
double TriangleArea() const;
private:
// assign the variables for the sides
double side1, side2, side3;
// check for a legal triangle
bool isValid(double side1, double side2, double side3) const
{
return ((side1 < side2 + side3) && (side2 < side1 + side3) &&
(side3 < side1 + side2) || (side1 == 0) || (side2 == 0) || (side3 == 0));
}
};
//triangle.cpp
//main program
#include <iostream>
#include <string>
using namespace std;
#include "triangle.h"
//
void printTriangleDetails(string Name, const Triangle &tri)
{
cout << Name << " triangle: [";
cout << tri.getSideA() << ", "
<< tri.getSideB() << ", "
<< tri.getSideC() << "]" << endl;
if (tri.isRightTriangle())
cout << " Triangle is a right triangle" << endl;
else
cout << " Triangle is not a right triangle" << endl;
// etc, etc, etc, for the other details about a triangle object
cout << endl; // end of details for this triangle
}
int main()
{
Triangle t1;
printTriangleDetails("Default", t1);
Triangle t2(5, 4, 3);
printTriangleDetails("t2(6,5,4)", t2);
Triangle t3(4, 5, 3);
printTriangleDetails("t3(5,6,4)", t3);
Triangle t4(4, 6, 11);
printTriangleDetails("t4(3,0,3)", t4);
Triangle t5;
t5 = t4;
printTriangleDetails("Assigned t5=t4", t5);
t5 = Triangle(3, 3, 3);
printTriangleDetails("On-The-Fly assigned Triangle(3,3,3)", t5);
if (t5.isIsoscelesTriangle())
{
cout << "Triangle is an Isosceles Triangle;" << endl;
}
return 0;
}
#include "triangle.h" #include <iostream> using namespace std; Triangle::Triangle() : side1(3), side2(4), side3(5) { // side1 = 3; // side2 = 4; // side3 = 5; } Triangle::Triangle(const double side1, const double side2, const double side3) { // check for a legal triangle if (!isValid(side1,side2,side3)) { cout << "Invalid Triangle, setting defaults!" << endl; Triangle::side1 = 3; Triangle::side2 = 4; Triangle::side3 = 5; } // if not legal, assign the default values else { Triangle::side1 = side1; Triangle::side2 = side2; Triangle::side3 = side3; } } double Triangle::getSideA() const { return side1; } double Triangle::getSideB() const { return side2; } double Triangle::getSideC() const { return side3; } // Check if the triangle is an Isosceles Traingle, ie, any two sides equal and different than the third. bool Triangle::isEquilateralTriangle() const { if (side1 == side2 && side2 == side3 && side1 == side3) { return true; } return false; } // Check if the triangle is Equilateral,ie, all sides are equal. bool Triangle::isIsoscelesTriangle() const { if (side1 == side2 || side2 == side3 || side1 == side3) { return true; } return false; } // Check if the triangle is a right triangle, ie, a triangle with a right angle. bool Triangle::isRightTriangle() const { if (pow(side3,2) == pow(side1,2) + pow(side2,2) || pow(side1,2) == pow(side3,2) + pow(side2,2) || pow(side2,2) == pow(side3,2) + pow(side1,2)) return true; else return false; } // get the area of the triangle double Triangle::TriangleArea() const { float Area, s; s = (side1 + side2 + side3) / 2; Area = sqrt(s*(s - side1) * (s - side2) * (s - side3)); return Area; } Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.