From 30c3ba8239b8c48365dda855d4bac3225d1a7698 Mon Sep 17 00:00:00 2001 From: school Date: Fri, 28 Sep 2018 03:50:32 -0500 Subject: [PATCH] Intersect method works --- Ship.java | 11 +++++------ ShipTest.java | 37 +++++++++++++++++++++++++++++++++++-- 2 files changed, 40 insertions(+), 8 deletions(-) diff --git a/Ship.java b/Ship.java index 16cf9c8..934e10a 100644 --- a/Ship.java +++ b/Ship.java @@ -50,7 +50,7 @@ public class Ship{ int cord1; int cord2; // - if (other.isVertical()) { + if (other.vertical) { cord1 = X; cord2 = Y; } @@ -65,20 +65,19 @@ public class Ship{ int o_s1 = other.start[cord1]; int o_e1 = other.end[cord1]; - int t_s2 = t_s2; + int t_s2 = this.start[cord2]; int t_e2 = this.end[cord2]; int o_s2 = other.start[cord2]; int o_e2 = other.end[cord2]; // lines are parallel - if (this.vertical == other.isVertical()) { - + if (this.vertical == other.vertical) { // overlaps cond1 = t_s2 == o_s2; // intersects - cond2 = o_e1 <= t_s1; - cond3 = t_e1 <= o_s1; + cond2 = o_e1 >= t_s1; + cond3 = t_e1 >= o_s1; return cond1 && (cond2 || cond3); } diff --git a/ShipTest.java b/ShipTest.java index 2ee1c23..38a6fc4 100644 --- a/ShipTest.java +++ b/ShipTest.java @@ -1,6 +1,39 @@ +import java.util.Scanner; public class ShipTest{ - public testShips(Ship ship1, Ship ship2){ + public static void testShips(Ship ship1, Ship ship2,String name){ + System.out.println("Testing " + name); + ship1.print(); + System.out.println(); + ship2.print(); System.out.println(ship1.intersects(ship2)+"=="+ship2.intersects(ship1)); } public static void main(String[] args){ - Ship test_intersect0 = new Ship(new int[]{ \ No newline at end of file + int[] ship1start = new int[2]; + int[] ship1end = new int[2]; + + int[] ship2start = new int[2]; + int[] ship2end = new int[2]; + String name; + while (true) { + Scanner input = new Scanner(System.in); + System.out.print("Enter test name: "); + name = input.nextLine(); + + System.out.print("Enter ship1 start: "); + ship1start[0] = input.nextInt(); + ship1start[1] = input.nextInt(); + System.out.print("Enter ship1 end: "); + ship1end[0] = input.nextInt(); + ship1end[1] = input.nextInt(); + + System.out.print("Enter ship2 start: "); + ship2start[0] = input.nextInt(); + ship2start[1] = input.nextInt(); + System.out.print("Enter ship2 end: "); + ship2end[0] = input.nextInt(); + ship2end[1] = input.nextInt(); + + testShips(new Ship(ship1start,ship1end),new Ship(ship2start,ship2end),name); + } + } +} \ No newline at end of file