Introduction to Type Conversions through Casting
Overview
In this lesson, students will learn how to manipulate data types through casting.
Learning Objectives
- 1.1.C.3 Use casting in expressions when necessary to obtain the correct type.
Skills
- S1.B Design algorithms for a program.
- S1.D Explain the impact design has on data storage.
- S2.A Write program code and implement algorithms.
- S2.C Analyze an algorithm and program code for correctness.
Student Outcomes
Students will be able to:
- use casting to temporarily change the type of a value
Duration: 1 class period
Resources
Warm-up / Motivate
In the Arithmetic Expressions and Assignment lesson plan, we learned that there are two different types of division.
- How can we force the program to use the type of division we need to solve a problem?
Learn
Either as a group or on their own, have students complete tutorial: Learn: Introduction to Type Conversions with Casting
Apply
Have students use the Java Playground to write code to calculate a test grade when 60% of the score comes from a multiple choice section with 25 questions and 40% comes from a free response question with 3 parts. Each question on the multiple choice section is worth the same amount and each part of the free response exam is worth the same amount. Provide them with the following starter code:
int mCQnumCorrect = 20;
double multipleChoicePart;
int fRQCorrect = 2;
double frqPart;
int score;
//add code to compute multipleChoicePart, frqPart, and score
//use proper type casting to get the correct answer of 74
Next Lesson
Lesson Plan: Introduction to Input and Output