CS101 · Annotated SlidesChapter 2 — Elementary Programming
Chapter 2 · Annotated slides

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

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

Elementary Programming

CS101 · Chapter 2
1
TipNumeric 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: Motivations
Exam weight Low
  1. Goal: write programs that compute with input data (area of a circle, loan payments…).
Slide 3: Objectives
Exam weight Low
  1. Objectives: variables, constants, types, operators, expressions, input, casting.
Slide 4: Introducing Programming with an Example
Exam weight Low
  1. ComputeArea: radius → area = radius * radius * 3.14159.
Slide 5: animation
Exam weight Low
  1. Trace: memory for radius and area is allocated.
Slide 6: animation
Exam weight Low
  1. Trace: radius = 20.
Slide 7: animation
Exam weight Low
  1. Trace: area computed.
Slide 8: animation
Exam weight Low
  1. Trace: output printed.
Slide 9: animation
Exam weight Low
  1. Trace: done.
Slide 10: Identifiers
Exam weight High
  1. Identifiers: letters, digits, _ and $; cannot start with a digit; not a reserved word; any length; case-sensitive.
🎯 How this can come up · 2 questions
MCQ

Which is a valid Java identifier?

True / False

Java identifiers are case-sensitive.

Slide 11: Variables
Exam weight Low
  1. A variable stores a value of a declared type.
Slide 12: Declaring Variables
Exam weight Low
  1. Declaring: int x; double radius; char a;
Slide 13: Assignment Statements
Exam weight Low
  1. Assignment: variable = expression; (x = x + 1 is legal).
Slide 14: Declaring and Initializing in One Step
Exam weight Low
  1. Declare and initialize in one step: int x = 1;
Slide 15: Named Constants
Exam weight High
  1. Named constant: final double PI = 3.14159; cannot be changed.
🎯 How this can come up · 1 question
MCQ

Which keyword declares a constant in Java?

Slide 16: Naming Conventions
Exam weight Low
  1. Conventions: variables and methods camelCase (radius, computeArea); classes Capitalized (ComputeArea).
Slide 17: Naming Conventions, cont.
Exam weight Low
  1. Constants in ALL_CAPS (PI, MAX_VALUE).
Slide 18: Numerical Data Types
Exam weight High
  1. The six numeric types and their ranges.
TipNumeric types: byte 8-bit · short 16 · int 32 · long 64 · float 32 · double 64. Default: whole literals are int, decimal literals are double.
🎯 How this can come up · 1 question
MCQ

How many bits does an int use?

Slide 19: Numeric Operators
Exam weight Low
  1. Operators: + - * / %.
Slide 20: Integer Division
Exam weight High
  1. Integer division truncates: 5 / 2 = 2, but 5.0 / 2 = 2.5.
Exam trapIf both operands are int, / drops the fraction.
🎯 How this can come up · 1 question
MCQ

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

Slide 21: Remainder Operator
Exam weight High
  1. % gives the remainder: 7 % 3 = 1; useful for even/odd and day-of-week problems.
🎯 How this can come up · 2 questions
MCQ

What is 20 % 6?

Written

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

Slide 22: Exponent Operations
Exam weight Low
  1. Math.pow(a, b) computes aᵇ (returns double).
Slide 23: Number Literals
Exam weight Low
  1. A literal is a constant value written in the program.
Slide 24: Integer Literals
Exam weight Low
  1. Integer literals are int by default; append L for long (2147483648L).
Slide 25: Floating-Point Literals
Exam weight Medium
  1. Floating-point literals are double by default; append f for float (100.2f).
🎯 How this can come up · 1 question
True / False

The literal 3.14 is of type float by default.

Slide 26: double vs. float
Exam weight Low
  1. double is more precise (about 16 digits) than float (about 7).
Slide 27: Scientific Notation
Exam weight Low
  1. Scientific notation: 1.23456e2 = 123.456; 1.23456e-2 = 0.0123456.
