Loading...
Loading...
A food delivery shop sits at point (sx, sy) on a city grid. Given the delivery addresses, print the nearest and the farthest by straight-line distance (the first in input order on a tie).
Write a class Point with fields name, x and y and methods long squaredDistanceTo(Point other) and double distanceTo(Point other). The shop is a Point too.
NEAREST name d and FARTHEST name d, with d printed with two digits after the decimal point.Example
Input
0 0 3 Hostel 3 4 Office -6 8 Park 1 1
Output
NEAREST Park 1.41 FARTHEST Office 10.00
Distances are 5, 10 and about 1.41.
Try the problem first. Open one hint at a time.
The distance is √(dx² + dy²); comparing the squared values gives the same order with exact long maths.
A method can take another object as its parameter: p.squaredDistanceTo(shop).
Start nearest at null and take the first point when it is still null.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor