Implementing Line Equations In Java
Not what you're looking for?
A line in the plane can be specified in various ways:
by giving a point (x, y) and a slope m
by giving two points (x1, y1), (x2, y2)
as an equation in slope-intercept form y = mx + b
as an equation x = a if the line is vertical
Implement a class Line with four constructors, corresponding to the four cases above. Implement methods
boolean intersects(Line other)
boolean equals(Line other)
boolean isParallel(Line other)
Use the following class as your tester class:
public class LineTester
{
public static void main(String[] args)
{
Line line1 = new Line(1, 1, 0.5);
Line line2 = new Line(1, 1, 1, 2);
Line line3 = new Line(0.5, -1);
Line line4 = new Line(1);
System.out.println(line1.equals(line2));
System.out.println("Expected: false");
System.out.println(line2.equals(line4));
System.out.println("Expected: true");
System.out.println(line1.intersects(line2));
System.out.println("Expected: true");
System.out.println(line1.intersects(line3));
System.out.println("Expected: false");
System.out.println(line1.isParallel(line3));
System.out.println("Expected: true");
System.out.println(line2.isParallel(line4));
System.out.println("Expected: true");
System.out.println(line1.isParallel(line2));
System.out.println("Expected: false");
}
}.
Purchase this Solution
Solution Summary
The solution discusses implementing line equations in Java.
Solution Preview
Please see the attachment in case something does not appear clearly here.
class Line {
double slope;
double xIntercept;
double yIntercept;
public Line(double x0, double y0, double m) {
// The line has equation y - y0 = m(x - x0)
// Then y = mx + (y0 - m*x0)
this.slope = m;
this.yIntercept = y0 - m * x0;
}
public Line(double x1, double y1, double x2, double y2) {
if (x1==x2) {
// The line has equation x = x1
this.slope = Double.MAX_VALUE; // slope is infinity
this.xIntercept = x1;
} else {
// The slope is m = (y2 - y1)/(x2 - ...
Purchase this Solution
Free BrainMass Quizzes
Word 2010: Tables
Have you never worked with Tables in Word 2010? Maybe it has been a while since you have used a Table in Word and you need to brush up on your skills. Several keywords and popular options are discussed as you go through this quiz.
Java loops
This quiz checks your knowledge of for and while loops in Java. For and while loops are essential building blocks for all Java programs. Having a solid understanding of these constructs is critical for success in programming Java.
Basic UNIX commands
Use this quiz to check your knowledge of a few common UNIX commands. The quiz covers some of the most essential UNIX commands and their basic usage. If you can pass this quiz then you are clearly on your way to becoming an effective UNIX command line user.
Word 2010: Table of Contents
Ever wondered where a Table of Contents in a Word document comes from? Maybe you need a refresher on the topic? This quiz will remind you of the keywords and options used when working with a T.O.C. in Word 2010.
Inserting and deleting in a linked list
This quiz tests your understanding of how to insert and delete elements in a linked list. Understanding of the use of linked lists, and the related performance aspects, is an important fundamental skill of computer science data structures.