My teacher commented that \" I noticed that you modified the PointXY class to ma
ID: 3918663 • Letter: M
Question
My teacher commented that
" I noticed that you modified the PointXY class to make x and y public. You are not allowed to do that. You have to use the accessor method."
How do I fix it?
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
class PointXY
{
public:
int x,y;
};
double distance(const PointXY &u,const PointXY &v)
{
return sqrt((u.x - v.x) * (u.x - v.x) + (u.y - v.y) * (u.y - v.y));
}
void area_perimeter(vector<PointXY> &v,double &area, double &perimeter)
{
double a = distance(v[0], v[1]);
double b = distance(v[1], v[2]);
double c = distance(v[2], v[0]);
perimeter = a+b+c;
double s = perimeter/2.0;
area = sqrt(s * (s-a) * (s-b) *( s-c));
}
int main()
{
int n = 3;
vector<PointXY> v(n);
v[0].x = 3.5;
v[0].y = 0.4;
v[1].x = 7.2;
v[1].y = 0.6;
v[2].x = 4.1;
v[2].y = 5.6;
double area, perimeter;
area_perimeter(v, area, perimeter);
cout << "area " << area << endl;
cout << "perimeter " << perimeter << endl;
return 0;
}
Explanation / Answer
#include #include #include using namespace std; class PointXY { private: int x, y; public: int getX() const { return x; } int getY() const { return y; } void setX(int x) { this->x = x; } void setY(int y) { this->y = y; } }; double distance(const PointXY& u, const PointXY& v) { return sqrt((u.getX() - v.getX()) * (u.getX() - v.getX()) + (u.getY() - v.getY()) * (u.getY() - v.getY())); } void area_perimeter(vector& v, double& area, double& perimeter) { double a = distance(v[0], v[1]); double b = distance(v[1], v[2]); double c = distance(v[2], v[0]); perimeter = a + b + c; double s = perimeter / 2.0; area = sqrt(s * (s - a) * (s - b) * (s - c)); } int main() { int n = 3; vector v(n); v[0].setX(3.5); v[0].setY(0.4); v[1].setX(7.2); v[1].setY(0.6); v[2].setX(4.1); v[2].setY(5.6); double area, perimeter; area_perimeter(v, area, perimeter); coutRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.