# AP® Computer Science A 1.11 Math Class

Source: https://lockincs.com/ap-csa/unit-1/1-11-math-class/  
Last reviewed against the College Board Course and Exam Description (CED): 2026-11-12  
CED codes: 1.11.A, 1.11.A.1, 1.11.A.2, 1.11.A.3

The Math class is in the java.lang package, available by default, and contains only class methods. The Java Quick Reference lists five: abs(int), abs(double), pow, sqrt and random. Math.random() returns a double from 0.0 (inclusive) up to 1.0 (exclusive), and arithmetic and casting turn it into a random value in any range.

## What you will learn

- 1.11.A Develop code to write expressions that incorporate calls to built-in mathematical libraries and determine the value that is produced as a result.

## 1\. The Math class

The `Math` class is part of the `java.lang` package. Classes in the `java.lang` package are available by default, so you need no import 1.11.A.1. The `Math` class contains only class methods 1.11.A.2, so you call each one with the class name: `Math.sqrt(2.25)`.

## 2\. The five Math methods on the Java Quick Reference

These methods, what they do and when to use them are part of the Java Quick Reference 1.11.A.2:

| Method | Returns | Example |
| --- | --- | --- |
| `static int abs(int x)` | the absolute value of an `int` | `Math.abs(-4)` is `4` |
| `static double abs(double x)` | the absolute value of a `double` | `Math.abs(-4.5)` is `4.5` |
| `static double pow(double base, double exponent)` | `base` raised to the power `exponent` | `Math.pow(2, 10)` is `1024.0` |
| `static double sqrt(double x)` | the non-negative square root of `x` | `Math.sqrt(2.25)` is `1.5` |
| `static double random()` | a `double` from 0.0 (included) up to 1.0 (not included) | at least 0.0 and less than 1.0 |

```
System.out.println(Math.abs(-4));
System.out.println(Math.abs(-4.5));
System.out.println(Math.pow(2, 10));
System.out.println(Math.sqrt(2.25));
double side = Math.sqrt(49);
System.out.println("Side: " + side);
```

Output:

```
4
4.5
1024.0
1.5
Side: 7.0
```

`Math.pow(2, 10)` has `int` arguments, but the parameters are `double`, so the arguments are widened and the result is a `double`.

**Watch out:** `pow` and `sqrt` return a `double`, even with `int` arguments, so `Math.pow(2, 3)` prints `8.0`, not `8`, and `int p = Math.pow(2, 3);` does not compile without a cast:

```
// does not compile
int p = Math.pow(2, 3);
```

Real view:

```
error: incompatible types: possible lossy conversion from double to int
```

`int p = (int) Math.pow(2, 3);` compiles and stores 8.

## 3\. Random numbers in a range

The values returned from `Math.random()` can be changed with arithmetic and casting operators to produce a random `int` or `double` in a defined range. Each endpoint of the range can be **inclusive**, meaning the value can occur, or **exclusive**, meaning it cannot 1.11.A.3.

Building a die roll, `int die = (int) (Math.random() * 6) + 1;`, step by step:

| Step | Expression | Possible values | What it does |
| --- | --- | --- | --- |
| 1 | `Math.random()` | 0.0 up to 1.0 | 1.0 is never returned |
| 2 | `... * 6` | 0.0 up to 6.0 | stretches to 6 values |
| 3 | `(int) (...)` | 0, 1, 2, 3, 4, 5 | drops the decimal part |
| 4 | `... + 1` | 1, 2, 3, 4, 5, 6 | shifts: the lowest is 1 |

Pattern: `(int) (Math.random() * (max - min + 1)) + min` gives an `int` from `min` to `max`, both inclusive 1.11.A.3.

You cannot predict a random value, but you can test the two extremes. Here `r` stands in for the smallest and a very large value that `Math.random()` could return:

```
double r = 0.0;                  // smallest possible value
System.out.println((int) (r * 6) + 1);
r = 0.9999999;                   // just below 1.0
System.out.println((int) (r * 6) + 1);
System.out.println((int) r * 6 + 1);   // cast too early
```

Output:

```
1
6
1
```

The last line shows the trap: `(int) r` is 0 for every value below 1.0, so the result is always 1.

A random `double` in a range works the same way, without the cast. `Math.random() * 5 + 10` gives a `double` from 10.0 (inclusive) up to 15.0 (exclusive):

```
double r = 0.0;
System.out.println(r * 5 + 10);
r = 0.5;
System.out.println(r * 5 + 10);
```

