Write a class called Point that contains two doubles that re
Write a class called Point that contains two doubles that represent its x- and y-coordinates. It should have get methods for both data members. It should have a constructor that takes two double parameters and uses them to initialize its data members. It should have a default constructor that initializes both coordinates to 0. It should also contain a method called distanceTo that takes a Point object as a parameter and returns the distance from that Point to the Point that we invoked the method on. 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 Line that contains two Points that the Line passes through. It should have get methods for both data members and a constructor that takes two Point parameters and uses them to initialize its data members. If the two Points have the same coordinates, this constructor should throw a DegenerateLineException. The line class should contain a method called slope that returns the slope of the Line. If the slope is undefined, this method should throw an UndefinedSlopeException. It should also contain a method called intersectWith that takes a Line object as a parameter and returns a Point object representing the coordinates at which the two lines intersect. If the two lines are parallel (or coincident), the intersectWith method should throw a ParallelLinesException. Remember that using \"==\" to test equality of floating-point types is error prone - instead you should test whether their values are very close. For this program, define \"very close\" as within 0.00001 (that\'s 4 zeroes) - define it as a constant. As just one example, the Line class might be used as follows:
Of course the above should use multiple try/catch blocks. In your main method you should write code that creates two lines and then prints out the slope of each line and the coordinates of the Point at which the two lines intersect (label your outputs clearly). Your code should correctly handle any possible exceptions with try/catch blocks that print an appropriate message.
The functions for the Point class should have the following names:
getXCoord, getYCoord
distanceTo
The functions for the Line class should have the following names:
getPoint1, getPoint2
slope
intersectWith
The files must be named: Point.hpp, Point.cpp, Line.hpp and Line.cpp
Solution
1.Point.hpp
public class Point{
public:
double x;
double y;
double getXCoord();
double getYCoord();
Point(double x,double y);
Point();
double distanceTo(Point p1);
};
2. Line.hpp
public class Line{
public:
Point p1;
Point p2;
Point getPoint1();
Point getPoint2();
Line(Point p1,Point p2);
double slope();
point intersectWith(Line l1);
const double veryclose=0.00001;
};
3. Point.cpp
#include \"Point.hpp\"
#include <math.h>
Point::Point(double x,double y)
{
this.Point();
this.x=x;
this.y=y;
}
double Point::distanceTo(Point p1)
{
double d1=pow((this.x-p1.x),2);
double d2=pow((this.y-p1.y),2);
return sqrt(d1+d2);
}
double Point:: getXCoord()
{
return this.x;
}
double Point:: getYCoord()
{
return this.y;
}
4. Line.cpp
#include \"Line.hpp\"
#include <iostream>
Line::Line(Point p1,Point p2)
{
DegeneratedLineException obj;
try{
if((this.p1.getXCoord()-this.p1.getXCoord())<=0 && (this.p2.getXCoord()-this.p2.getXCoord())<=0)
{
throw obj;
}
}
catch(DegenerateLineException e)
{
cout<<e.what();
}
else{
this.p1=p1;
this.p2=p2;
}
}
double Line::slope()
{
double m=0.0;
double num=this.p2.getYCoord()- this.p1.getYCoord();
double den=this.p2.getXCoord()-this.p2.getYCoord();
try{ if(den<=veryclose)
{
throw UndefinedSlopeException u;
}
}
catch(UndefinedSlopeException u)
{
cout<<u.what();
}
m=num/den;
return m;
}
Point Line::intersectWith(Line l2)
{
try{ if(this.slope()-l2.slope()<=veryclose)
{
throw ParallelLinesException p;
}
}
catch(ParallelLinesException p)
{
cout<<p.what();
}
return
}
5. #include <iostream>
#include <exception>
using namespace std;
class UndefinedSlopeException.: public exception
{
virtual const char* what() const throw()
{
return \"UndefinedSlope\";
}
};
6. #include <iostream>
#include <exception>
using namespace std;
class ParallelLinesException: public exception
{
virtual const char* what() const throw()
{
return \"lines are parallel\";
}
};
7.#include <iostream>
#include <exception>
using namespace std;
class DegenerateLineException.: public exception
{
virtual const char* what() const throw()
{
return \"Points have same coordinates\";
}
};
#include \"Point.hpp\"
#include <iostream>
using namespace std;
int main()
{
Point p1;
Point p2;
Line l1;
Line l2;
Point intersection;
double slopel1=0.0;
double slopel2=0.0
p1=new Point(-1.5,0.0);
p2=new Point(1.5,4.0);
p3=new Point(2.3,1.0);
p4=new Point(1.5,1.1);
l1=new Line(p1,p2);
l2=new Line(p3,p4);
slopel1=l1.slope();
slopel2=l2.slope();
intersection=l1.intersectWith(l2);
cout<<slope1;
cout<<slop2;
cout<<intersection;
}



