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

How do I solve this? please provide reasoning so that it\'s easy for me to under

ID: 3846918 • Letter: H

Question

How do I solve this? please provide reasoning so that it's easy for me to understand.


(8 Po Given the Point class from the class examples, what additions would you make such that jects have a natural ordering and if sorted. Point objects closer to the origin point oo,o oy would come ob before Point objects further away. Make sure that you make a needed additions. Distance formula: y2 public class Point private final myX final double my public Point (final double thex a double the thex myY the public double getx return public double get Y return myY

Explanation / Answer

In the given class Point we can add another member field dist that gives the distance of the point object from the origin point (0.0, 0.0). Once we have this distance information then we can sort the points using this info. So the class definition becomes.

public class Point

{

private final double myX;

private final double myY;

private final double dist;

public Point(final double theX, final double theY)

{   

myX = theX;

myY = theY;

dist = sqrt(pow(myX,2)+pow(myY,2));

}

public double getX()

{

return myX;

}

public double getY()

{

return myY;

}

public double getDist()

{

return dist;

}

}

Now the points can be sorted by comparing their distances. Suppose P1, P2 are 2 point objects then the ordering can be determined by comapring

if (P1.getDist() < P2.getDist() )

then P1 comes before P2

else

P2 comes before P1

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