JAVA Programming Implement the equals method of the Arrow class. Two arrows are
ID: 3921557 • Letter: J
Question
JAVA Programming
Implement the equals method of the Arrow class. Two arrows are equal when they have the same starting point and direction.
------(Only can work at "Work here"
Arrow.JAVA
public class Arrow
{
private int x;
private int y;
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)
{
this.x = x;
this.y = 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 position and direction.
*/
public boolean equals(Object otherObject)
{
"Work Here"
}
}
------
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");
}
}
Explanation / Answer
Hi,
I have implemented equals method and highlighted the code changes beow.
Arrow.java
public class Arrow
{
private int x;
private int y;
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)
{
this.x = x;
this.y = 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 position and direction.
*/
public boolean equals(Object otherObject)
{
Arrow a = (Arrow)otherObject;
if(x == a.x && y == a.y && direction.equals(a.direction)){
return true;
}
else{
return false;
}
}
}
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");
}
}
Output:
true
Expected: true
false
Expected: false
false
Expected: false
false
Expected: false
true
Expected: true
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.