CS101 · Chapter 4 · Annotated SlidesMathematical Functions, Characters, and Strings
Original
Overview

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.

How to read this page

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.

All 42 slides
  1. 1 Mathematical Functions, Characters, and Strings
  2. 2 Motivations
  3. 3 Objectives
  4. 4 Mathematical Functions
  5. 5 Math
  6. 6 Trigonometric Methods
  7. 7 Exponent Methods
  8. 8 Rounding Methods
  9. 9 Rounding Methods Examples
  10. 10 min, max, and abs
  11. 11 The random Method
  12. 12 Case Study: Computing Angles of a Triangle
  13. 13 Character Data Type
  14. 14 Unicode Format
  15. 15 ASCII Code for Commonly Used Characters
  16. 16 Escape Sequences for Special Characters
  17. 17 Appendix B: ASCII Character Set
  18. 18 ASCII Character Set, cont.
  19. 19 Casting between char and Numeric Types
  20. 20 Comparing and Testing Characters
  21. 21 Methods in the Character Class
  22. 22 The String Type
  23. 23 Simple Methods for String Objects
  24. 24 Simple Methods for String Objects
  25. 25 Getting String Length
  26. 26 Getting Characters from a String
  27. 27 Converting Strings
  28. 28 String Concatenation
  29. 29 Reading a String from the Console
  30. 30 Reading a Character from the Console
  31. 31 Comparing Strings
  32. 32 Obtaining Substrings
  33. 33 Finding a Character or a Substring in a String
  34. 34 Finding a Character or a Substring in a String
  35. 35 Conversion between Strings and Numbers
  36. 36 Problem: Guessing Birthday
  37. 37 Mathematics Basis for the Game
  38. 38 Case Study: Converting a Hexadecimal Digit to a Decimal Value
  39. 39 Revising the
  40. 40 Formatting Output
  41. 41 Frequently-Used Specifiers
  42. 42 FormatDemo
Slide 1 / 42

Mathematical Functions, Characters, and Strings

Slide 1: Mathematical Functions, Characters, and Strings

Notes

  1. 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).

Slide 2 / 42

Motivations

Slide 2: Motivations

Notes

  1. Motivation: compute triangle angles, work with characters and text.

Slide 3 / 42

Objectives

Slide 3: Objectives

Notes

  1. Objectives.

Slide 4 / 42

Mathematical Functions

Slide 4: Mathematical Functions
Slide 5 / 42

Math

Slide 5: Math
Slide 6 / 42

Trigonometric Methods

Slide 6: Trigonometric Methods
Slide 7 / 42

Exponent Methods

Slide 7: Exponent Methods

Exam angle

PredictionQ1

Math.pow(2, 3) returns:

Slide 8 / 42

Rounding Methods

Slide 8: Rounding Methods

Notes

  1. 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).

Slide 9 / 42

Rounding Methods Examples

Slide 9: Rounding Methods Examples

Exam angle

PredictionQ1

Math.round(-2.5) returns:

PredictionQ2

Math.ceil(4.1) returns:

Slide 10 / 42

min, max, and abs

Slide 10: min, max, and abs
Slide 11 / 42

The random Method

Slide 11: The random Method

Exam angle

PredictionQ1

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

Compare with the model answer

10 + (int)(Math.random() * 40)

Slide 12 / 42

Case Study: Computing Angles of a Triangle

Slide 12: Case Study: Computing Angles of a Triangle

Notes

  1. Case study: angles of a triangle with Math.acos and the law of cosines.

Slide 13 / 42

Character Data Type

Slide 13: Character Data Type
Slide 14 / 42

Unicode Format

Slide 14: Unicode Format
Slide 15 / 42

ASCII Code for Commonly Used Characters

Slide 15: ASCII Code for Commonly Used Characters

Notes

  1. ASCII: '0'–'9' = 48–57, 'A'–'Z' = 65–90, 'a'–'z' = 97–122.

Exam angle

PredictionQ1

The ASCII/Unicode value of 'a' is:

