Question
Implement the equals method of the Arrow class. Two arrows are equal when they have the same starting point and direction. Complete the following file:
Implement the equals method of the Arrow class. Two arrows are equal when they have the same starting point and direction.
Complete the following file:
Use the following code/ Please only modify and add code:
public class Arrow { private Point start; private String direction;
/** Constructs an arrow. @param x the x-position @param y the y-position @param direction a compass direction N E S W NE NW SE SW */ public Arrow(int x, int y, String direction) { start = new Point(); start.move(x, y); this.direction = direction; }
/** Checks whether this arrow is equal to another. @param otherObject another arrow @return true if this arrow and otherObject have the same start and direction. */ public boolean equals(Object otherObject) { . . . } }
Refer to the following files as reference:
ArrowTester.java
public class ArrowTester { public static void main(String[] args) { Arrow arrow1 = new Arrow(1, 2, "SE"); Object arrow2 = new Arrow(1, 2, "W"); Arrow arrow3 = new Arrow(1, 0, "SE"); Arrow arrow4 = new Arrow(0, 2, "SE"); Arrow arrow5 = new Arrow(1, 2, "SEE".substring(0, 2)); System.out.println(arrow1.equals(arrow1)); System.out.println("Expected: true"); System.out.println(arrow1.equals(arrow2)); System.out.println("Expected: false"); System.out.println(arrow1.equals(arrow3)); System.out.println("Expected: false"); System.out.println(arrow1.equals(arrow4)); System.out.println("Expected: false"); System.out.println(arrow1.equals(arrow5)); System.out.println("Expected: true"); } }
Point.java
public class Point { private int x; private int y; /** Constructs a point at the origin. */ public Point() { this.x = 0; this.y = 0; } /** Moves this point by a given amount. @param dx the x-distance @param dy the y-distance */ public void move(int dx, int dy) { x = x + dx; y = y + dy; } /** Checks whether this point is equal to another. @param otherObject another point @return true if this point and otherObject have the same position. */ public boolean equals(Object otherObject) { Point other = (Point) otherObject; return x == other.x && y == other.y; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started