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.
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
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.