CS Principles
This activity addresses the concept of abstraction, focusing
on functions and parameters (or arguments). It
introduces functions as re-usable programming abstractions and shows
how parametrization can be used to generalize a specific solution.
It addresses the following learning objectives.
- 7-8: The student can develop and use abstractions.
- 15-16: The student can develop and express an algorithm in Pseudocode and App Inventor.
- 20-22: The student can use abstractions (specifically functions) to manage complexity in programs and develop a
correct program (with functions).
Inclass/Homework Exercises
Write a function that converts temperature in Celsius to temperature
in Fahrenheit.
Identify a conversion function that you are interested in and write
an app that performs that conversion. For example,
# Comment: A function to convert Celsius to Fahrenheit
# where F = 9/5C + 32
# Arguments: C represents temp in Celsius
To celsiusToFahrenheit(C):
Return: (9 / 5 * C) + 32
|
(Click to enlarge.)
|
- If you are a runner, you might want to convert kilometers to miles
or vice versa.
# Comment: A function to convert km to miles
# where 1 km = 0.62 miles
# Arguments: km represents number of kilometers
To kmToMiles(km):
Return: km * 0.62
|
(Click to enlarge.)
|
- If you are interested in baking or cooking, you might want to
convert tablespoons to ounces, or something in that area.
# Comment: A function to convert tablespoons to ounces
# where 1 tbls = 0.5 ounces
# Arguments: tbls represents number of tablespoons
To tblsToOunces(tbls):
Return: tbls * 0.5
|
(Click to enlarge.)
|
- If you are baseball player you might want to calculate earned
run average or slugging percentage.
# Comment: A function to compute earned run average (ERA)
# which is number of earned runs per 9 innings
# Arguments: innings, number of innings
# earnedruns, number of earned runs
To era(innings, earnedruns):
Return: earnedruns / innings / 9
|
(Click to enlarge.)
|
- If you are a squash player, you might want to
convert wins and losses into winning percentage.
# Comment: Function to convert wins and lossed
# into winning percentage
# Arguments: number of wins and losses
To winningPercentage (wins, losses):
Return: wins / (wins + losses)
|
(Click to enlarge.)
|
It's up to you to choose a conversion that you would find truly useful.
- Write a Pseudocode function for your conversion. Have you pseudocode
checked by the instructor or TA.
- Convert your Pseudocode into an App Inventor function. Use the
blocks editor to test that it is correct.
- Design and implement a simple app that peforms that conversion.
You app should have a simple interface. For example, your app could
use a simple Input-Process-Output design, similar to
the Leap
Year app. It could have 1 or 2 TextBoxes where the user
can enter the value(s) needed as the input to the function,
a Button that is clicked to perform the conversion, and
a Label to display the result. You could copy and adapt the
Leap Year app for this purpose.
- Describe your conversion algorithm and your app on a Portfolio page.
Homework
Finish for homework whatever you don't complete in class.