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

Write a java program for the following. The equation of a line in standard form

ID: 3821434 • Letter: W

Question

Write a java program for the following.

The equation of a line in standard form is ax + by = c, where a and b both cannot be zero, and a, b, and c are real number. If b 0, then –a/b is the slope of the line. If a = 0, then it is a horizontal line, and if b = 0, then it is a vertical line. The slope of a vertical line is undefined. Two lines are parallel if they have the same slope or both are vertical lines. Two line are perpendicular if one of the lines is horizontal and another is vertical, or if the product of their slopes is -1. Design the class Line to store a line. To store line, you need to store the values of a (coefficient of x), b (coefficient of y), and c. Your class must contain the following operations:

a. If a line is nonvertical, then determine its slope.

b. Determine if two lines are equal. (Two lines a1x +b1x =c1 and a2x + b2y = c2 are equal if either

a1 = a2 , b1 = b2 , and c1 = c2 or a1 = ka2 , b1 = kb2 , and c1 = kc2 for some real number k.)

c. Determine if two lines are parallel.

d. Determine if two lines are perpendicular.

e. If two lines are not parallel, then find the point of intersection.

Add appropriate constructors to initialize variables of Line. Also write a program to test your class.

Explanation / Answer

Line.java


public class Line {
private double a;
private double b;
private double c;
  
private boolean isVertical;
private boolean isHorizontal;

Line(double a, double b, double c)
{
this.a = a;
this.b = b;
this.c = c;
isHorizontal = false;
isVertical = false;
  
if (a == 0)
isHorizontal = true;
  
if (b == 0)
isVertical = true;
}
  
public double slope()
{
if (!isVertical)
{
return -1*a/b;
}
return 0;
}
  
public boolean isEqual(Line l)
{
  
if (a == l.a && b == l.b && c == l.c)
{
return true;
}
double multiple = a/l.a;
  
if (b/l.b != multiple)
return false;
  
if (c/l.c != multiple)
return false;
  
return true;
}
  
public boolean isParallel(Line l)
{
return slope() == l.slope();
}
  
public boolean isPerpendicular(Line l)
{
return (isVertical && l.isHorizontal) || (isHorizontal && l.isVertical) || (slope()*l.slope() == -1);
}
  
public void intersectionPoint(Line l)
{
if (!isParallel(l))
{
double y = (c*l.a - a*l.c)/(l.a*b - a*l.b);
double x = (c - b*y)/a;
  
System.out.println("Intersection point: (" + x + ", " + y + ")");
}
}
}

TestLine.java


public class TestLine {
public static void main(String[] args)
{
Line l1 = new Line(1, 1, 1);
Line l2 = new Line(1, 1, 1);
  
System.out.println("Slope of l1: " + l1.slope());
  
if (l1.isEqual(l2))
{
System.out.println("l1 and l2 are equal");
}
  
Line l3 = new Line(2, 2, 2);
if(l1.isParallel(l3))
{
System.out.println("l1 and l3 are parallel");
}
  
Line l4 = new Line(-1, 1, 1);
if(l1.isPerpendicular(l4))
{
System.out.println("l1 and l4 are perpendicular");
}
  
l1.intersectionPoint(l4);
  
}
}

Sample output

Slope of l1: -1.0
l1 and l2 are equal
l1 and l3 are parallel
l1 and l4 are perpendicular
Intersection point: (0.0, 1.0)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote