SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Stuart Reges, Marty Stepp
Chapter:
Classes
Exercise:
Exercises
Question:12 | ISBN:9780136091813 | Edition: 2

Question

Add the following method to your Line class:
public boolean isCollinear(Point p)
Returns true if the given Point is collinear with the Points of this Line—that is, if, when this Line is stretched infinitely, it would eventually hit the given Point. Points are collinear if a straight line can be drawn that connects them. Two basic examples are three points that have the same x- or y-coordinate. The more general case can be determined by calculating the slope of the line between each pair of points and checking whether this slope is the same for all pairs of points. Use the formula (y2 – y1) / (x2 – x1) to determine the slope between two points (x1, y1) and (x2, y2). (Note that this formula fails for points with identical x-coordinates, so this will have to be a special case
in your code.) Since Java’s double type is imprecise, round all slope values to a reasonable accuracy such as four
digits past the decimal point before you compare them.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

// package classes;

public class Line {

	Point point1;
	Point point2;

// initialize the line class with the constructor
	public Line(Point Point1, Point point2) {
		this.point1 = Point1;
		this.point2 = point2;
	}

     // initialize the line class by passing the points
	public Line(int x1, int y1, int x2, int y2) {

		this.point1 = new Point(x1, y1);
		this.point2 = new Point(x2, y2);
	}

	public Point getPoint1() {
		return point1;
	}

	public Point getPoint2() {
		return point2;
	}

    // override the tostring to print the points
	public String toString() {
		return "[" + point1.toString() + ", " + point2.toString() + "]";
	}


    //this method return the slope of two points
	public double getSlope() {
		if (point1.getX() == point2.getX())
			throw new IllegalArgumentException();

		double slope = (double) (point2.getY() - point1.getY()) / (point2.getX() - point1.getX());
		return slope;
	}
	
	
public boolean isCollinear(Point pnt) {
		
		
	    if(this.point1.x == this.point2.x && this.point2.x == pnt.x)
	        return false;
	    
	    if(this.point1.y == this.point2.y && this.point2.y == pnt.y)
	        return false;
	        
	    double slope1 = ((double) (pnt.y - this.point1.y)) / (pnt.x - this.point1.x);
	    double slope2 = ((double) (pnt.y - this.point2.y)) / (pnt.x - this.point2.x);
	    
	    if(slope1 == slope2)
	        return true;
	        
	    return false;
	}

     // driver method
	public static void main(String[] args) {

			Line line = new Line(2,4,4,6);
            Point pnt = new Point(6,3);
        // try these sample collinear points (2,4) (4,6) (6,8)   
		System.out.println("the string representation of the line is :" + line);
		System.out.println("new point: " +pnt);
		
		System.out.println("are the three points are in collinear: " +line.isCollinear(pnt));

	}

}
Output:

the string representation of the line is :[(2,4), (4,6)]
new point: (6,3)
are the three points are in collinear: false

 

0 0

Discussions

Post the discussion to improve the above solution.