logo Practice-It logo

closerDistance

Language/Type: Java method basics return
Author: Roy McElmurry (on 2010/12/28)

Write a static method closerDistance that takes two pairs of integers, x1 and y1 and x2 and y2, representing two ordered pairs (x1, y1) and (x2, y2) on an x-y plane. Your method should calculate each pair?s the distance from the origin (0, 0) and return the closer of the two distances as a real number. If the two points are the same distance from the origin, you may return either of the two distances, since they are equal.

For example, the point (12, 5) has a distance of 13.0 from the origin, and the point (9, 9) has a distance of 12.727922061357855 from the origin, so a call to closerDistance(12, 5, 9, 9) would return 12.727922061357855. Notice your method should not do any rounding.

Recall that the formula to find the distance between a point (x, y) and the origin is equal to the square root of (x2 + y2).

Type your solution here:


This is a method problem. Write a Java method as described. Do not write a complete program or class; just the method(s) above.

You must log in before you can solve this problem.


Log In

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.