In: Computer Science
Use C++ for this program
Define a Line Class.
Constraint: Line must be in First Quadrant
for X-Y Coordinate System.
Create the following methods:
1) Parameterized Constructor
2) Horizontal Line Constructor
3) Vertical Line Constructor
4) Default Constructor
5) Compute Length of Line.
TEST CASES: Find Length of Line
(a) Point 1: (5,6) Point 2: (15,6)
(b) Point 1: (7,8) Point 2: (7,20)
(c) Point 1: (9,10) Point 2: (19,20)
(d) Default Line of 10 units at (0,0).
A quick question, would this be put in a header file and called into a source file?
//Line Class
#include <bits/stdc++.h>
using namespace std;
class Line {
private:
// x and y cordinates of two point Point1 and
Point2
int xCordinate_Point1, yCordinate_Point1,
xCordinate_Point2, yCordinate_Point2;
public:
// 1) Horizontal Constructor where cordinate
of first points are parameters and length of line
Line(int xCordinate_Point1,int
yCordinate_Point1,int length){
//CHECKING Line must be in
first qudrant
if(xCordinate_Point1 <
0 || yCordinate_Point1 < 0){
cout<<"Line is Not in First Quadrant"<<endl;
}
else{
// this
keyword holds reference of current object
this->xCordinate_Point1 = xCordinate_Point1;
this->yCordinate_Point1 = yCordinate_Point1;
// for second
cordinate of horizontal line y cordinate remains same while for x
length is added
this->xCordinate_Point2 = xCordinate_Point1 + length;
this->yCordinate_Point2 = yCordinate_Point1;
}
}
// 2) Vertical Constructor where cordinate of
first points are parameters and length of line
/*Here one extra parameter is added just to
differentite from Horizontal Constructor so
that ambiguity can be removed and constructor can be
overloaded in parameter vertical
v is passed to indicate vertical */
Line(int xCordinate_Point1,int
yCordinate_Point1,int length,char vertical){
//CHECKING Line must be in first
qudrant
if(xCordinate_Point1 < 0 ||
yCordinate_Point1 < 0){
cout<<"Line is Not in First Quadrant"<<endl;
return;
}
else{
// this
keyword holds reference of current object
this->xCordinate_Point1 = xCordinate_Point1;
this->yCordinate_Point1 = yCordinate_Point1;
// for second
cordinate of vertical line x cordinate remains same while for y
length is added
this->xCordinate_Point2 = xCordinate_Point1;
this->yCordinate_Point2 = yCordinate_Point1+length;
}
}
// 3) Parameterized Constructor where
cordinates of points are parameters
Line(int xCordinate_Point1,int
yCordinate_Point1,int xCordinate_Point2,int
yCordinate_Point2){
//CHECKING Line must be in
first qudrant
if(xCordinate_Point1 <
0 || yCordinate_Point1 < 0 || xCordinate_Point2 < 0 ||
yCordinate_Point2 < 0){
cout<<"Line is Not in First Quadrant"<<endl;
return;
}
// this keyword holds reference
of current object
this->xCordinate_Point1 =
xCordinate_Point1;
this->yCordinate_Point1 =
yCordinate_Point1;
this->xCordinate_Point2 =
xCordinate_Point2;
this->yCordinate_Point2 =
yCordinate_Point2;
}
/* 4) Default Constructor where cordinates of
points are not passed and no initialisation so only
object reference is created but object is not
initialized*/
Line(){
}
// 5) Compute length function
double computeLength(){
// this keyword holds
reference of current object
// using distance formulae distance
= sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2))
int x_difference =
(this->xCordinate_Point1 - this->xCordinate_Point2);
int y_difference =
(this->yCordinate_Point1 - this->yCordinate_Point2);
int square_sum = x_difference *
x_difference + y_difference * y_difference;
//taking square root
return
pow(square_sum,0.5);
}
};
int main(){
// creating line object and finding the
distance between points
// PART-(a)
//Point 1: (5,6) Point 2: (15,6)
Line *line1 = new Line(5,6,15,6);
cout<<"Length between Point 1: (5,6) Point 2:
(15,6) is "<<line1->computeLength()<<endl;
// PART-(b)
//Point 1: (7,8) Point 2: (7,20)
Line *line2 = new Line(7,8,7,20);
cout<<"Length between Point 1: (7,8) Point 2:
(7,20) is "<<line2->computeLength()<<endl;
// PART-(c)
//Point 1: (9,10) Point 2: (19,20)
Line *line3 = new Line(9,10,19,20);
cout<<"Length between Point 1: (9,10) Point 2:
(19,20) is "<<line3->computeLength()<<endl;
// PART-(d)
//Default Line of 10 units at (0,0).
Line *line4 = new Line(0,0,10);
cout<<"Default Line of 10 units at (0,0) is
drawn"<<endl;
}
//OUTPUT TERMINAL

For Answer of Quick Question :
Yes It can be done as this class can be used in source file and required methods can be called from there.