Determine a Data Type
Overview
In this lesson, students will begin to develop an understanding of the different types of data that are stored by the computer.
Learning Objectives
- 1.1.B.1 Determine the type of data that is being stored.
- 1.1.B.2 Distinguish between primitive data types and non-primitive data types (objects), including String.
Skills
- S1.C Explain the impact design has on data storage.
Student Outcomes
Students will be able to:
- determine the data that needs to be stored
- recognize the difference between primitive data and object data
- recognize how to group data together
Duration: 1 class period
Resources
Background
A computer takes in input (data), stores it, processes it, and then outputs the new data. It's all about data. That is the source and life of any program. Consider some of the following popular programs you might be using:
- Restaurant Rewards App: A restaurant rewards app takes in and processes data from the user such as: their location, their personal account information, a tracking of purchases for points. It will also take in and process data from the restaurant itself, such as: menu items, prices, hours of operation, location of restaurant, rewards that they are willing to offer to customers. It will produce output, such as: a receipt for order, time until the order is ready to be picked up, update of the points for the customer.
- 2048 Game: The game 2048 takes in data from the user in terms of which direction the tiles should slide. The data that is coming from the game would be the generation of the next tile, the combining of like tiles, and updating the score. The output would be an updated screen and score.
Let's also consider a program that would be used to Analyze Data. The input would be data from a database. The program would store this data in a list. The program would then process the data to answer a question someone might have about the data. The output would be the answer from this analysis.
Discussion
Ask students to think of a game that they play regularly, either on their phone or on the computer (they could also use the board game they played to generate an algorihtm). Have them pair up with another student that has the same game. Students may need to change their game of choice to a different one so that all students are in groups of 2 - 3. Set a timer for 2 minutes and have students individually list the data that the program uses to run. Now that they have a list of data, ask them to take 2 minutes to individually catalog where this data originates from. Is the data:
- input from the user?
- input from another source?
- preset in the program?
- generated during the play of the game?
Have students share their lists and classification, refining the list with their partner. Have the pairs create 1 slide with the name of the game and then four columns, one for each data source, listing the data in each column. Print these out and have students hang them up in the room.
Have students do a gallery walk with post-it notes to add comments related to data that might have been forgotten and needs to be added.
Primitive Data
Java divides data into primitive data and object data.
Primitive data is:
- numbers
- whether something is true or false, called boolean values
- characters
Numbers are broken into two categories:
- Integers (positive, negative, and zero) - There are four data types:
byte
, short
, int
, and long
.
- Real Numbers - There are two data types:
float
and double
.
Why are there all these data types to represent integers and real numbers?
Each of these data types have a different storage capacity. This can be important if you have storage concerns.
- byte: The
byte
data types is an 8-bit signed two's complement integer. It has a minimum value of -128 and a maximum value of 127(inclusive).
- short: The
short
data type is a 16-bit signed two's complement integer. It has a minimum value of -32,768 and a maximum value of 32,767(inclusive).
- int: The
int
data type is a 32-bit signed two's complement integer. It has a minimum value of -2 to the 31s power and a maximum value of 2 to the 31 power - 1. It can also be used to represent a larger range of unsigned values.
- long: The
long
data type is a 64-bit two's complement integer. It can also be used to be a signed or unsigned value range.
- float: The
float
data type is single-precision 32-bit floating point.
- double: The
double
data type is a double-precision 64-bit floating point.
- boolean: The
boolean
data type has only two values: true
and false
.
- char: The
char
data type is a single 16-bit Unicode character.
Primitive vs. Object Data
What if the data isn't a number, boolean, or character? How do we store it then? Data that doesn't fall into these categories are considered objects. Objects either represent things in the real-world or group related data together. They are represented in one of two ways:
Some examples of object data are:
String
: A prime example of object data would be words, sentences, paragraphs. These collections of individual characters is considered String
data and would be represented by the String
class.
- Dates: A calendar date, such as a birthday. There are ways to manipulate these dates, such as making it reoccuring year to year. We would use the
LocalDate
class to represent dates.
- Dice: Used in games to generate random values between 1 and 6 inclusively. You can create a
Dice
class to simulate the rolling of a dice.
- A Block: Such as the ones in 2048 that have color and a number on them. You can create a
Block
class to represent a colored block with a number on it that can be used as a visual representation in the game.
- Fruit data: From a spread sheet, it could include the name of the fruit, form it is being distributed in (fresh, canned, juice), retail price, unit of this price, etc. All of the data listed here are
String
or primitive data and can represented by creating a Fruit
record to store all the data together.
Activities
Activity 1: Cataloging data
Provide students with a list of different types of data. Have students classify the data as primitive or object. If it is primitive, have them determine which of the 8 primitive types should be used.
Some examples can be found in Practice - Classifying Data
Activity 2: Improve Game Data
Have students improve their lists of data for their games (from the discussion above) by adding the data types for each piece of data.
Activity 3: Flash Card Warm-up
Write descriptions of data on index cards. As students enter the classroom, provide each students with one of the cards. Have them write the data type for their description on the back of the card. Ask them to share with a student next to them and explain why they choose that data type. Does their partner agree or disagree? Is their another data type that might be suitable?