Did my answer fullfill what the question want?
Question:
Design a class named Triangle to represent a triangle.The class contain:
- Three sets of points X and Y represented by two double values each,containing the three points that define a triangle.
- A no-arg constructor that creates a default triangle with the points {1.0,1.0},{1.0,1.0},{1.0,1.0}
- A constructor that creates a triangle from the specified three points
- The accessor and mutator functions for all the data fields
- A function named getArea() that returns the area of the triangle
- A function named getPerimeter() that returns the length of the perimeter
Draw the UML diagram for the class.Implement the class.Write the test program that creates two Triangle objects.
Assign points{1,1},{5,2.5},{1,5} to the first object.
Assign points{-4.0,-3.0},{-1.5,-1.0},{1.2,-3.0} to the second object.
Display the properties of both objects.In addition,find and display their area and perimeter.
UML diagram:
_______________
Triangle
_______________
-x1:double
-y1:double
-x2:double
-y2:double
-x3:double
-y3:double
_______________
+Triangle()
+Triangle(newX1:double,newY1:double,newX2:double,newY2:double,newX3:double,newY3:double)
+getX1():double
+getY1():double
+getX2():double
+getY2():double
+getX3():double
+getY3():double
+getPerimeter():double
+getArea():double
#include<iostream>
#include<cmath>
#include<iomanip>
#include<cstdlib>
using namespace std;
class Triangle
{
public:
Triangle()
{ x1=1.0;
y1=1.0;
x2=1.0;
y2=1.0;
x3=1.0;
y3=1.0; }
Triangle(double newX1,double newY1,double newX2,double newY2,double newX3,double newY3)
{ x1=newX1;
y1=newY1;
x2=newX2;
y2=newY2;
x3=newX3;
y3=newY3; }
double getX1()
{ return x1; }
double getY1()
{ return y1; }
double getX2()
{ return x2; }
double getY2()
{ return y2; }
double getX3()
{ return x3; }
double getY3()
{ return y3; }
double getPerimeter()
{
double distance1=sqrt(pow(x2-x1,2)+pow(y2-y1,2));
double distance2=sqrt(pow(x3-x2,2)+pow(y3-y2,2));
double distance3=sqrt(pow(x3-x1,2)+pow(y3-y1,2));
return distance1+distance2+distance3;
}
double getArea()
{
return abs((x1*(y2-y3)+x2*(y1-y3)+x3*(y1+y2))/2);
}
private:
double x1,y1,x2,y2,x3,y3;
};
int main()
{
Triangle triangle1(1,1,5,2.5,1,5);
Triangle triangle2(-4.0,-3.0,-1.5,-1.0,1.2,-3.0);
cout<<fixed<<setprecision(1);
cout<<"Triangle 1 has point("<<triangle1.getX1()<<","<<triangle1.getY1()<<") , ("<<triangle1.getX2()
<<","<<triangle1.getY2()<<") and ("<<triangle1.getX3()<<","<<triangle1.getY3()<<")"<<endl;
cout<<"The area is "<<triangle1.getArea()<<" and perimeter is "<<triangle1.getPerimeter()<<endl<<endl;
cout<<"Triangle 2 has point("<<triangle2.getX1()<<","<<triangle2.getY1()<<") , ("<<triangle2.getX2()
<<","<<triangle2.getY2()<<") and ("<<triangle2.getX3()<<","<<triangle2.getY3()<<")"<<endl;
cout<<"The area is "<<triangle2.getArea()<<" and perimeter is "<<triangle2.getPerimeter()<<endl;
system("PAUSE");
return 0;
}

New Topic/Question
Reply



MultiQuote




|