- Goal: write programs that compute with input data (area of a circle, loan payments…).
- Objectives: variables, constants, types, operators, expressions, input, casting.
- ComputeArea: radius → area = radius * radius * 3.14159.
- Trace: memory for radius and area is allocated.
- Trace: radius = 20.
- Trace: area computed.
- Trace: output printed.
- Trace: done.
- Identifiers: letters, digits, _ and $; cannot start with a digit; not a reserved word; any length; case-sensitive.
🎯 How this can come up · 2 questions
Which is a valid Java identifier?
Correct: $total2.
Java identifiers are case-sensitive.
True.
- A variable stores a value of a declared type.
- Declaring: int x; double radius; char a;
- Assignment: variable = expression; (x = x + 1 is legal).
- Declare and initialize in one step: int x = 1;
- Named constant: final double PI = 3.14159; cannot be changed.
🎯 How this can come up · 1 question
Which keyword declares a constant in Java?
Correct: final.
- Conventions: variables and methods camelCase (radius, computeArea); classes Capitalized (ComputeArea).
- Constants in ALL_CAPS (PI, MAX_VALUE).
- The six numeric types and their ranges.
🎯 How this can come up · 1 question
How many bits does an int use?
Correct: 32.
- Operators: + - * / %.
- Integer division truncates: 5 / 2 = 2, but 5.0 / 2 = 2.5.
🎯 How this can come up · 1 question
What is 7 / 2 in Java when both are int?
Correct: 3.
- % gives the remainder: 7 % 3 = 1; useful for even/odd and day-of-week problems.
🎯 How this can come up · 2 questions
What is 20 % 6?
Correct: 2.
Today is Saturday. What day is it 10 days later? Use %.
(6 + 10) % 7 = 2 → Tuesday (with Sunday = 0).
- Math.pow(a, b) computes aᵇ (returns double).
- A literal is a constant value written in the program.
- Integer literals are int by default; append L for long (2147483648L).
- Floating-point literals are double by default; append f for float (100.2f).
🎯 How this can come up · 1 question
The literal 3.14 is of type float by default.
False. Decimal literals are double.
- double is more precise (about 16 digits) than float (about 7).
- Scientific notation: 1.23456e2 = 123.456; 1.23456e-2 = 0.0123456.
- Writing arithmetic expressions in Java: watch the parentheses.
- Evaluation: parentheses first, then * / %, then + −, left to right.
🎯 How this can come up · 1 question
What is 3 + 4 * 4 + 5 * (4 + 3) - 1?
3 + 16 + 35 − 1.
- Mixed types: the narrower operand is widened (int + double → double).
- Widening rules: byte → short → int → long → float → double.
- Casting narrows explicitly: (int)3.9 = 3 (truncates). Widening is automatic.
🎯 How this can come up · 1 question
What is (int) 7.9?
Correct: 7.
- Augmented assignment: +=, -=, *=, /=, %=.
- x += 4.5 for an int x is legal: it means x = (int)(x + 4.5).
🎯 How this can come up · 1 question
int sum = 0; sum += 4.5; compiles.
True. Augmented assignment includes an implicit cast.
- Scanner input = new Scanner(System.in); then input.nextDouble(), nextInt()…
- Methods: nextByte, nextShort, nextInt, nextLong, nextFloat, nextDouble.
- ComputeAreaWithConsoleInput example.
- Example output.
- Compute the average of three numbers.
- Fahrenheit to Celsius: celsius = (5.0 / 9) * (fahrenheit − 32). Using 5 / 9 gives 0 (integer division).
🎯 How this can come up · 1 question
Why does (5 / 9) * (f − 32) always give 0 in Java?
Correct: 5 / 9 is integer division and equals 0.
- ++ and --: prefix (++i) changes then uses; postfix (i++) uses then changes.
- Examples of prefix and postfix.
🎯 How this can come up · 1 question
int i = 10; int j = i++ + 5; What are i and j?
Correct: i = 11, j = 15.
- Avoid using ++/-- more than once in one expression.
- An assignment is also an expression: i = j = k = 1;
- Practice: predict the output.
- Practice: predict the output.
- Two decimals: (int)(tax * 100) / 100.0.
- Common errors and pitfalls.
- Error 1: using a variable before declaring or initializing it.
- Error 2: integer overflow: a value too big for the type wraps around (int max 2147483647).
🎯 How this can come up · 1 question
Adding 1 to Integer.MAX_VALUE causes a runtime error in Java.
False. It silently overflows to the minimum value.
- Error 3: round-off errors: floating-point numbers are approximations (1.0 − 0.9 ≠ 0.1 exactly).
- Error 4: unintended integer division (average of ints).
- Pitfall: create ONE Scanner and reuse it.
No slides match this filter.