0% found this document useful (0 votes)
10 views3 pages

Line

The document contains a Java class named 'Line' that defines methods for calculating the slope, length, and relationships (collinearity, parallelism, and perpendicularity) between lines based on points. It includes a main method that interacts with the user to input points and choose functions to execute. The class utilizes a 'Point' class to represent coordinates, although the 'Point' class implementation is not included in the document.

Uploaded by

golyanpanav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views3 pages

Line

The document contains a Java class named 'Line' that defines methods for calculating the slope, length, and relationships (collinearity, parallelism, and perpendicularity) between lines based on points. It includes a main method that interacts with the user to input points and choose functions to execute. The class utilizes a 'Point' class to represent coordinates, although the 'Point' class implementation is not included in the document.

Uploaded by

golyanpanav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

File: Untitled Document 1 Page 1 of 3

import java.util.*;
/**
* necessary instance variables are: Point1
* NULL POINTER EXCEPTION = RUN TIME ERROR
*/
public class Line
{
Point p1;//This variable stores a point
Point p2;//This variable stores a point

//Default constructor
public Line()
{
p1 = new Point();
p2 = new Point();
p1.y = 4;
p2.y = 4;
}

//Parameeterised constructor
public Line(Point p1 , Point p2)
{
this.p1 = p1;
this.p2 = p2;
}

//This function finds the slope


double getSlope ()
{
double xfinal = p2.x - p1.x;
double yfinal = p2.y - p1.y;
return yfinal/xfinal;
}

//This function finds the length of a line


double getLength()
{
double length = p1.distance(p2.x , p2.y);
return length;
}

//This function finds if three points are collinear


boolean isCollinear(Point p3)
{
double m = getSlope();

double xfinal = p1.x - p3.x;


double yfinal = p1.y - p3.y;
double m2 = yfinal / xfinal;

return m == m2;
}

//This function finds if two lines are parallel


boolean isParallel(Line l2)
{
return getSlope() == l2.getSlope();
}

//This function finds if two lines are parallel


boolean isPerpendicular(Line l2)
{
return getSlope() * l2.getSlope() == -1;
}

public static void main (String [] args)


File: Untitled Document 1 Page 2 of 3

{
Scanner sc = new Scanner(System.in);
int option , start , reuse;

Point p1;
p1 = new Point();

Point p2;
p2 = new Point();

Line l2;
l2 = new Line();

System.out.print("Enter 1 to use the function and 0 to exit");


start = sc.nextInt();
while(start!=0)
{
System.out.println("Enter the coordinates of the first point of line 1:\t");
p1.readCoordinates();

System.out.println("Enter the coordinates of the second point of line 1:\t");


p2.readCoordinates();

Line l1;
l1 = new Line(p1 , p2);

System.out.print("Enter 1 to use this data or else enter 0\t");


reuse = sc.nextInt();
while(reuse!=0)
{
System.out.println("Enter 1. to use the slope function");
System.out.println("Enter 2. to use is collinear function");
System.out.println("Enter 3. to use is parallel function");
System.out.println("Enter 4. to use is perpendicular function");
option = sc.nextInt();

switch (option)
{
case 1:
System.out.println("The Slope of the line is:" + l1.getSlope());
break;
case 2:
System.out.println("Enter the coordinates of another point in the
same line :");
Point p3;
p3 = new Point();
p3.readCoordinates();
System.out.print (l1.isCollinear(p3));
break;
case 3:
System.out.println("Enter the coordinates of the first point of
line 2:\t");
l2.p1.readCoordinates();
System.out.println("Enter the coordinates of the second point of
line 2:\t");
l2.p2.readCoordinates();
System.out.println(l1.isParallel(l2));
break;
case 4:
System.out.println("Enter the coordinates of the first point of
line 2:\t");
l2.p1.readCoordinates();
System.out.println("Enter the coordinates of the second point of
line 2:\t");
l2.p2.readCoordinates();
System.out.println(l1.isPerpendicular(l2));
File: Untitled Document 1 Page 3 of 3

break;
default:
System.out.println("Pls choose a correct option");// Break is not
required for case in the end
}
System.out.println("Enter 1 to use this data or else enter 0\t");
reuse = sc.nextInt();
}
System.out.println("Enter 1 to use the function and 0 to exit\t");
start = sc.nextInt();
}
/**
* Output:
*
* Enter 1 to use the function and 0 to exit1
Enter the coordinates of the first point of line 1:
Enter the coordinates of x axis 3
Enter the coordinates of y axis 5
Enter the coordinates of the second point of line 1:
Enter the coordinates of x axis 3
Enter the coordinates of y axis 8
Enter 1 to use this data or else enter 0 1
Enter 1. to use the slope function
Enter 2. to use is collinear function
Enter 3. to use is parallel function
Enter 4. to use is perpendicular function
1
The Slope of the line is:Infinity
Enter 1 to use this data or else enter 0
0
Enter 1 to use the function and 0 to exit
0

*/
}
}

You might also like