Unit 1 Lesson 6 Casting Variables Lesson Plan
Unit 1 Lesson 6 Casting Variables Lesson Plan
Overview
This lesson introduces the programming concept of type casting and corresponds to
College Board AP CSA topic 1.5. By completing this lesson, students learn how to cast a
value or variable to act as an integer or double. Students are given an opportunity to
round values to the nearest integer and explore integer overflow.
Note: If you wish to provide a URL/link for students to access this lesson, you will need
to replace “yourCustomCourseName” in the customized link below with the name of
your custom course that you created. For instructions on how to set up a custom
course, please see https://www.csawesome.org/runestone-e-book.
CON-1 The way variables and operators are sequenced and combined in an expression
determines the computed result.
1
Lesson Plan
Lesson 1.6: Casting Variables
Learning Activities
● Hook/Motivation (5 mins):
2
Lesson Plan
Lesson 1.6: Casting Variables
■ Confirm they have the correct mixed numbers and decimals, then tell
them the Java Outputs for the last column (or show them the output by
running a Java program).
■ Have them think and record their answers to the two questions, then
discuss with a partner, then share with the class.
■ Guide the students to the conclusion that all decimal values were
truncated.
■ Explain that this occurred because Java performs integer division when
both the numerator and denominator of a division operation are
integers.
● Experiences and Explorations (30 mins):
○ Guided Practice: Using the Runestone site:
■ Have students read the 1.6 Casting Variables introduction, run
ActiveCode 1, and read the paragraph after about type casting.
■ Allow students to Paraphrase the content by sharing multiple ways to
modify the WarmUp output statements in order to obtain the decimal
equivalent values as output.
■ Demonstrate how values of type double can be rounded to the nearest
integer by (int)(x + 0.5) or (int)(x - 0.5) for negative numbers
■ Read and discover about Integer.MAX_VALUE, Integer.MIN_VALUE, and
integer overflow using ActiveCode 2. A suggestion to help your students
remember the max and min values is to memorize in chunks as a 10-digit
phone number.
○ Independent Practice: Ask students to run ActiveCode 3, then answer questions
1-6-1, 1-6-2, and 1-6-3. Ask if there are any questions and discuss, if necessary.
○ Pair Programming: Allow students to work in pairs to complete the
Programming Challenge. Remind them to enter their code on the blank line
following each commented line of pseudocode and to “switch drivers” after
every line of code. There is an extra challenge of using type-casting to char to
print out UNICODE Chinese characters. This project can be open-ended
exploration that may engage diverse students.
● Rethink, Reflect, and/or Revise (10 mins):
3
Lesson Plan
Lesson 1.6: Casting Variables
● Common Misconceptions:
○ Students have a difficult time noticing the difference between
( (double) (7 / 2) ) and ( (double) 7 / 2 ). A strategy to help this would be to have
students highlight or underline the numerator and denominator with different
colors. In the first case, only the 7 and 2 would be highlighted, separately. In the
second case, the entire expression (double) 7 would be highlighted as the
numerator.
● Personal Progress Check (College Board): Go to AP Classroom to assign the Personal
Progress Check Part B for Unit 1 which covers AP topics 1.4-1.5 (in CSAwesome lessons
1.5 and 1.6). Review the results in class to identify and address any student
misunderstandings.
Progress Check Part B covers (12 Questions):
● 1.4 Compound Assignment Operators; Questions 1 – 6
● 1.5 Casting and Ranges of Variables; Questions 7 – 1
Differentiation: Enrichment
● Type conversion information from Geeks for Geeks - refer students to this site
for more advanced discussion about data types, widening versus narrowing type
conversion, and automatic versus explicit type conversion
● Have students do the extra challenge on type casting unicode characters at the
end of the challenge section. They can look up characters in 3 different
languages.
● Have students think about a banking penny-shaving scheme, where interest
payments are rounded down. How much would the bank or a hacker make by
shaving pennies if they kept a half a penny per 1000 transactions in 1 month?
How could this be prevented by type casting? (Thanks to Betsy Dillard of CREC
Academy for this activity).