- Motivation: compute triangle angles, work with characters and text.
- Objectives.
- Java provides common math functions in the Math class.
- Math class: constants PI and E; methods are static (Math.sqrt(4)).
- Trigonometric methods use radians: sin, cos, tan, asin, toRadians, toDegrees.
- Exponent methods: exp, log (natural), log10, pow, sqrt.
🎯 How this can come up · 1 question
Math.pow(2, 3) returns:
pow returns a double.
- Rounding: ceil, floor, rint, round.
- Rounding examples.
🎯 How this can come up · 2 questions
Math.round(-2.5) returns:
round(x) = floor(x + 0.5).
Math.ceil(4.1) returns:
Correct: 5.0.
- min, max, abs.
- Math.random() returns a double in [0.0, 1.0). a + Math.random() * b gives [a, a + b).
🎯 How this can come up · 1 question
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.
- char holds one character: char letter = 'A';
- Java uses Unicode (16-bit): \u0041 = A.
- ASCII: '0'–'9' = 48–57, 'A'–'Z' = 65–90, 'a'–'z' = 97–122.
🎯 How this can come up · 1 question
The ASCII/Unicode value of 'a' is:
Correct: 97.
- Escape sequences: \b \t \n \f \r \\ \" .
- ASCII table.
- ASCII table continued.
- char ↔ int: (int)'A' = 65, (char)97 = 'a'; 'a' + 1 = 98.
🎯 How this can come up · 1 question
What does System.out.println((char)('a' + 2)); print?
Correct: c.
- Characters compare by their codes: 'a' < 'b' is true.
- Character methods: isDigit, isLetter, isLetterOrDigit, isLowerCase, isUpperCase, toLowerCase, toUpperCase.
- String is a reference type (a class), not primitive.
- String methods: length(), charAt(i), concat(s), toUpperCase(), toLowerCase(), trim().
- Instance methods are called on a string object: message.length().
- length() returns the number of characters.
- charAt(index): indexes start at 0; the last is length() − 1.
🎯 How this can come up · 1 question
For s = "Welcome", s.charAt(3) is:
W0 e1 l2 c3.
- toUpperCase, toLowerCase, trim (removes leading/trailing whitespace).
- + concatenates: "Chapter" + 2 = "Chapter2"; s1.concat(s2).
🎯 How this can come up · 1 question
What is "1" + 2 + 3?
Left to right: "1" + 2 = "12", then "123".
- next() reads a word; nextLine() reads the whole line.
- Read a char: input.nextLine().charAt(0).
- Compare strings with equals / equalsIgnoreCase / compareTo, never ==.
🎯 How this can come up · 1 question
Which correctly checks that two Strings have the same contents?
Correct: s1.equals(s2).
- substring(begin) and substring(begin, end), end exclusive.
🎯 How this can come up · 1 question
"Welcome to Java".substring(0, 7) returns:
Correct: "Welcome".
- indexOf(ch), indexOf(s), lastIndexOf(...) return −1 if not found.
- More indexOf examples.
- Integer.parseInt("12"), Double.parseDouble("1.5"); number to String: "" + number.
- Guessing a birthday with five sets.
- The math behind it: binary digits.
- Hex digit to decimal: 'A'–'F' → 10–15.
- Revised lottery with strings.
- printf formats output: System.out.printf("%.2f", x);
- Specifiers: %b boolean, %c char, %d int, %f float/double, %e scientific, %s string; width and precision: %8.2f.
🎯 How this can come up · 1 question
System.out.printf("%.2f", 3.14159) prints:
Correct: 3.14.
- FormatDemo: a formatted table.
No slides match this filter.