Casting, Overflow &
Code Shortcuts

casting · overflow · round-off · shortcuts

Task 1: Casting Test

Run the code and explain what changed.

Your Goal

1. Run the program exactly as shown.

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

3. Explain why the decimal part disappeared in both cases.

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 happened after the largest int increased by one.

3. Explain why the smallest int can still keep going instead of stopping with 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 to the decimal math you expected in your head.

3. Explain why double calculations can still look slightly off.

Example Code

Task 4: Money to Cents

Convert a decimal amount into cents safely.

Your Goal

1. Create a program that starts with the amount shown.

2. Convert that amount into cents and store it in an int.

3. Add a rounding step before the cast so the cents value stays accurate.

4. Print the original amount and the final cents value.

Example Code

Task 5: Scoreboard Rewrite

Replace the long updates without changing the result.

Your Goal

1. Rewrite the same program with shortcut 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 keeps the decimal part.

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 output to 1248.

2. Change the code so it actually prints 1248 in order.

3. Keep the structure simple and stay inside the shortcut-operator ideas from this lesson.

Example Code

Task 9: Safe to Add?

Decide what must be true before two ints are added.

Your Goal

1. Read a method that adds two int values and prints the sum.

2. Describe what must already be true so the addition does not overflow.

3. Explain why knowing each number is an int is not enough by itself.

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: Checkout Fix

Finish a checkout calculation without losing cents.

Your Goal

1. Create a final total by combining the subtotal and the tax rate.

2. Convert that final total into cents with an accurate rounding step.

3. Print the total in dollars and the final cents value.

Example Code

Task 12: Build the Output

Write a tiny shortcut-operator program from a target result.

Your Goal

1. Create a short program that starts from the value shown.

2. Use shortcut operators so the output becomes 2526.

3. Print the two numbers back to back in the correct order.

Example Code

Task 13: 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 14: Round to Nearest Int

Turn a decimal into the nearest int instead of truncating.

Your Goal

1. Create an expression that rounds the value shown to the nearest int.

2. Store the rounded result in an integer variable.

3. Print both the original decimal and the rounded integer.

Example Code

Task 15: 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 / 16