JAVA Programming Implement the equals method of the Arrow cl

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

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

Solution

Hi,

I have implemented the method equals() and highlighted the code changes below.

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)
{
   Arrow a = (Arrow)otherObject;
if(start.equals(a.start) && direction.equals(a.direction)){
   return true;
}
else{
   return false;
}
}

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

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

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site