Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Trying to figure out how to write this part of the code, I got everything else,

ID: 3761108 • Letter: T

Question

Trying to figure out how to write this part of the code, I got everything else, but the part (I think). My code is underneather.

public boolean equals(Object obj)

Determines whether or not two points are equal. Two instances of Point2D are equal if the values of their x and y member fields, representing their position in the coordinate space, are the same.

Overrides:

equals in class Object

Parameters:

obj - an object to be compared with this Point2D

Returns:

true if the object to be compared is an instance of Point2D and has the same values; false otherwise.

This is what I have

public final class Point2D {
final int x;
final int y;
  
/** Create a new 2D point with given x and y values
*
* @param xValue is the value for the x-coordinate
* @param yValue is the value for the y-coordinate
*/
public Point2D(int xValue, int yValue) {
x = xValue;
y = yValue;
}
  
/** Return a string describing the 2D point */
public String toString() {
return "(" + x + ", " + y + ")";
}
  
  
/** Override the default hashcode method */
public int hashCode(){
int xValue = ((Double) x).hashCode();
int yValue = ((Double) y).hashCode();
return 31*hashX + hashY;
// compute hash code that uses both x and y values
   //I am not positive this part above is 100percent correct.

}
  
/** Override the default equals method.
* Two Point2D objects equal if their values are equal.
*/
public boolean equals(Object obj) {
if (obj instanceof Point2D == false){
return false;
}
Point2D p = (Point2D) obj;
if (){
return true;
}
// TODO: return true if this point and the given object describe the same 2D point
// (i.e. x and y values are the same between the two point objects)
  
return true;
}
  
}

Explanation / Answer

public final class Point2D {

final int x;

final int y;

  

/** Create a new 2D point with given x and y values

*

* @param xValue is the value for the x-coordinate

* @param yValue is the value for the y-coordinate

*/

public Point2D(int xValue, int yValue) {

x = xValue;

y = yValue;

}

  

/** Return a string describing the 2D point */

public String toString() {

return "(" + x + ", " + y + ")";

}

  

  

/** Override the default hashcode method */

public int hashCode(){

return 31*x + y;

// compute hash code that uses both x and y values

//I am not positive this part above is 100percent correct.

}

  

/** Override the default equals method.

* Two Point2D objects equal if their values are equal.

*/

public boolean equals(Object obj) {

if (!(obj instanceof Point2D)){

return false;

}

Point2D p = (Point2D) obj;

if (this == p){

return true;

}

// TODO: return true if this point and the given object describe the same 2D point

// (i.e. x and y values are the same between the two point objects)

  

return ((this.x == p.x) && (this.y == p.y));

}

  

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote