0% found this document useful (0 votes)
19 views4 pages

Unit 1 Lesson 6 Casting Variables Lesson Plan

Uploaded by

shuguang liao
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views4 pages

Unit 1 Lesson 6 Casting Variables Lesson Plan

Uploaded by

shuguang liao
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Lesson Plan

Lesson 1.6: Casting Variables

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.

Customized Link to Share:


https://runestone.academy/ns/books/published/yourCustomCourseName/Unit1-
Getting-Started/topic-1-6-casting.html

Student Learning Objectives


ENDURING UNDERSTANDING

CON-1 The way variables and operators are sequenced and combined in an expression
determines the computed result.

LEARNING OBJECTIVE ESSENTIAL KNOWLEDGE

CON-1.C Evaluate ➢ CON-1.C.1 The casting operators (int) and (double)


arithmetexpressions that use can be used to create a temporary value converted to
casting. a different data type.
➢ CON-1.C.2 Casting a double value to an int causes
the digits to the right of the decimal point to be
truncated.
➢ CON-1.C.3 Some programming code causes int
values to be automatically cast (widened) to double
values.
➢ CON-1.C.4 Values of type double can be rounded to
the nearest integer by (int)(x + 0.5) or
(int)(x - 0.5) for negative numbers.
➢ CON-1.C.5 Integer values in Java are represented by
values of type int, which are stored using a finite

1
Lesson Plan
Lesson 1.6: Casting Variables

amount (4 bytes) of memory. Therefore, an int value


must be int the range from Integer.MIN_VALUE
to Integer.MAX_VALUE inclusive.
➢ CON-1.C.6 If an expression would evaluate to an int
value outside of the allowed range, an integer
overflow occurs. This could result in an incorrect value
within the allowed range.

Materials and Resources


● Presentation system (LCD projector/Interactive whiteboard)
● 1.6 Casting Variables on the CSAwesome e-book
● Casting Variables WarmUp and Answer Key
● Casting and Constants (optional YouTube Video, 0:00-5:20)
● Kahoot- AP CS Awesome! Type Casting - exit ticket
● CS Awesome! Programming Solutions
● AP Classroom Personal Progress Check Part B for Unit 1 (could be done after
summary and exercises sections).

Learning Activities

Estimated Length: 45 min

● Hook/Motivation (5 mins):

○ Think-Pair-Share; Look for a Pattern:


■ Have students complete the middle two columns of the Casting Variables
WarmUp.
■ Give a hardcopy of the WarmUp to each student, or project the WarmUp
table information.
■ Ask students to record answers on paper, or on-screen if projecting with
an interactive board.

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

○ Student Response Systems: Kahoot- AP CS Awesome! Type Casting


○ Ask students to review the Summary section on the Runestone site

Check for Understanding


● Interactive Exercises: 1-6-1 to 1-6-3
● Coding Exercises: Programming Challenge

3
Lesson Plan
Lesson 1.6: Casting Variables

● Student Response Systems: Kahoot- AP CS Awesome! Type Casting

● 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: More Practice


● Mathbits.com provides some information about integer division at the Math
Operators link.
● Practice-It has numerous online practice problems

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

You might also like