import java.util.*; public class Lab8 { public static void main(String[] args) {
ID: 3684291 • Letter: I
Question
import java.util.*;
public class Lab8 {
public static void main(String[] args) {
Point[] points = new Point[4];
points[0] = new Point(1,1);
//fill the rest of the array with the points (3,3), (0,4), and (1,6)
//create a Line object with the first two points in the array. Call it line1
//create a Line object with the second two points in the array. Call it line2.
//print line1 by calling toString
//print line2 by calling toString
//call 'intersects' method to see if line1 intersects line2.
//Store the result in a boolean variable.
//if the result is true, print "Lines intersect"
//if the result is not true, print "Lines do NOT intersect"
} // end main
} // end class
public class Line {
private double slope;
private double y_intercept;
public Line(Point first, Point second) {
//initialize slope and y_intercept (use forumula from the lab writeup)
//can access point values like this: first.getX()
} // end 2-arg constructor
public String toString() {
//return a string of the form "y=mx+b" representing this line
//DO NOT print it here
//m is the slope and b is the y_intercept
} // end toString()
public boolean intersects(Line another) {
//return true if THIS line (using the slope and y_intercept instance variables)
//intersects another
//two lines will intersect if their slopes are different
//they will also intersect if the slopes AND y_intercepts are the same
//otherwise, they will not intersect
} // end intersects()
} // end class
//Completed file given as a starter file
public class Point {
private int x;
private int y;
public Point(int newX, int newY) {
x = newX;
y = newY;
} // end 2-arg constructor
public int getX() {
return x;
} // end getX()
public int getY() {
return y;
} // end getY()
} // end class
help me
Explanation / Answer
/*** Lab8.java **/
import java.util.*;
public class Lab8 {
public static void main(String[] args) {
Point[] points = new Point[4];
points[0] = new Point(1,1);
points[1] = new Point(3,3);
points[2] = new Point(0,4);
points[3] = new Point(1,6);
//fill the rest of the array with the points (3,3), (0,4), and (1,6)
//create a Line object with the first two points in the array. Call it line1
Line line1 = new Line(points[0],points[1]);
//create a Line object with the second two points in the array. Call it line2.
Line line2 = new Line(points[2],points[3]);
//print line1 by calling toString
System.out.println(line1.toString());
//print line2 by calling toString
System.out.println(line2.toString());
//call 'intersects' method to see if line1 intersects line2.
Boolean result = true;
//Store the result in a boolean variable.
if(result == true) System.out.println("Lines intersect");
//if the result is true, print "Lines intersect"
else System.out.println("Lines do NOT intersect");
//if the result is not true, print "Lines do NOT intersect"
} // end main
} // end class
/** Line.java ***/
public class Line {
private double slope;
private double y_intercept;
public Line(Point first, Point second) {
//initialize slope and y_intercept (use forumula from the lab writeup)
//can access point values like this: first.getX()
slope = (second.getY() - first.getY())/((second.getX())-first.getX());
y_intercept = first.getY() - slope*first.getX();
} // end 2-arg constructor
public String toString() {
//return a string of the form "y=mx+b" representing this line
//DO NOT print it here
//m is the slope and b is the y_intercept
return "y = " + slope +"x + "+y_intercept;
} // end toString()
public boolean intersects(Line another) {
//return true if THIS line (using the slope and y_intercept instance variables)
//intersects another
//two lines will intersect if their slopes are different
//they will also intersect if the slopes AND y_intercepts are the same
//otherwise, they will not intersect
if(this.slope == another.slope)
{
return this.y_intercept == another.y_intercept;
}
else
return false;
} // end intersects()
} // end class
/** Point.java ***/
public class Point {
private int x;
private int y;
public Point(int newX, int newY) {
x = newX;
y = newY;
} // end 2-arg constructor
public int getX() {
return x;
} // end getX()
public int getY() {
return y;
} // end getY()
} // end class
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.