All puzzles
Per page
L
Math methods ยท Order of steps

Distance between two points

Return the distance from the point (x1, y1) to the point (x2, y2). You may use Math.sqrt().

distance(1, 2, 4, 6) returns 5.0.

8 strips: L1 to L8

return dist;
L1
int dy = y2 - y1;
L2
double dist = Math.sqrt(squares);
L3
double dist = Math.pow(squares, 2);
L4
int dx = x2 - x1;
L5
int squares = dx * dx + dy * dy;
L6
public static double distance(int x1, int y1, int x2, int y2) {
L7
}
L8

Answer key: L. Distance between two points

Order: L7, L5, L2, L6, L3, L1, L8. Extra: L4

Lines with the same mark can go in either order.

public static double distance(int x1, int y1, int x2, int y2) {
	int dx = x2 - x1;
	int dy = y2 - y1;
	int squares = dx * dx + dy * dy;
	double dist = Math.sqrt(squares);
	return dist;
}

Extra strip: double dist = Math.pow(squares, 2);. Math.pow(squares, 2) squares the number again. The distance is the square root, Math.sqrt(squares).