CS101 · Annotated SlidesChapter 4 — Mathematical Functions, Characters, and Strings
Chapter 4 · Annotated slides

Mathematical Functions, Characters, and Strings

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.

Slide text Study notes Highlight: key words Tips, tricks & traps Seen again elsewhere How it can come up

Mathematical Functions, Characters, and Strings

CS101 · Chapter 4
1
TipRounding: 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).
Slide 2: Motivations
Exam weight Low
  1. Motivation: compute triangle angles, work with characters and text.
Slide 3: Objectives
Exam weight Low
  1. Objectives.
Slide 4: Mathematical Functions
Exam weight Low
  1. Java provides common math functions in the Math class.
Slide 5: Math
Exam weight Low
  1. Math class: constants PI and E; methods are static (Math.sqrt(4)).
Slide 6: Trigonometric Methods
Exam weight Low
  1. Trigonometric methods use radians: sin, cos, tan, asin, toRadians, toDegrees.
Slide 7: Exponent Methods
Exam weight Medium
  1. Exponent methods: exp, log (natural), log10, pow, sqrt.
🎯 How this can come up · 1 question
MCQ

Math.pow(2, 3) returns:

Slide 8: Rounding Methods
Exam weight Low
  1. Rounding: ceil, floor, rint, round.
TipRounding: 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).
Slide 9: Rounding Methods Examples
Exam weight High
  1. Rounding examples.
🎯 How this can come up · 2 questions
MCQ

Math.round(-2.5) returns:

MCQ

Math.ceil(4.1) returns:

Slide 10: min, max, and abs
Exam weight Low
  1. min, max, abs.
Slide 11: The random Method
Exam weight High
  1. 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
Written

Write an expression for a random int between 10 and 49 inclusive.

Slide 12: Case Study: Computing Angles of a Triangle
Exam weight Low
  1. Case study: angles of a triangle with Math.acos and the law of cosines.
Slide 13: Character Data Type
Exam weight Low
  1. char holds one character: char letter = 'A';
Slide 14: Unicode Format
Exam weight Low
  1. Java uses Unicode (16-bit): \u0041 = A.
Slide 15: ASCII Code for Commonly Used Characters
Exam weight High
  1. ASCII: '0'–'9' = 48–57, 'A'–'Z' = 65–90, 'a'–'z' = 97–122.
🎯 How this can come up · 1 question
MCQ

The ASCII/Unicode value of 'a' is:

Slide 16: Escape Sequences for Special Characters
Exam weight Low
  1. Escape sequences: \b \t \n \f \r \\ \" .
Slide 17: Appendix B: ASCII Character Set
Exam weight Low
  1. ASCII table.
Slide 18: ASCII Character Set, cont.
Exam weight Low
  1. ASCII table continued.
Slide 19: Casting between char and Numeric Types
Exam weight High
  1. char ↔ int: (int)'A' = 65, (char)97 = 'a'; 'a' + 1 = 98.
🎯 How this can come up · 1 question
MCQ

What does System.out.println((char)('a' + 2)); print?

Slide 20: Comparing and Testing Characters
Exam weight Low
  1. Characters compare by their codes: 'a' < 'b' is true.
Slide 21: Methods in the Character Class
Exam weight Low
  1. Character methods: isDigit, isLetter, isLetterOrDigit, isLowerCase, isUpperCase, toLowerCase, toUpperCase.
Slide 22: The String Type
Exam weight Low
  1. String is a reference type (a class), not primitive.
Slide 23: Simple Methods for String Objects
Exam weight Low
  1. String methods: length(), charAt(i), concat(s), toUpperCase(), toLowerCase(), trim().
Slide 24: Simple Methods for String Objects
Exam weight Low
  1. Instance methods are called on a string object: message.length().
Slide 25: Getting String Length
Exam weight Low
  1. length() returns the number of characters.
Slide 26: Getting Characters from a String
Exam weight High
  1. charAt(index): indexes start at 0; the last is length() − 1.
Exam traps.charAt(s.length()) throws StringIndexOutOfBoundsException.
🎯 How this can come up · 1 question
MCQ

For s = "Welcome", s.charAt(3) is:

Slide 27: Converting Strings
Exam weight Low
  1. toUpperCase, toLowerCase, trim (removes leading/trailing whitespace).
Slide 28: String Concatenation
Exam weight High
  1. + concatenates: "Chapter" + 2 = "Chapter2"; s1.concat(s2).
🎯 How this can come up · 1 question
MCQ

What is "1" + 2 + 3?

Slide 29: Reading a String from the Console
Exam weight Low
  1. next() reads a word; nextLine() reads the whole line.
Slide 30: Reading a Character from the Console
Exam weight Low
  1. Read a char: input.nextLine().charAt(0).
Slide 31: Comparing Strings
Exam weight High
  1. Compare strings with equals / equalsIgnoreCase / compareTo, never ==.
Exam trap== compares references, not contents.
🎯 How this can come up · 1 question
MCQ

Which correctly checks that two Strings have the same contents?

Slide 32: Obtaining Substrings
Exam weight High
  1. substring(begin) and substring(begin, end), end exclusive.
🎯 How this can come up · 1 question
MCQ

"Welcome to Java".substring(0, 7) returns:

Slide 33: Finding a Character or a Substring in a String
Exam weight Low
  1. indexOf(ch), indexOf(s), lastIndexOf(...) return −1 if not found.
Slide 34: Finding a Character or a Substring in a String
Exam weight Low
  1. More indexOf examples.
Slide 35: Conversion between Strings and Numbers
Exam weight Low
  1. Integer.parseInt("12"), Double.parseDouble("1.5"); number to String: "" + number.
Slide 36: Problem: Guessing Birthday
Exam weight Low
  1. Guessing a birthday with five sets.
Slide 37: Mathematics Basis for the Game
Exam weight Low
  1. The math behind it: binary digits.
Slide 38: Case Study: Converting a Hexadecimal Digit to a Decimal Value
Exam weight Low
  1. Hex digit to decimal: 'A'–'F' → 10–15.
Slide 39: Revising the
Exam weight Low
  1. Revised lottery with strings.
Slide 40: Formatting Output
Exam weight Low
  1. printf formats output: System.out.printf("%.2f", x);
Slide 41: Frequently-Used Specifiers
Exam weight High
  1. 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
MCQ

System.out.printf("%.2f", 3.14159) prints:

Slide 42: FormatDemo
Exam weight Low
  1. FormatDemo: a formatted table.