CS101 · Chapter 2 · Annotated SlidesElementary Programming
Original
Overview

Elementary Programming

Variables, identifiers, constants, the numeric types and operators (integer division and %), literals and scientific notation, evaluation order, type conversion and casting, augmented assignment, ++/--, reading input with Scanner, and the common errors (overflow, round-off, unintended integer division).

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 53 slides
  1. 1 Elementary Programming
  2. 2 Motivations
  3. 3 Objectives
  4. 4 Introducing Programming with an Example
  5. 5 animation
  6. 6 animation
  7. 7 animation
  8. 8 animation
  9. 9 animation
  10. 10 Identifiers
  11. 11 Variables
  12. 12 Declaring Variables
  13. 13 Assignment Statements
  14. 14 Declaring and Initializing in One Step
  15. 15 Named Constants
  16. 16 Naming Conventions
  17. 17 Naming Conventions, cont.
  18. 18 Numerical Data Types
  19. 19 Numeric Operators
  20. 20 Integer Division
  21. 21 Remainder Operator
  22. 22 Exponent Operations
  23. 23 Number Literals
  24. 24 Integer Literals
  25. 25 Floating-Point Literals
  26. 26 double vs. float
  27. 27 Scientific Notation
  28. 28 Arithmetic Expressions
  29. 29 How to Evaluate an Expression
  30. 30 Numeric Type Conversion
  31. 31 Conversion Rules
  32. 32 Type Casting
  33. 33 Augmented Assignment Operators
  34. 34 Casting in an Augmented Expression
  35. 35 Reading Input from the Console
  36. 36 Reading Numbers from the Keyboard
  37. 37 ComputeAreaWithConsoleInput
  38. 38 Write a program that takes three numbers
  39. 39 Compute Average
  40. 40 Problem: Converting Temperatures
  41. 41 Increment and Decrement Operators
  42. 42 Increment and Decrement Operators, cont.
  43. 43 Increment and Decrement Operators, cont.
  44. 44 Assignment Expressions and Assignment Statements
  45. 45 What is the output of the following code:
  46. 46 What is the output of the following code:
  47. 47 Problem: Keeping Two Digits After Decimal Points
  48. 48 Common Errors and Pitfalls
  49. 49 Common Error 1: Undeclared/Uninitialized Variables and Unused Variables
  50. 50 Common Error 2: Integer Overflow
  51. 51 Common Error 3: Round-off Errors
  52. 52 Common Error 4: Unintended Integer Division
  53. 53 Common Pitfall 1: Redundant Input Objects
Slide 1 / 53

Elementary Programming

Slide 1: Elementary Programming

Notes

  1. Tip Numeric types: byte 8-bit · short 16 · int 32 · long 64 · float 32 · double 64. Default: whole literals are int, decimal literals are double.

Slide 2 / 53

Motivations

Slide 2: Motivations

Notes

  1. Goal: write programs that compute with input data (area of a circle, loan payments…).

Slide 3 / 53

Objectives

Slide 3: Objectives
Slide 4 / 53

Introducing Programming with an Example

Slide 4: Introducing Programming with an Example

Notes

  1. ComputeArea: radius → area = radius * radius * 3.14159.

Slide 5 / 53

animation

Slide 5: animation
Slide 6 / 53

animation

Slide 6: animation
Slide 7 / 53

animation

Slide 7: animation
Slide 8 / 53

animation

Slide 8: animation
Slide 9 / 53

animation

Slide 9: animation

Notes

  1. Trace: done.

Slide 10 / 53

Identifiers

Slide 10: Identifiers

Exam angle

PredictionQ1

Which is a valid Java identifier?

PredictionQ2

Java identifiers are case-sensitive.

Slide 11 / 53

Variables

Slide 11: Variables

Notes

  1. A variable stores a value of a declared type.

Slide 12 / 53

Declaring Variables

Slide 12: Declaring Variables
Slide 13 / 53

Assignment Statements

Slide 13: Assignment Statements

Notes

  1. Assignment: variable = expression; (x = x + 1 is legal).

Slide 14 / 53

Declaring and Initializing in One Step

Slide 14: Declaring and Initializing in One Step
Slide 15 / 53

Named Constants

Slide 15: Named Constants

Exam angle

PredictionQ1

Which keyword declares a constant in Java?

Slide 16 / 53

Naming Conventions

Slide 16: Naming Conventions
Slide 17 / 53

Naming Conventions, cont.

Slide 17: Naming Conventions, cont.
Slide 18 / 53

Numerical Data Types

Slide 18: Numerical Data Types

Notes

  1. The six numeric types and their ranges.

  2. Tip Numeric types: byte 8-bit · short 16 · int 32 · long 64 · float 32 · double 64. Default: whole literals are int, decimal literals are double.

Exam angle

PredictionQ1

How many bits does an int use?

Slide 19 / 53

Numeric Operators

Slide 19: Numeric Operators

Notes

  1. Operators: + - * / %.

Slide 20 / 53

Integer Division

Slide 20: Integer Division

Notes

  1. Trap If both operands are int, / drops the fraction.

Exam angle

PredictionQ1

What is 7 / 2 in Java when both are int?

Slide 21 / 53

Remainder Operator

Slide 21: Remainder Operator

Exam angle

PredictionQ1

What is 20 % 6?

PredictionQ2

Today is Saturday. What day is it 10 days later? Use %.

Compare with the model answer

(6 + 10) % 7 = 2 → Tuesday (with Sunday = 0).

