C problem only Write a class called Point that contains two

C++ problem only

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

Note: Given exceptions in java

Point.h

// Include required head files

#include <cmath>

#ifndef POINT_H

#define POINT_H

// Class point

class Point

{

// Declare the required variables

private:

double xval;

double yval;

  

// Method prototype declaration

public:

Point();

Point(double pnt_xval, double pnt_yval);

void setX_Coord(double pnt_xval);

void setY_Coord(double pnt_yval);

double getX_Coord();

double getY_Coord();

double distanceTo(const Point &pnt);

};

#endif

Point.cpp

// Include required head files

#include \"Point.h\"

// point class Method definition

// Constructor

Point::Point()

{

setX_Coord(0);

setY_Coord(0);

}

// Constructor with arguments

Point::Point(double pnt_xval, double pnt_yval)

{

setX_Coord(pnt_xval);

setY_Coord(pnt_yval);

}

// Method to set x coordinate

void Point::setX_Coord(double pnt_xval)

{

xval = pnt_xval;

}

// Method to set y coordinate

void Point::setY_Coord(double pnt_yval)

{

yval = pnt_yval;

}

// Method to get x coordinate

double Point::getX_Coord()

{

return xval;

}

// Method to get y coordinate

double Point::getY_Coord()

{

return yval;

}

// Method to calculate the distance between points

double Point::distanceTo(const Point &pnt)

{

return sqrt(pow((xval - pnt.xval), 2) + pow((yval - pnt.yval), 2));

}

Line.h

// Include required head files

#include \"Point.h\"

#ifndef LINE_H

#define LINET_H

//Class linesegment

class Line
{

// Declare the required variables

private:

Point pnt1,pnt2;

// Method protocol declaration

public:

Line();

Line(Point &pnt1, Point &pnt2);

void setEnd1(Point &pnt);

void setEnd2(Point &pnt);

Point &getEnd1();

Point &getEnd2();

double getLength();

double getSlope();
      
       Point intersection(Line l2);

};

#endif

Line.cpp

// Include required head files

#include \"Line.h\"

#include <iostream>

using namespace std;

// Method definition for the class linesegment

// Constructor

Line::Line()

{

Point pnt1, pnt2;

setEnd1(pnt1);

setEnd2(pnt2);

}

// Constructor with arguments

Line::Line(Point &pnt1, Point &pnt2)

{
   if((pnt1.getX_Coord()==pnt2.getX_Coord()) && (pnt1.getY_Coord()==pnt2.getY_Coord()) )
   {
       cout<<\"DegenerateLine Exception\"<<endl;
   }
   else
   {
   setEnd1(pnt1);

   setEnd2(pnt2);
   }

}

// Method to set the endpoint1

void Line::setEnd1(Point &pnt)

{

pnt1.setX_Coord(pnt.getX_Coord());

pnt1.setY_Coord(pnt.getY_Coord());

}

// Method to set the endpoint2

void Line::setEnd2(Point &pnt)

{

pnt2.setX_Coord(pnt.getX_Coord());

pnt2.setY_Coord(pnt.getY_Coord());

}

// Method to get the end point1

Point &Line::getEnd1()

{

return pnt1;

}

// Method to get the end point2

Point &Line::getEnd2()

{

return pnt2;

}

// Method to get the length of the line

double Line::getLength(){

return pnt1.distanceTo(pnt2);

}

// Method to calculate eth eline slope

double Line::getSlope()

{

if (pnt2.getX_Coord() - pnt1.getX_Coord() == 0)

{
   cout<<\"slope unidentified\"<<endl;
return -55555;

}

else

{

return   (pnt2.getY_Coord()-pnt1.getY_Coord())/ (pnt2.getX_Coord() - pnt1.getX_Coord());

}
}

// Method to check the line is slope or not
Point Line:: intersection(Line l2)
{
   Point c;

   float c1=pnt1.getY_Coord()-(getSlope()*pnt1.getX_Coord());
   float c2=l2.pnt1.getY_Coord()-(l2.getSlope()*l2.pnt1.getX_Coord());
   if( (getSlope()- l2.getSlope()) == 0)
std::cout << \"Parallel lines\";
else
{
c.setX_Coord((c2 - c1) / (getSlope() - l2.getSlope()));
c.setY_Coord (getSlope() * c.getX_Coord() + c1);


   }
   return c;
}


int main()

{

double x1, y1, x2, y2;

Point p1(1, 2);

Point p2(5, 7);

cout << \"Co-ordinates for first point: 0 0\"<< endl;

cout << \"Co-ordinates for second point: 3 4\"<< endl;

cout << \"Length of the line segment is:\"

<<p1.distanceTo(p2) << endl;

cout << \"Co-ordinates for first point: \";

cin >> x1 >> y1;

cout << \"Co-ordinates for second point: \";

cin >> x2 >> y2;

Point p3(x1, y1), p4(x2, y2);

Line L(p3, p4);

Line L1(p1,p2);

cout << \"Length of the line segment is \" <<

L.getLength() << endl;

cout << \"Slope of the Line segment is \" <<

L.getSlope() << endl;

Point k=L1.intersection(L);
cout<<\"intersection points are\"<<k.getX_Coord()<<\" , \"<<k.getY_Coord()<<endl;

system(\"pause\");

return 0;

}

C++ problem only 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.
C++ problem only 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.
C++ problem only 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.
C++ problem only 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.
C++ problem only 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.
C++ problem only 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.
C++ problem only 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.

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site