Slide 28: Arithmetic Expressions
Exam weight Low
  1. Writing arithmetic expressions in Java: watch the parentheses.
Slide 29: How to Evaluate an Expression
Exam weight High
  1. Evaluation: parentheses first, then * / %, then + −, left to right.
🎯 How this can come up · 1 question
MCQ

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

Slide 30: Numeric Type Conversion
Exam weight Low
  1. Mixed types: the narrower operand is widened (int + double → double).
Slide 31: Conversion Rules
Exam weight Low
  1. Widening rules: byte → short → int → long → float → double.
Slide 32: Type Casting
Exam weight High
  1. Casting narrows explicitly: (int)3.9 = 3 (truncates). Widening is automatic.
🎯 How this can come up · 1 question
MCQ

What is (int) 7.9?

Slide 33: Augmented Assignment Operators
Exam weight Low
  1. Augmented assignment: +=, -=, *=, /=, %=.
Slide 34: Casting in an Augmented Expression
Exam weight Medium
  1. x += 4.5 for an int x is legal: it means x = (int)(x + 4.5).
🎯 How this can come up · 1 question
True / False

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

Slide 35: Reading Input from the Console
Exam weight Low
  1. Scanner input = new Scanner(System.in); then input.nextDouble(), nextInt()…
Slide 36: Reading Numbers from the Keyboard
Exam weight Low
  1. Methods: nextByte, nextShort, nextInt, nextLong, nextFloat, nextDouble.
Slide 37: ComputeAreaWithConsoleInput
Exam weight Low
  1. ComputeAreaWithConsoleInput example.
Slide 38: Slide 38
Exam weight Low
  1. Example output.
Slide 39: Compute Average
Exam weight Low
  1. Compute the average of three numbers.
Slide 40: Problem: Converting Temperatures
Exam weight High
  1. Fahrenheit to Celsius: celsius = (5.0 / 9) * (fahrenheit − 32). Using 5 / 9 gives 0 (integer division).
🎯 How this can come up · 1 question
MCQ

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

Slide 41: Increment and Decrement Operators
Exam weight Low
  1. ++ and --: prefix (++i) changes then uses; postfix (i++) uses then changes.
Slide 42: Increment and Decrement Operators, cont.
Exam weight High
  1. Examples of prefix and postfix.
🎯 How this can come up · 1 question
MCQ

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

Slide 43: Increment and Decrement Operators, cont.
Exam weight Low
  1. Avoid using ++/-- more than once in one expression.
Slide 44: Assignment Expressions and Assignment Statements
Exam weight Low
  1. An assignment is also an expression: i = j = k = 1;
Slide 45: What is the output of the following code:
Exam weight Low
  1. Practice: predict the output.
Slide 46: What is the output of the following code:
Exam weight Low
  1. Practice: predict the output.
Slide 47: Problem: Keeping Two Digits After Decimal Points
Exam weight Low
  1. Two decimals: (int)(tax * 100) / 100.0.
Slide 48: Common Errors and Pitfalls
Exam weight Low
  1. Common errors and pitfalls.
Slide 49: Common Error 1: Undeclared/Uninitialized Variables and Unused Variables
Exam weight Low
  1. Error 1: using a variable before declaring or initializing it.
Slide 50: Common Error 2: Integer Overflow
Exam weight Medium
  1. Error 2: integer overflow: a value too big for the type wraps around (int max 2147483647).
🎯 How this can come up · 1 question
True / False

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

Slide 51: Common Error 3: Round-off Errors
Exam weight Low
  1. Error 3: round-off errors: floating-point numbers are approximations (1.0 − 0.9 ≠ 0.1 exactly).
Slide 52: Common Error 4: Unintended Integer Division
Exam weight Low
  1. Error 4: unintended integer division (average of ints).
Slide 53: Common Pitfall 1: Redundant Input Objects
Exam weight Low
  1. Pitfall: create ONE Scanner and reuse it.