Skip Top Navigation Bar

Introduction to Type Conversions through Casting

Overview

In this lesson, students will learn how to manipulate data types through casting.

Learning Objectives

Skills

Student Outcomes

Students will be able to:

Duration: 1 class period

Resources

Background

In the Arithmetic Expressions and Assignment lesson plan, we learned that there are two different types of division.

This means that division functions in two different ways:

There are often times when you have two int values or variables and want to compute division and have the result include the remainder. There are also times when one of the values is a double and we need the whole part of the division.

For example, maybe you are computing a student average. You have two int values: the sum of all the grades and the number of grades that you have. When we compute the average of the the two int values, it will truncate the result, essentially always rounding down. Consider the following:

    int sumGrades = 90 + 85 + 95 + 95; 
    int gradeCount = 4;
    int average = sumGrades / gradeCount; //average is assigned the value 91

Instead, you might want to have the real-number average. To do this, you can cast one or both of the operands to be a double. Casting Conversions converts the type of an expression to the type specified. It is done by placing the desired type in parenthesis prior to the expression. Consider the following:

    int sumGrades = 90 + 85 + 95 + 95; 
    int gradeCount = 4;
    double average = (double)sumGrades / gradeCount; //converts sumGrades to type double; average is assigned the value 91.25
    average = sumGrades / (double)gradeCount; //converts gradeCount to type double; average is assigned the value 91.25
    average = (double)sumGrades / (double)gradeCount; //converts both sumGrades and gradeCount to type double; average is assigned 91.25

When we type cast from an int to a double, this is considered a widening conversion since the int value fits into the double type without any loss of data.

When we type cast from a double to an int, this is considered a narrowing conversion since the double value might need to be truncated to fit into the int type thereby losing some data. For example:

    double average = 89.75;
    int truncatedAverage = (int)average; //truncatedAverage is assigned the value 89

Activity

Activity 1: Mini-Lab

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