Finish this Java program with the following instructions: Before you begin, DOWN
ID: 3730537 • Letter: F
Question
Finish this Java program with the following instructions:
Before you begin, DOWNLOAD the additional files for this lab (Lab7.java, Point.java, and Line.java) using the "Lab7 Data Files.zip" link within the assignment directions in Canvas. You will be modifying two of these three SEPARATE classes (files) Point Line -p1, -p2: Point -slope: double -y intercept: double x: int int +Point() +Point (newX:int newY:int) +Line (first Point, second:Point) +display(): void +intersects (another:Line): boolean +getX():int +getY():int +setX(newX:int):void +setY(newY:int:void 1) Open the Point class and complete by using the UML class diagram given above. The no-arg constructor sets both x and y to 999 Finish the Line.java class, using the UML diagram above as reference, which contains the following a) instance variables: Two Point objects, slope and y-intercept b) constructor takes two Point object variables, which are two points on the line - Within the constructor, calculate the slope and y-intercept and initialize the instance variables c) display-displays line in the form "y = mx + b" representing this line (where ‘m' is the slope and 'b' is the y-intercept). Display ‘m' and 'b, to 2-decimal places intersect - returns true if another intersects this line (else false) d) 3) Finish the Lab7.java class which contains ONLY the main method (driver / application class) - Finish filling the points array per the instructions in the file - Create linel object and line2 object per the instructions - Print both objects by calling the display method - Print "Lines intersect" if the two lines intersect or "Lines do NOT intersect" if the lines don't intersect (using your intersect method)Explanation / Answer
// lab7.java File
import java.util.*;
public class lab7{
public static void main(String[] args){
Point[] points = new Point[4];
points[0] = new Point(1,1);
points[1] = new Point(3,2);
points[2] = new Point(0,4);
points[3] = new Point(1,6);
Line line1 = new Line(points[0], points[1]);
Line line2 = new Line(points[2], points[3]);
line1.display();
line2.display();
boolean result = line1.intersects(line2);
if(result = true)
System.out.println("Lines intersects");
else
System.out.println("Lines do NOT intersects");
// Part 2 code starts:
Scanner in = new Scanner(System.in);
System.out.println("Enter in a new X1 value:");
int x1 = in.nextInt();
System.out.println("Enter in a new y1 value:");
int y1 = in.nextInt();
points[0].setX(x1); points[0].setY(y1);
System.out.println("Enter in a new X2 value:");
int x2 = in.nextInt();
System.out.println("Enter in a new y2 value:");
int y2 = in.nextInt();
points[1].setX(x2); points[1].setY(y2);
line1 = new Line(points[0], points[1]);
line1.display();
line2.display();
result = line1.intersects(line2);
if(result == true)
System.out.println("Lines intersects");
else
System.out.println("Lines do NOT intersects");
}
}
//point.java File
public class Point{
private int x;
private int y;
public Point()
{
x = 999;
y = 999;
}
public Point(int newX, int newY)
{
x = newX; y = newY;
}
public int getX()
{
return x;
}
public int getY()
{
return y;
}
public void setX(int newX)
{
x = newX;
}
public void setY(int newY)
{
y = newY;
}
}
// line.java File
import java.text.DecimalFormat;
import java.util.*;
public class Line
{
private Point p1, p2;
private double slope;
private double y_intercept;
public Line(Point first, Point second)
{
p1 = first;
p2 = second;
slope = (double)(second.getY() - first.getY())/(second.getX() - first.getX());
y_intercept = (double)(first.getY() - slope*first.getX());
}
public void display()
{
String line;
DecimalFormat df = new DecimalFormat("####0.00");
line ="y = " + df.format(slope) + "x + " + df.format(y_intercept);
System.out.println(line);
}
public boolean intersects(Line another)
{
boolean result = false;
double newLineSlope = (another.p2.getY() - another.p1.getY())/(another.p2.getX() - another.p1.getX());
double newLineY_intercept = another.p1.getY() - newLineSlope*another.p1.getX();
if( newLineSlope != slope )
{
result = true;
}
else if( newLineSlope == slope )
{
if( newLineY_intercept == y_intercept )
{
result = true;
}
else
{
result = false;
}
}
else
{
result = false;
}
return result;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.