Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

**C++** Design a class Line that implements a line, which is represented by thef

ID: 3781313 • Letter: #

Question

**C++**

Design a class Line that implements a line, which is represented by theformula y = ax+b. Your class should store a and b as double member variables andwrite a member function intersect() such that L.intersect(K) returns the x coordinate ofthe unique point at which lines L and K intersect. If there is not a unique such point,the function should throw an appropriate exception.


The function should throw an exception if the two lines are the same and hence havean infinite number of common points; or are parallel and hence have no point ofintersection. For this, you will need subclasses EqualLines and ParallelLiines of theclass RunTimeException. The message part of theexceptions should be "The lines are equal: infinite intersection" for EqualLines and"The lines are parallel: no intersection" for ParallelLiines. When either exception iscaught, the messages will be printed. This will occur in the program that uses the Lineclass. The class methods should not produce any output.

Line.cpp


#include <iostream>#include "line.h"
double Line::intersect(const Line L) const throw(ParallelLines,

   EqualLines)

{ }
double Line::get_y(double z) const{
}

Line.h

#include <string>using namespace std;
class RuntimeException { // generic run-time exception private:  string errorMsg; public:  RuntimeException(const string& err) { errorMsg = err; }  string getMessage() const { return errorMsg; }};
class EqualLines: public RuntimeException{ public:
};
class ParallelLines: public RuntimeException{ public:
};
class Line { public:  Line(double slope, double y_intercept): a(slope), b(y_intercept) {};  double intersect(const Line L) const throw(ParallelLines,

    EqualLines);

  double getSlope() const {return a;};  double getIntercept() const {return b;};   // return the y-coordinate of the point with x-coordinate z:  double get_y(double z) const;  private:  double a;  double b;};

Explanation / Answer

line.h ------

#ifndef LINE_H
#define LINE_H

#include<iostream>

using namespace std;

class RuntimeException
{
   private:
  
   string errorMsg;
  
   public:
  
   RuntimeException(const string& err)
   {
       errorMsg = err;
   }
  
   string getMessage() const { return errorMsg; }
  
};
  
class EqualLines: public RuntimeException
{
   public:
  
   EqualLines(string s):RuntimeException(s)
   {
      
   }
  
};

class ParallelLines: public RuntimeException
{
   public:
  
   ParallelLines(string s):RuntimeException(s)
   {
      
   }
  
};

class Line
{
   double a,b;

   public:

   Line(double slope, double y_intercept)
   {
       a = slope, b = y_intercept ;
   }

   double intersect(const Line L) const throw(ParallelLines,EqualLines);
  
   double getSlope() const {return a;};
  
   double getIntercept() const {return b;};
  
};  

#endif

line.cpp ---

#include<iostream>

using namespace std;

#include"line.h"

double Line::intersect(const Line L) const throw(ParallelLines,EqualLines)
{
   double x1 = a , y1 = b;
   double x2 = L.getSlope() , y2 = L.getIntercept();
   if( x1 == x2 && y1 == y2)
       throw EqualLines(" The lines are equal : infinite intersection ");
   else if(x1 == x2)
       throw ParallelLines(" The lines are parallel: no intersection ");
   else
   {
       double m = x1 - x2 , n = - y1 + y2 ;
       return (n / m);
   }
}

int main()
{
   double x1,x2,y1,y2;
   cout<<" Slope of line 1 : ";
   cin>>x1;
   cout<<" Intercept of line 1 : ";
   cin>>y1;
   cout<<" Slope of line 2 : ";
   cin>>x2;
   cout<<" Intercept of line 2 : ";
   cin>>y2;
   //cout<<x1<<endl<<y1<<endl<<x2<<endl<<y2<<endl;
   Line L(x1,y1),K(x2,y2);
   try
   {
       double intersectingPoint=L.intersect(K);
       cout<<" The x coordinate of the unique point at which lines L and K intersect : "<<intersectingPoint<<endl;
   }catch(RuntimeException E)
   {
       cout<<E.getMessage();
   }
   return 0;
}