Math.pow(2, 3) returns:
- pow returns a double.
The Math class (trigonometric, exponent, rounding, min/max/abs, random), the char type, Unicode and ASCII, escape sequences, casting between char and numbers, the Character methods, String methods (length, charAt, concatenation, comparison, substring, indexOf), conversions, and printf formatting.
Every original slide is shown unchanged. Yellow marks on a slide point to a numbered note: hover or tap a mark to jump to its note, and hover a note to see its marks. Questions tagged Past exam appeared on a real paper; Prediction questions are likely angles, not confirmed. Pick an answer to each question: a wrong pick tells you why it is wrong, so you can try again.
Tip Rounding: Math.ceil(2.1) = 3.0 · Math.floor(2.9) = 2.0 · Math.rint(2.5) = 2.0 (nearest even) · Math.round(2.5) = 3 (returns int/long).
Motivation: compute triangle angles, work with characters and text.
Objectives.
Math.pow(2, 3) returns:
Tip Rounding: Math.ceil(2.1) = 3.0 · Math.floor(2.9) = 2.0 · Math.rint(2.5) = 2.0 (nearest even) · Math.round(2.5) = 3 (returns int/long).
Math.round(-2.5) returns:
Math.ceil(4.1) returns:
Write an expression for a random int between 10 and 49 inclusive.
10 + (int)(Math.random() * 40)
Case study: angles of a triangle with Math.acos and the law of cosines.
ASCII: '0'–'9' = 48–57, 'A'–'Z' = 65–90, 'a'–'z' = 97–122.
The ASCII/Unicode value of 'a' is:
ASCII table.
ASCII table continued.
What does System.out.printlnchar)('a' + 2; print?
Characters compare by their codes: 'a' < 'b' is true.
length() returns the number of characters.
charAt(index): indexes start at 0; the last is length() − 1.
Trap s.charAt(s.length()) throws StringIndexOutOfBoundsException.
For s = "Welcome", s.charAt(3) is:
toUpperCase, toLowerCase, trim (removes leading/trailing whitespace).
What is "1" + 2 + 3?
next() reads a word; nextLine() reads the whole line.
Trap == compares references, not contents.
Which correctly checks that two Strings have the same contents?
substring(begin) and substring(begin, end), end exclusive.
"Welcome to Java".substring(0, 7) returns:
indexOf(ch), indexOf(s), lastIndexOf(...) return −1 if not found.
More indexOf examples.
Guessing a birthday with five sets.
The math behind it: binary digits.
Specifiers: %b boolean, %c char, %d int, %f float/double, %e scientific, %s string; width and precision: %8.2f.
System.out.printf("%.2f", 3.14159) prints: