Implement the equals method of the Arrow class Two arrows ar

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 extends Point
{
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)
{
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)
{
Work Here
}
}

-----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;
}
}

-------Tester.java

public class ArrowTester
{
public static void main(String[] args)
{
Arrow arrow1 = new Arrow(1, 2, \"SE\");
Point p1 = arrow1; // Just checking that Arrow extends Point
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\");
}
}

Solution

Hi, Please find my implementation:

public class Arrow extends Point

{

   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)

   {

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)

   {

if( otherObject instanceof Arrow){

      // typecasting otherObject in Arrow

      Arrow p = (Arrow)otherObject;

      // if point ans direction is equal then return true

      if(super.equals(otherObject) && direction.equalsIgnoreCase(p.direction))

          return true;

}

  

return false; // if not equal then return false

   }

}

##############

public class ArrowTester

{

   public static void main(String[] args)

   {

Arrow arrow1 = new Arrow(1, 2, \"SE\");

Point p1 = arrow1; // Just checking that Arrow extends Point

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\");

   }

}

/*

Sampel Output:

true

Expected: true

false

Expected: false

false

Expected: false

false

Expected: false

true

Expected: true

*/

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 \
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 \
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 \
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 \

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site