Unit 1 ยท Lab 2

Casting, Overflow & Code Shortcuts

Test how Java converts values, handles numeric limits, and updates variables.

๐Ÿงช Task 1: Casting Test

Run the code and explain what changed.

Your Goal

1. Run the program exactly as shown.

2. Describe what happened when each double became an int.

3. Compare automatic widening with the explicit (double) cast.

4. Explain why narrowing can lose data while widening does not.

Example Code

๐Ÿšง Task 2: Overflow Test

Push an int past the edge and explain the result.

Your Goal

1. Run the code exactly as shown.

2. Observe what happens after the largest int increases by one.

3. Explain why subtracting one from the smallest int produces another in-range value instead of an error.

Example Code

๐Ÿงฉ Task 3: Decimal Surprise

Run the decimals and explain the strange output.

Your Goal

1. Run the program exactly as written.

2. Compare the printed values with the decimal results you expected.

3. Explain why double calculations can contain small round-off errors.

Example Code

๐ŸŽฏ Task 4: Cast or Round?

Compare truncation with rounding for the same positive value.

Your Goal

1. Cast measurement directly to int and store the result.

2. Round the same positive value by adding 0.5 before casting.

3. Print the original value and both integer results.

4. Explain why casting and rounding produce different answers.

Example Code

โšก Task 5: Scoreboard Rewrite

Replace the long updates without changing the result.

Your Goal

1. Rewrite the same program with compound assignment operators.

2. Use ++ where it naturally fits.

3. Keep the starting value and final printed result exactly the same.

Example Code

โž— Task 6: Cast with Division

Compare casting before division and after division.

Your Goal

1. Run the code and compare the two printed values.

2. Decide which line casts before the division happens.

3. Explain why one result becomes 0.0 while the other remains 0.4.

Example Code

๐Ÿ”ฎ Task 7: Shortcut Prediction

Predict the result before you run it.

Your Goal

1. Predict the final printed value before you run the program.

2. Run the program and check whether your prediction was correct.

3. Translate each shortcut line back into long form to explain the result.

Example Code

๐ŸŽฏ Task 8: Target Output Fix

Change the program so it reaches the right output.

Your Goal

1. Run the program and compare its result with the target output.

2. Change the code so it matches the target exactly.

3. Keep the structure simple and use only the shortcut operators from this lesson.

Example Code
Example Output
1248

๐Ÿ›ก๏ธ Task 9: Safe to Add?

Decide what must be true before two ints are added.

Your Goal

1. Run the expression and observe the unexpected result.

2. Choose two replacement int values that can be added safely.

3. Describe what must be true about the sum before the addition happens.

4. Explain why two valid int values can still produce overflow.

Example Code

๐Ÿงญ Task 10: Parentheses Matter

Compare two similar lines and explain why they differ.

Your Goal

1. Run both lines and compare the printed values.

2. Decide which expression casts first and which expression adds first.

3. Explain how the parentheses changed the meaning of the code.

Example Code

๐Ÿ”ข Task 11: Build the Output

Work backward from two target values.

Your Goal

1. Start with the value shown.

2. Use one compound assignment operator to change val to 25, then print it.

3. Use ++ to change val to 26, then print it.

4. Match the target output without spaces or a line break between the two values.

Example Code
Example Output
2526

๐Ÿ“Š Task 12: Average Fix

Repair an average that loses its decimal part.

Your Goal

1. Run the program and decide whether the printed average is mathematically correct.

2. Fix the code so the average keeps its decimal value.

3. Explain why the original line lost information before the result reached the double variable.

Example Code

๐Ÿ“ Task 13: Round to Nearest Int

Round both positive and negative values instead of truncating.

Your Goal

1. Create one expression that rounds positiveDistance to the nearest int.

2. Create another expression that correctly rounds negativeDistance.

3. Store both rounded results in integer variables.

4. Print each original value beside its rounded result.

Example Code

โค๏ธ Task 14: Lives Trace

Trace a short chain of increment and decrement updates.

Your Goal

1. Predict the final printed value before you run the code.

2. Run the program and check your prediction.

3. Explain the value after each update in order.

Example Code
01 / 15