← Java from Scratch Lesson 11 of 88 · 10 XP

Math

Theory and problem statement

Machine translation, not yet reviewed by a native speaker.

Math.max(5, 10)          // 10
Math.min(5, 10)          // 5
Math.abs(-4.7)           // 4.7
Math.sqrt(64)            // 8.0
Math.pow(2, 10)          // 1024.0 — a double
Math.round(2.5)          // 3 — to the nearest, a half goes UP (in C# it would be 2)
Math.floor(2.9)          // 2.0
Math.ceil(2.1)           // 3.0
Math.PI                  // 3.141592653589793
Math.random()            // a random 0.0 ≤ x < 1.0
Math.floorMod(-7, 3)     // 2 — the remainder "as in maths", unlike -7 % 3 == -1

A random integer from 1 to 6: (int) (Math.random() * 6) + 1. For repeatability in tests — new Random(seed).

Task. Read the coordinates of two points x1 y1 x2 y2 (integers). Print the distance between them with two decimal places (%.2f) and, on the second line, its value rounded with Math.round.

Sign up for free to solve this task right in your browser: code editor, running, test checks and hints.