Slide 22 / 53

Exponent Operations

Slide 22: Exponent Operations

Notes

  1. Math.pow(a, b) computes aᵇ (returns double).

Slide 23 / 53

Number Literals

Slide 23: Number Literals
Slide 24 / 53

Integer Literals

Slide 24: Integer Literals
Slide 25 / 53

Floating-Point Literals

Slide 25: Floating-Point Literals

Exam angle

PredictionQ1

The literal 3.14 is of type float by default.

Slide 26 / 53

double vs. float

Slide 26: double vs. float

Notes

  1. double is more precise (about 16 digits) than float (about 7).

Slide 27 / 53

Scientific Notation

Slide 27: Scientific Notation
Slide 28 / 53

Arithmetic Expressions

Slide 28: Arithmetic Expressions

Notes

  1. Writing arithmetic expressions in Java: watch the parentheses.

Slide 29 / 53

How to Evaluate an Expression

Slide 29: How to Evaluate an Expression

Notes

  1. Evaluation: parentheses first, then * / %, then + −, left to right.

Exam angle

PredictionQ1

What is 3 + 4 * 4 + 5 * (4 + 3) - 1?

Slide 30 / 53

Numeric Type Conversion

Slide 30: Numeric Type Conversion

Notes

  1. Mixed types: the narrower operand is widened (int + double → double).

Slide 31 / 53

Conversion Rules

Slide 31: Conversion Rules

Notes

  1. Widening rules: byte → short → int → long → float → double.

Slide 32 / 53

Type Casting

Slide 32: Type Casting

Exam angle

PredictionQ1

What is (int) 7.9?

Slide 33 / 53

Augmented Assignment Operators

Slide 33: Augmented Assignment Operators
Slide 34 / 53

Casting in an Augmented Expression

Slide 34: Casting in an Augmented Expression

Notes

  1. x += 4.5 for an int x is legal: it means x = (int)(x + 4.5).

Exam angle

PredictionQ1

int sum = 0; sum += 4.5; compiles.

Slide 35 / 53

Reading Input from the Console

Slide 35: Reading Input from the Console
Slide 36 / 53

Reading Numbers from the Keyboard

Slide 36: Reading Numbers from the Keyboard
Slide 37 / 53

ComputeAreaWithConsoleInput

Slide 37: ComputeAreaWithConsoleInput

Notes

  1. ComputeAreaWithConsoleInput example.

Slide 38 / 53

Write a program that takes three numbers

Slide 38: Write a program that takes three numbers

Notes

  1. Example output.

Slide 39 / 53

Compute Average

Slide 39: Compute Average
Slide 40 / 53

Problem: Converting Temperatures

Slide 40: Problem: Converting Temperatures

Notes

  1. Fahrenheit to Celsius: celsius = (5.0 / 9) * (fahrenheit − 32). Using 5 / 9 gives 0 (integer division).

Exam angle

PredictionQ1

Why does (5 / 9) * (f − 32) always give 0 in Java?

Slide 41 / 53

Increment and Decrement Operators

Slide 41: Increment and Decrement Operators

Notes

  1. ++ and --: prefix (++i) changes then uses; postfix (i++) uses then changes.

Slide 42 / 53

Increment and Decrement Operators, cont.

Slide 42: Increment and Decrement Operators, cont.

Notes

  1. Examples of prefix and postfix.

Exam angle

PredictionQ1

int i = 10; int j = i++ + 5; What are i and j?

Slide 43 / 53

Increment and Decrement Operators, cont.

Slide 43: Increment and Decrement Operators, cont.

Notes

  1. Avoid using ++/-- more than once in one expression.

Slide 44 / 53

Assignment Expressions and Assignment Statements

Slide 44: Assignment Expressions and Assignment Statements
Slide 45 / 53

What is the output of the following code:

Slide 45: What is the output of the following code:

Notes

  1. Practice: predict the output.

Slide 46 / 53

What is the output of the following code:

Slide 46: What is the output of the following code:

Notes

  1. Practice: predict the output.

Slide 47 / 53

Problem: Keeping Two Digits After Decimal Points

Slide 47: Problem: Keeping Two Digits After Decimal Points
Slide 48 / 53

Common Errors and Pitfalls

Slide 48: Common Errors and Pitfalls
Slide 49 / 53

Common Error 1: Undeclared/Uninitialized Variables and Unused Variables

Slide 49: Common Error 1: Undeclared/Uninitialized Variables and Unused Variables

Notes

  1. Error 1: using a variable before declaring or initializing it.

Slide 50 / 53

Common Error 2: Integer Overflow

Slide 50: Common Error 2: Integer Overflow

Notes

  1. Error 2: integer overflow: a value too big for the type wraps around (int max 2147483647).

Exam angle

PredictionQ1

Adding 1 to Integer.MAX_VALUE causes a runtime error in Java.

Slide 51 / 53

Common Error 3: Round-off Errors

Slide 51: Common Error 3: Round-off Errors

Notes

  1. Error 3: round-off errors: floating-point numbers are approximations (1.0 − 0.9 ≠ 0.1 exactly).

Slide 52 / 53

Common Error 4: Unintended Integer Division

Slide 52: Common Error 4: Unintended Integer Division
Slide 53 / 53

Common Pitfall 1: Redundant Input Objects

Slide 53: Common Pitfall 1: Redundant Input Objects

Notes

  1. Pitfall: create ONE Scanner and reuse it.