Write a class called Point that contains two doubles that represent its x- and y
ID: 3759946 • Letter: W
Question
Write a class called Point that contains two doubles that represent its x- and y-coordinates. It should have get and set methods for both fields. It should have a constructor that takes two double parameters and passes those values to the set methods to initialize its fields. It should have a default constructor that initializes both coordinates to 0. It should also contain a method called distanceTo that takes as a parameter a constant reference to another Point and returns the distance from the Point that was passed as a parameter to the Point that we called the method of. You will need to use sqrt(). For example at the end of the following, dist should be equal to 5.0:
Next, write a class called LineSegment that contains two Points that represent its two endpoints. It should have get and set methods for both fields and a constructor that takes two Point parameters and passes them to the set methods to initialize the data members. It should also contain a method called length that returns the length of the LineSegment – by using the distanceTo method on its endpoints – and a method called slope that returns the slope of the LineSegment (if the LineSegment is vertical, go ahead and return the value you get when dividing doubles by zero, which is infinity). The LineSegement class might be used as follows:
Do not include a main method in the files you submit - just the definition of your Point and LineSegment classes. I will be including a main method for testing, and there can only be one main method in a program. You will of course need to have a main method for testing purposes - just make sure you delete it or comment it out before submitting your files.
The functions for the Point class should have the following names:
setXCoord, getXCoord
setYCoord, getYCoord
distanceTo
The functions for the LineSegment class should have the following names:
setEnd1, getEnd1
setEnd2, getEnd2
length
slope
The files must be named: Point.hpp, Point.cpp, LineSegment.hpp andLineSegment.cpp
Explanation / Answer
Classes for Point and LineSegment is below..
class Point
{
private:
double x, y;
public:
Point() : x(0), y(0) {}
Point(int x, int y) : x(x), y(y) {}
double getXCoord(){ return x;}
double getYCoord(){ return Y;}
void setXCoord(double x1){ X = x1;}
void setYCoord(double y1){ Y = y1;}
double distanceTo(const Point& obj)
{
Point z;
double result = 0.0;
z.X= X;
z.Y= Y;
result = sqrt(z.X*obj.X + z.Y*obj.y);
return z;
}
}
class Line
{
private:
Point p1;
Point p2;
double slope;
double length;
public:
Line(const Point & p1, const Point & p2 ) : p1(p1), p2(p2) {}
void setPoints( const Point & ap1, const Point & ap2)
{
p1 = ap1;
p2 = ap2;
}
double length1()
{
length = sqrt(pow((p2.X - p1.X),2) + pow((p2.Y - p1.Y),2));
return length;
}
double slope1()
{
slope = (p2.Y - p1.Y)/(p2.X - p1.X);
return slope;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.