Slide 16 / 42

Escape Sequences for Special Characters

Slide 16: Escape Sequences for Special Characters
Slide 17 / 42

Appendix B: ASCII Character Set

Slide 17: Appendix B: ASCII Character Set

Notes

  1. ASCII table.

Slide 18 / 42

ASCII Character Set, cont.

Slide 18: ASCII Character Set, cont.

Notes

  1. ASCII table continued.

Slide 19 / 42

Casting between char and Numeric Types

Slide 19: Casting between char and Numeric Types

Exam angle

PredictionQ1

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

Slide 20 / 42

Comparing and Testing Characters

Slide 20: Comparing and Testing Characters

Notes

  1. Characters compare by their codes: 'a' < 'b' is true.

Slide 21 / 42

Methods in the Character Class

Slide 21: Methods in the Character Class
Slide 22 / 42

The String Type

Slide 22: The String Type
Slide 23 / 42

Simple Methods for String Objects

Slide 23: Simple Methods for String Objects
Slide 24 / 42

Simple Methods for String Objects

Slide 24: Simple Methods for String Objects
Slide 25 / 42

Getting String Length

Slide 25: Getting String Length

Notes

  1. length() returns the number of characters.

Slide 26 / 42

Getting Characters from a String

Slide 26: Getting Characters from a String

Notes

  1. charAt(index): indexes start at 0; the last is length() − 1.

  2. Trap s.charAt(s.length()) throws StringIndexOutOfBoundsException.

Exam angle

PredictionQ1

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

Slide 27 / 42

Converting Strings

Slide 27: Converting Strings

Notes

  1. toUpperCase, toLowerCase, trim (removes leading/trailing whitespace).

Slide 28 / 42

String Concatenation

Slide 28: String Concatenation

Exam angle

PredictionQ1

What is "1" + 2 + 3?

Slide 29 / 42

Reading a String from the Console

Slide 29: Reading a String from the Console

Notes

  1. next() reads a word; nextLine() reads the whole line.

Slide 30 / 42

Reading a Character from the Console

Slide 30: Reading a Character from the Console
Slide 31 / 42

Comparing Strings

Slide 31: Comparing Strings

Notes

  1. Trap == compares references, not contents.

Exam angle

PredictionQ1

Which correctly checks that two Strings have the same contents?

Slide 32 / 42

Obtaining Substrings

Slide 32: Obtaining Substrings

Notes

  1. substring(begin) and substring(begin, end), end exclusive.

Exam angle

PredictionQ1

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

Slide 33 / 42

Finding a Character or a Substring in a String

Slide 33: Finding a Character or a Substring in a String

Notes

  1. indexOf(ch), indexOf(s), lastIndexOf(...) return −1 if not found.

Slide 34 / 42

Finding a Character or a Substring in a String

Slide 34: Finding a Character or a Substring in a String

Notes

  1. More indexOf examples.

Slide 35 / 42

Conversion between Strings and Numbers

Slide 35: Conversion between Strings and Numbers
Slide 36 / 42

Problem: Guessing Birthday

Slide 36: Problem: Guessing Birthday

Notes

  1. Guessing a birthday with five sets.

Slide 37 / 42

Mathematics Basis for the Game

Slide 37: Mathematics Basis for the Game

Notes

  1. The math behind it: binary digits.

Slide 38 / 42

Case Study: Converting a Hexadecimal Digit to a Decimal Value

Slide 38: Case Study: Converting a Hexadecimal Digit to a Decimal Value
Slide 39 / 42

Revising the

Slide 39: Revising the
Slide 40 / 42

Formatting Output

Slide 40: Formatting Output
Slide 41 / 42

Frequently-Used Specifiers

Slide 41: Frequently-Used Specifiers

Notes

  1. Specifiers: %b boolean, %c char, %d int, %f float/double, %e scientific, %s string; width and precision: %8.2f.

Exam angle

PredictionQ1

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

Slide 42 / 42

FormatDemo

Slide 42: FormatDemo