Output:

```
10.0
12.5
```

In a real program, write `Math.random()` in place of `r`, for example `int card = (int) (Math.random() * 13) + 1;` for a card value from 1 to 13.

## Common mix-ups

- Not true: Math.pow(2, 3) gives the int 8. pow and sqrt always return a double, even with int arguments. Math.pow(2, 3) is 8.0, and int p = Math.pow(2, 3); does not compile without a cast.
- Not true: Math.random() can return 1.0. Math.random() returns a value greater than or equal to 0.0 and less than 1.0. The 1.0 endpoint is exclusive.
- Not true: (int) Math.random() * 6 + 1 simulates a die. The cast applies to Math.random() alone, which is always 0 after truncation, so this is always 1. Write (int) (Math.random() * 6) + 1.
- Not true: You need to create a Math object first. Math contains only class methods. Call them with the class name: Math.sqrt(2.25).

## In 30 seconds

- Math is in java.lang (no import needed) and has only class methods.
- abs(int) returns an int; abs(double), pow, sqrt and random return a double.
- Math.random() returns a double from 0.0 (inclusive) up to 1.0 (exclusive).
- (int) (Math.random() * (max - min + 1)) + min gives an int from min to max, both inclusive.
- An endpoint is inclusive if it can occur and exclusive if it cannot.

## Checkpoint: 4 practice questions

### Question 1

What is printed?

```java
System.out.println(Math.abs(-7)
                   + Math.sqrt(16)
                   + Math.pow(2, 3));
```

- A. `19`
- B. `19.0`
- C. `5.0`
- D. `748.0`

**Answer: B.** 7 + 4.0 + 8.0: `sqrt` and `pow` return doubles, so the sum is the double 19.0. A forgets that the result is a double. C subtracts the absolute value instead of adding it. D writes the parts 7, 4 and 8 side by side as if + glued them together. No String is involved, so + adds numbers (and gluing text would give 74.08.0 anyway).

### Question 2

Which expression produces a random int from 10 to 20, inclusive?

- A. `(int) (Math.random() * 10) + 10`
- B. `(int) Math.random() * 11 + 10`
- C. `(int) (Math.random() * 11) + 10`
- D. `(int) (Math.random() * 20) + 10`

**Answer: C.** `Math.random() * 11` gives 0.0 up to, but not including, 11.0; the cast gives 0 to 10; plus 10 gives 10 to 20. A gives 10 to 19. B is always 10, because the cast applies to `Math.random()` alone. D gives 10 to 29.

### Question 3

Consider the following code segment. What is printed?

```java
double a = Math.abs(-2.5);
double b = Math.pow(a, 2);
System.out.println(Math.sqrt(b));
```

- A. `2.5`
- B. `6.25`
- C. `-2.5`
- D. `2`

**Answer: A.** `a` is 2.5, `b` is 6.25, and the non-negative square root of 6.25 is 2.5. B prints `b` instead of its square root. C forgets that `abs` and `sqrt` never give negative results here. D treats the double result as an int.

### Question 4

A program needs a random double that is at least 5.0 and less than 8.0. Which expression produces it?

- A. `Math.random() * 8 + 5`
- B. `(int) (Math.random() * 3) + 5`
- C. `Math.random() * 5 + 3`
- D. `Math.random() * 3 + 5`

**Answer: D.** `Math.random() * 3` gives 0.0 up to 3.0 (exclusive), and adding 5 shifts it to 5.0 up to 8.0. A gives 5.0 up to 13.0. B gives only the ints 5, 6 and 7. C gives 3.0 up to 8.0.

## Questions students ask

**What Math methods do I need for AP® Computer Science A?**

Five, all on the Java Quick Reference: abs(int x), abs(double x), pow(double base, double exponent), sqrt(double x) and random().

**How do you get a random integer in a range in Java?**

Use (int) (Math.random() * (max - min + 1)) + min. For a die, (int) (Math.random() * 6) + 1 gives an int from 1 to 6, both inclusive.

**Does Math.random() include 1.0?**

No. Math.random() returns a double greater than or equal to 0.0 and less than 1.0. 0.0 is inclusive and 1.0 is exclusive.

**Do I need to import the Math class?**

No. Math is in the java.lang package, and classes in java.lang are available by default.

---

© LockInCS, licensed CC BY-NC-ND 4.0. AP® is a trademark registered by the College Board, which is not affiliated with, and does not endorse, this website.
