Task 11 of 14 · 0 solved · 14 to go0%
You can read the problem, but answers are checked only for signed-in students. Sign in to answer →

1.4 Arithmetic — the Math class

Lesson

Ready-made calculations

Java ships with a class called Math holding calculations you would not want to write yourself. You call them on the class name, without creating anything: Math.abs(x), Math.pow(a, b), Math.sqrt(x), Math.max(a, b), Math.min(a, b).


Worked example

System.out.println(Math.abs(-7));      // 7   — distance from zero
System.out.println(Math.pow(2, 10));   // 1024.0
System.out.println(Math.sqrt(144));    // 12.0
System.out.println(Math.max(3, 9));    // 9

⚠️ Watch the decimal points: Math.pow and Math.sqrt always return a double, even when the answer is a whole number. Math.abs, Math.max and Math.min keep the type you gave them.


▶ Try it: run this, then change something and run it again.



Math.pow(3, 2) returns...


How do you call a Math method?


Type exactly what this program prints.

public class Main { public static void main(String[] a){
    System.out.println(Math.abs(-2.5));
    System.out.println(Math.sqrt(25));
}}

Math.abs(-7) returns an int because...


True or false?