Question: 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
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:
Point p1(-1.5, 0.0); Point p2(1.5, 4.0); double dist = p1.distanceTo(p2);
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:
Line line1(p1, p2); Line line2(p3, p4); double slope = line1.slope(); Point intersection = line1.intersectWith(line2);
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.
You can find an equation for the intersection of two lines here (Links to an external site.)Links to an external site..
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
Point.cpp and Line.hpp should both #include Point.hpp. Line.cpp should #include Line.hpp. Your main method will also need to include Line.hpp. If you named the file with your main method "geomMain.cpp", then you can compile your program with "g++ Point.cpp Line.cpp geomMain.cpp -o geom", which will create an executable file named "geom".
THIS IS C++
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
