Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

need help on a homework assignment for a java class. we have only discussed cond

ID: 3789368 • Letter: N

Question

need help on a homework assignment for a java class.

we have only discussed conditional statementes and arrays so far in class. So my code would have to only include condtional statements and arrays at the most. also the code for this program should all be in one block and not have any classes or constructors since we havent discussed them yet. also we cant use API for this program.

here is the homework assignment:

Build an Intersection Check program. User inputs two points as x-y coordinates in twodimension space. Two points represent two end points of a line segment. Then, user can input second two points for the second line segment. With the two line segments, the program checks whether or not the two line segments intersect. The program displays “1” if the two line segments are crossing. Otherwise, display “0”. Please implement your algorithm in Java but not use API. Hint: use slop and intercept
Input:

Four x-y coordinates that represent two point for the first line segment and two points for the second line segments

OUTPUT:

0 or 1 that represents intersection or no intersection

Example

Input: 7 8 5 12 10 10 3 10

Output: 1

Do you want to test again? Y

Input: 5 10 5 2 2 1 8 7

Output: 1

Do you want to test again? Y

Input: 10 10 3 10 6 3 6 6

Output: 0

Do you want to test again? Y

Input: 5 4 8 6 10 6 5 1

Output: 0

Do you want to test again? N

Thank you!

Explanation / Answer

Point.java

public class Point {
public double x, y;
public Point(double x, double y) {
super();
this.x = x;
this.y = y;
}
}

====================================================================================

Line.java

public class Line {

public final Point start, end;
public final boolean isVertical;
public final double M_SLOPE, intercept;
  
public Line(Point start, Point end) {
this.start = start;
this.end = end;

if (start.x == end.x)
isVertical = true;
else
isVertical = false;


if (!isVertical){
M_SLOPE = (start.y - end.y) / (start.x - end.x);
intercept = (end.x * start.y - start.x * end.y ) /(start.x - end.x);
}
else {
M_SLOPE = Double.MAX_VALUE;
intercept = - Double.MAX_VALUE;
}
}
}

====================================================================================

intersection.java

import java.util.Scanner;

public class Intersection {

public final Line Line1, Line2;
private Boolean intersectOrNot;
  
public Intersection(Line Line1, Line Line2){
this.Line1 = Line1;
this.Line2 = Line2;
}
  
public Intersection(double x1, double y1, double x2, double y2,
double x3, double y3, double x4, double y4){
Point start1 = new Point(x1, y1);
Point end1 = new Point(x2, y2);
Point start2 = new Point(x3, y3);
Point end2 = new Point(x4, y4);
this.Line1 = new Line(start1, end1);
this.Line2 = new Line(start2, end2);
}
  
public boolean intersectOrNot(){
if (intersectOrNot != null)
return intersectOrNot;

if (Line1.isVertical){
if ( (Line2.start.x - Line1.start.x)*(Line2.end.x - Line1.start.x) > 0 )
intersectOrNot = false;
else {
double fx_at_Line1startx = Line1.M_SLOPE * Line1.start.x + Line1.intercept;
double smaller, larger;
if (Line1.start.x < Line1.end.x ){
smaller = Line1.start.x;larger = Line1.end.x;
}
else {
larger = Line1.start.x;smaller = Line1.end.x;
}
if (smaller <= fx_at_Line1startx && fx_at_Line1startx <= larger)
intersectOrNot = true;
else
intersectOrNot = false;
}
}
else if (Line2.isVertical){
intersectOrNot = new Intersection(Line2, Line1).intersectOrNot();
}
else { //both Line1 and Line2 are not vertical
if (Line1.M_SLOPE == Line2.M_SLOPE)
intersectOrNot = false;
else {
double x1 = Line1.start.x;
double y1 = Line1.start.y;
double x2 = Line1.end.x;
double y2 = Line1.end.y;
double x3 = Line2.start.x;
double y3 = Line2.start.y;
double x4 = Line2.end.x;
double y4 = Line2.end.y;
double x = ((x4*y3-y4*x3)/(x4-x3) - (x2*y1-y2*x1)/(x2-x1))
/( (y2-y1)/(x2-x1) - (y4-y3)/(x4-x3));

double smaller, larger;
if (x1 < x2){
smaller = x1; larger = x2;
}
else {
smaller = x2; larger = x1;
}
if (smaller <= x && x <= larger)
intersectOrNot = true;
else
intersectOrNot = false;
}
}
return intersectOrNot;
}

  
public static void main(String[] args) {
     
   String ch;
   Scanner sc=new Scanner(System.in);
     
   double x1,y1,x2,y2,x3,y3,x4,y4;
   do
   {
       System.out.println("Input: ");
x1=sc.nextDouble();
y1=sc.nextDouble();
x2=sc.nextDouble();
y2=sc.nextDouble();
x3=sc.nextDouble();
y3=sc.nextDouble();
x4=sc.nextDouble();
y4=sc.nextDouble();
Intersection checker1 = new Intersection(x1,y1,x2,y2,x3,y3,x4,y4 );
boolean hi1 = checker1.intersectOrNot();
       if(hi1)
       {
           System.out.println("Output:1");
       }
       else
       {
           System.out.println("Output:0");
       }
System.out.println("Do you want to test again? Y");

ch=sc.next();
}while(ch.equals("Y"));
   System.out.println("thank you");
}

}

====================================================================================

Output:

Input:
7 8 5 12 10 10 3 10
Output:1
Do you want to test again? Y
N
thank you

====================================================================================