Swift Coding Club - Apple Inc. · another device like a robot or drone. If you have these devices,...
Transcript of Swift Coding Club - Apple Inc. · another device like a robot or drone. If you have these devices,...
Swift Coding Club Middle School Coding LessonsIntermediate
Welcome to the Everyone Can Code Club! These coding lessons cover fundamental coding concepts to go along with the app design lessons. With these lessons, you’ll not only build up your coding skills, but you’ll start to understand how apps work. This will help you design better apps.
In each lesson, you’ll learn about a specific coding concept with a brief introduction activity, then you’ll apply that coding concept to solve puzzles in Swift Playgrounds.
There are also “Pick and Choose” activities for each lesson to help you learn more about that concept. These additional activities are optional and you can choose to do none, one, or both activities. Note that some of these activities require another device like a robot or drone. If you have these devices, they’re a great way to apply what you’ve learned.
Have fun!
Lessons
Think Like a NewsBot: Variables 3
Think Like an Architect: Types 5
Think Specifically: Parameters 7
Think Organized: Arrays 9
2
Think Like a NewsBot: VariablesIntermediate
1
Variable
Introduction
Keeping Track
Bump Up the Value
Incrementing the Value
Seeking Seven Gems
Three Gems, Four Switches
Checking for Equal Values
Round Up the Switches
Collect the Total
Variable: A named container that stores a value. The value canchange over time.
Introduction (5 minutes) Imagine you’re moving to a new home. You’d probably put your belongings into boxes, labeling them with a word or two to describe the contents. For example, if a box contains trophies, you might label the box “Trophies.” This example suggests three important features of a container.
When we program a computer, we use something called variables instead of boxes. Variables are similar to boxes—they have both a label (a name) and contents (a value). The value or contents can change, but the label or name can’t. We can find a variable’s value or contents by finding the variable with the correct name or label.
Watch this video to learn more.
Practice (40 minutes) Now use Swift Playgrounds to complete the puzzles with the green checkmarks in the list at right.
Think about it: How did using variables help you with the app?
3
Newsbot (1 session) 1. For this activity, you’ll create NewsBot—a robot that can
automatically write a short article. First, think about a news or sporting event, and what pieces of information you might need to write about it.
2. Brainstorm four to six variables, like a team name, score, and event date. Write a two- to three-sentence story using the variables, which NewsBot can then fill in for other similar events.
3. Pair up. One of you provides the information for each variable to fill in the story. Then switch. Do you have two complete stories that make sense?
Think about it: Did any variable names work better than others? Why or why not?
Sphero Arcade (1 session) 1. If you have a Sphero SPRK+ robot available, download
the Sphero Arcade lesson in Swift Playgrounds.
2. Apply functions and variables to aim, detect collisions, and eventually build your own version of Pong.
3. On the Play Sphero Pong page, analyze the code before running or editing it. What does each command do?
4. What elements of the game did you change using variables?
Think about it: What other games could you customize in the way that you did with Pong?
Think Like a NewsBot: Variables Pick and Choose
Intermediate
1
4
Types
Introduction
Deactivating a Portal
Portal On and Off
Setting the Right Portal
Corners of the World
Random Gems Everywhere
Initialization
Introduction
Initializing Your Expert
Train Your Expert
Using Instances of Different Types
It Takes Two
Think Like an Architect: TypesIntermediate
2
Type: A named grouping of properties (the features) and methods (the behaviors) of a kind of data. Initialization: The act of creating a new instance of a type, which includes setting initial values for any properties of the type.
Introduction (5 minutes) How many different types of buildings can you think of? Pick one type. What makes it unique? In other words, what are some specific features of that particular type of building? What typically happens in it? We’ll call these behaviors. For example, a school has classrooms (features) and the bell rings between class periods (behavior). Does everyone agree on the properties and behaviors? Why or why not?
If we use a computer program to help us construct the building, we have to be very specific. We need to define its type by providing the properties (which we called features) and methods (what we called behaviors).
Watch this video to learn more.
Practice (40 minutes) Now use Swift Playgrounds to complete the puzzles with the green checkmarks in the list at right.
Think about it: What were the types in the app? What did you initialize?
5
Be an architect (1 session) 1. Choose a type of building, either real or imagined.
2. Think of five to six variables to describe what your type of building looks like. These variables shouldn’t contain any values; instead, they should be descriptive of values—like “height” or “numberOfWindows.”
3. Add values next to the variables. This describes a specific instance of the building type. In Swift terminology, you’re initializing an instance of the building type.
4. Finish the initialization by using a drawing app like Notes to sketch the building type, reflecting the variables and values.
5. Find a partner and trade the lists of your building types’ variables and values. Draw each other’s building types, then share your drawings. How similar are they?
Think about it: Can you think of ways to make the drawings look more alike?
Rock, Paper, Scissors (1 session) 1. Download the Rock, Paper, Scissors challenge in Swift
Playgrounds.
2. Explore the lesson. How does the code work? What coding concepts do you recognize?
3. Now try to create a version of the game in which you use your own rules and add new actions. To do this, you’ll need to define the game’s type and initialize it, along with the associated properties and methods.
Think about it: What were the properties and methods in the original game? What were the new properties and methods you added to personalize the game?
Think Like an Architect: Types Pick and Choose
Intermediate
2
6
Type: rocketHouse numberOfEngines = 4 height = 15 meters numberOfWindows = 8 color = Space Gray doorShape = Rounded rectangle
Think Specifically: ParametersIntermediate
3
Introduction (5 minutes) As a group, brainstorm examples of functions you do in everyday life—for example, “jump” or “make a sandwich.”
For each example, what kinds of questions—if any—might someone who’s been told to perform that function have? Jump. How high? Make a sandwich. What kind?
Functions sometimes require parameters. You can think of parameters as extra information added to a function to make it more specific. For example, in the command, “Say Hello,” the word “Say” is the function and “Hello” is the parameter.
Watch this video to learn more.
Practice (40 minutes) Now use Swift Playgrounds to complete the puzzles with the green checkmarks in the list at right.
Think about it: What parameters did you use in the app? Are there functions that don’t need parameters?
Parameter: Extra information that gets passed to a function.
Variable
Introduction
Moving Further Forward
Generalizing a Function
Crank Up and Down
Placing at a Specific Location
Rivers to Cross
Placing Two Characters
Two Experts
Twin Peaks
7
Siri says (1 session) 1. Each person writes 5 to 10 commands using parameters.
Be sure that the commands are doable—for example, “Dance the hokey pokey” is doable, but “Make a ham sandwich” probably isn’t.
2. As a group, decide who gets to be Siri. You can switch every few rounds.
3. Whoever is Siri decides when or when not to say, “Siri says,” before giving the group’s commands. Those who complete a command without hearing “Siri says” are out. Play until there’s a winner.
Think about it: How did adding parameters affect the commands? Was the game more or less interesting using parameters?
EV3 Animal Rescue (multiple sessions) 1. If you have LEGO® MINDSTORMS® Education EV3, try
EV3 Animal Rescue in Swift Playgrounds.
2. In this playground, you’ll design, engineer, and program a solution to help an injured turtle move and interact with its environment. You’ll modify and create different functions using parameters to control and improve your solution.
3. Share your solutions with the group and compare designs.
Think about it: What parameters did you use and how did that information help define the function? Who designed the fastest turtle?
Think Specifically: Parameters Pick and Choose
Intermediate
3
Siri says…
8
Think Organized: ArraysIntermediate
4
Introduction (5 minutes) Pretend your parents are letting you invite seven friends to go minigolfing. Your parents can pick everyone up, but you need to determine the order.
Write down and number this list of friends: Jacob, Alesha, Alberto, Sang, and Amir. But you forgot that Alesha is out of town. Erase Alesha and adjust the numbers so that Alberto becomes number 2 and so on. You’ve thought of two more people, Yumi and Danielle. They live closer to the minigolf course, so add them to the bottom of the list as numbers 5 and 6. Wait, you should also invite Yumi’s sister, Blair, so add her as number 6 after Yumi. Adjust the numbers so there are seven numbered names.
In coding, this is called an array—a collection that stores an ordered list of items. The index is the number that represents the position of an item in an array. So in the list, Jacob is indexed at number 1. You can “append” the list, like you did by adding Yumi and Danielle to the bottom. You can also “insert” at index, like you did with Blair at number 6, and “remove” at index, like you did with Alesha at number 2.
Watch this video to learn more.
Practice (40+ minutes) Now use Swift Playgrounds to complete the puzzles with the green checkmarks in the list at right.
Think about it: How do arrays relate to types, initialization, and variables?
Array: A collection that stores an ordered list of items. Index: The number that represents the position of an item in an array.
World Building
Introduction
Uniting Worlds
Connect and Solve
Making Your Own Portals
Reach for the Stairs
Floating Islands
Build a Loop
A Puzzle of Your Own
Arrays
Introduction
Storing Information
Iteration Exploration
Getting in Order
Appending to an Array
Island Builder
Appending Removed Values
Fixing Array Out of Bounds Errors
Generate a Landscape
Randomized Lands
Another Way to Create an Array
The Art of the Array
World Creation 9
Fruity arrays (1 session) 1. Pair up and choose one of three fruit salad recipes below.
Note that in everyday life, lists usually start with number 1. With data arrays, we start with the index of 0.
peachyBlend 1: 0) apple 1) orange 2) peach 3) peach 4) banana 5) apple
fruityMixALot 2: 0) banana 1) apple 2) peach 3) strawberry 4) banana 5) apple
orangeTastic 3: 0) peach 1) orange 2) orange 3) peach 4) orange 5) orange
2. Download this Keynote template. It contains five slides with these titles: apple, banana, orange, strawberry, and peach.
3. Figure out the steps for rearranging the slides into the order of your recipe. Write pseudocode of the steps you took, using the coding terms insert, append, remove, and index.
Think about it: Compare notes with those who had the same fruit salad recipe. Did anyone do it in fewer moves?
Graphing trends (1 session) 1. Download this version of the Graphing Starting Point
template.
2. Come up with a research question and hypothesis that look at trends over the last 10 years. For example, “What were the rainfall levels in California over the last 10 years?” or “What’s the U.S. birthrate for the last 10 years?”
3. Research your question, then plot your data year by year using the Lines page in the Graphing Starting Point.
4. The code on the Lines page uses an array to plot the number of students in each grade. Edit the code to fit your data.
Think about it: How are arrays used with graphing? Was your hypothesis correct? What’s the relationship between arrays and for loops?
Think Organized: Arrays Pick and Choose
Intermediate
4
10