Extend Exercises 14-17 to create a LongLine Class described below: SUBMIT LongLi
ID: 645682 • Letter: E
Question
Extend Exercises 14-17 to create a LongLine Class described below:
SUBMIT LongLine.java (no main(), no System calls)
This means your LongLine.java file will contain the following line of code:
public class LongLine extends Line {
// no additional fields allowed, just some added methods:
Your LongLine Class will inherit all behavior of the line segment that we've already completed, and you can assume that I do have a full and correct solution to the Line Class Exercises as described on page 569 of text (given in Line.java). The LongLine differs in that it represents the full line for all points in Cartesian coordinates. So we define the following 4 additional methods for a LongLine (toString, getIntercept, doesIntersect, and intersection):
LongLine a = new LongLine(new Point(), new Point(1,1)); // creates line of slope 1 and zero intercept
System.out.println(a); // should now print in the form y=mx+b, so this will be just y=x
System.out.println(a.getIntercept()); // returns y-intercept, so in this case is 0
LongLine b = new LongLine(new Point(1,1), new Point(2,0)); // creates another line
System.out.println(b); // y = -x + 2 yes, this toString() have MANY special cases.....
// toString() should avoid too many decimals places, more like y = (-9.76E-4) x - (6.79E+8) for large or small numbers
// toString() should use the single integer rounded number whenever within 0.001 of an integer so (-6.001) displays as -6
System.out.println(a.doesIntersect(b)); // boolean that tells us if two lines intersect, true here
System.out.println(a.intersection(b)); // returns the Point of intersection (1,1) in this case.
Here is the original Line.java:
import java.awt.Point;
public class Line {
// Keep fields private
private Point p1;
private Point p2;
// Constructors
public Line(Point p1, Point p2){
this(p1.x,p1.y,p2.x,p2.y);
}
// Default
public Line() {
this(0,0,0,0);
}
// Four argument int, used by all other constructors
public Line (int x1, int y1, int x2, int y2){
p1 = new Point(x1,y1);
p2 = new Point(x2,y2);
}
// ACCESSORS
public Point getP1(){
return this.p1;
}
// so simple
public Point getP2(){
return this.p2;
}
// standard slope calculation
public double getSlope(){
// if vertical, throw an exception
if (p1.x==p2.x) {
throw new IllegalStateException("IllegalStateException");
}
return (double)(p2.y-p1.y)/(p2.x-p1.x);
}
// All points on the same line?
public boolean isCollinear(Point p){
if (p.x==p1.x && p.x==p2.x)
return true; // vertical lines are a special case
else if (p1.x==p2.x && p1.x!=p.x)
return false; // another special case revealed by testers
else {
Line test = new Line(p1.x, p1.y, p.x, p.y);
double difference = test.getSlope() - this.getSlope();
if (Math.abs(difference) < 0.0001) return true; // close enough
}
return false; // none of above cases even tested
}
// Default toString() operation used by many....
public String toString(){
return ("[("+p1.x+", "+p1.y+"), ("+p2.x+", "+p2.y+")]");
}
}
Explanation / Answer
/*********************************/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.