Write a class called Point that contains two doubles that represent its x- and y
ID: 3759702 • 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 and LineSegment.cpp
Point.cpp and LineSegment.hpp should both #include Point.hpp. LineSegment.cpp should #include LineSegment.hpp. The main method you write for testing will also need to include LineSegment.hpp. If you named the file with your main method "geomMain.cpp", then you can compile your program with "g++ Point.cpp LineSegment.cpp geomMain.cpp -o geom".
Explanation / Answer
geomain.cpp:
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <fstream>
#include "Point.hpp"
#include "LineSegment.hpp"
using namespace std;
void print(Point p, string name)
{
cout << name << " = (" << p.getXCoord() << "," << p.getYCoord() << ")";
}
int main()
{
string input;
cout << " Welcome to Geometry class test driver" << endl << endl;
Point p1;
cout << "Default constructor:" << endl;
print(p1,"p1"); cout << endl << endl;
p1 = Point(1.23,4.56);
cout << "X-Y constructor:" << endl;
print(p1,"p1"); cout << endl << endl;
p1.setXCoord(-3.21);
cout << "X setter:" << endl;
print(p1,"p1"); cout << endl << endl;
p1.setYCoord(-6.54);
cout << "Y setter:" << endl;
print(p1,"p1"); cout << endl << endl;
// easy test for distance using 3-4-5 triangle at 10,10
p1.setXCoord(10);
p1.setYCoord(10);
Point p2(13,14);
cout << "Distance (3-4-5):" << endl;
print(p1,"p1");
cout << " , ";
print(p2,"p2"); cout << endl;
cout << "Distance = " << p1.distanceTo(p2) << endl << endl;
// another test for distance
p1 = Point(-1.5, 0.0);
p2 = Point(1.5, 4.0);
double dist = p1.distanceTo(p2);
cout << "Distance (3-4-5):" << endl;
print(p1,"p1");
cout << " , ";
print(p2,"p2"); cout << endl;
cout << "Distance = " << dist << endl << endl;
p1 = Point();
p2 = Point(1,1);
LineSegment s1 = LineSegment(p1, p2);
cout << "LineSegment(0,0 -> 1,1):" << endl;
cout << "length = " << s1.length() << endl;
cout << "slope = " << s1.slope() << endl << endl;
p1 = Point();
p2 = Point(3,4);
s1 = LineSegment(p1, p2);
cout << "LineSegment(0,0 -> 3,4):" << endl;
cout << "length = " << s1.length() << endl;
cout << "slope = " << s1.slope() << endl << endl;
p1 = Point();
p2 = Point(4,3);
s1 = LineSegment(p1, p2);
cout << "LineSegment(0,0 -> 4,3):" << endl;
cout << "length = " << s1.length() << endl;
cout << "slope = " << s1.slope() << endl << endl;
p1 = Point();
p2 = Point(0,3);
s1 = LineSegment(p1, p2);
cout << "LineSegment(0,0 -> 0,3):" << endl;
cout << "length = " << s1.length() << endl;
cout << "slope = " << s1.slope() << endl << endl;
p1 = Point();
p2 = Point(0,-3);
s1 = LineSegment(p1, p2);
cout << "LineSegment(0,0 -> 0,-3):" << endl;
cout << "length = " << s1.length() << endl;
cout << "slope = " << s1.slope() << endl << endl;
// system("pause");
}
LineSegement.cpp:
#include <math.h>
#include "LineSegment.hpp"
LineSegment::LineSegment(Point p1, Point p2)
{
setP1(p1);
setP2(p2);
}
LineSegment::~LineSegment(void)
{
}
Point LineSegment::getP1() const
{
return m_p1;
}
void LineSegment::setP1(const Point p)
{
m_p1 = p;
}
Point LineSegment::getP2() const
{
return m_p2;
}
void LineSegment::setP2(const Point p)
{
m_p2 = p;
}
double LineSegment::length() const
{
return m_p1.distanceTo(m_p2);
}
double LineSegment::slope() const
{
double rise = m_p2.getYCoord() - m_p1.getYCoord();
double run = m_p2.getXCoord() - m_p1.getXCoord();
return rise/run;
}
Point.cpp:
#include <math.h>
#include "Point.hpp"
Point::Point(void)
{
setXCoord(0);
setYCoord(0);
}
Point::Point(double x, double y)
{
setXCoord(x);
setYCoord(y);
}
Point::~Point(void)
{
}
double Point::getXCoord() const
{
return m_xCoord;
}
void Point::setXCoord(double x)
{
m_xCoord = x;
}
double Point::getYCoord() const
{
return m_yCoord;
}
void Point::setYCoord(double y)
{
m_yCoord = y;
}
double Point::distanceTo(const Point &otherPoint) const
{
return sqrt((getXCoord() - otherPoint.getXCoord()) * (getXCoord() - otherPoint.getXCoord())
+ (getYCoord() - otherPoint.getYCoord()) * (getYCoord() - otherPoint.getYCoord()));
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.