Your paper prototype shows what your app looks like, and your clients have now tried it. Now you plan what it holds and what it does, before you write any code.
In Building Multiplication Maestro you built a list and a procedure that I planned. Here you plan your own. keepResults(matching:) and its results list have the shape College Board asks for, but your list and procedure must be your own idea, for your own client.
You plan this on your own, by hand, on paper. Later, on the AP Exam, you answer written questions about your list and your procedure on your own, so plan something you understand completely.
Try it first
Look at your paper prototype. Put a tick on every card where the app has to show or use something that there is more than one of: questions, animals, words, levels, pictures, sounds.
Now look at the ticks. What would it take to add five more of those things later? If the honest answer is “copy and paste a lot of code”, you have just found where your list belongs.
Plan your list
College Board asks for a list that stores a collection of data your app uses, and that makes your program easier to write, or easier to change, than it would be without the list.
On paper, write:
- Its name. Name it after what it holds:
animalFacts, notmyArray. - What one item holds. If one item needs more than one piece of information, list the pieces and their types (for example, a name that is a
Stringand a length that is anInt). - Three example items, written out in full.
- Why a list? One or two sentences: how does the list make your app easier to build, or easier to change later, than it would be without one?
Plan your procedure
Your procedure does something with your list that your app needs, and what it does depends on a parameter. For example: choose the questions for a level, count how many answers were right, pick the animals that match what the child chose.
Check your idea against College Board’s requirements. Your procedure:
A button’s action or a view’s body does not count as your procedure: College Board does not consider built-in structures like these to be student-developed. Your procedure is a function you write yourself and call.
Then write its steps in plain language, numbered, the way you would explain them to a classmate.
Choose your test inputs
A procedure that runs is not the same as a procedure that is right. Before you write yours, try this with the person beside you, at a whiteboard.
A spelling game should show every word shorter than the length the child picks. Here is the procedure:
func keepWords(shorterThan maximumLength: Int, from wordList: [String]) -> [String] {
var shortWords: [String] = []
for word in wordList {
if word.count <= maximumLength {
shortWords.append(word)
}
}
return shortWords
}Without running anything, choose three calls to this procedure that would convince you it works. Write each call and the result you expect. Then compare with the next pair. Did any of your six calls expose the bug? Which one, and what made it a good choice?
What the bug is (select to open after you have tried)
A call like
keepWords(shorterThan: 10, from: ["cat", "frog", "ox"])gives the right answer, and so does every call where no word is exactly the chosen length. The bug only shows at the boundary:keepWords(shorterThan: 3, from: ["cat", "frog", "ox"])should give["ox"], but gives["cat", "ox"], because theifuses<=where the game needs<.
Now pick at least three inputs for your own procedure, and write the output you expect for each. At least one should be at an extreme or on a boundary: an empty list, the smallest possible value, the largest possible value, a value exactly on the edge of your if.
An example
This is a fictional example, here only to show the shape of a plan. It is deliberately simple. Yours must be your own idea, for your own clients.
Part of the app: a screen where the child picks a length with a slider and sees every dinosaur longer than that.
List: allDinosaurs. One item holds a name (String), a length in metres (Int), and whether it ate meat (Bool). Examples: Triceratops, 9, false; Velociraptor, 2, true; Spinosaurus, 15, true.
Why a list: adding a new dinosaur means adding one line to the list. Without a list, every dinosaur would need its own variables and its own code on the screen.
Procedure: keepDinosaurs(longerThan:from:). Parameter: the minimum length the child chose, and the list to search. Returns the dinosaurs longer than that length.
- Start with an empty result list.
- For each dinosaur in the list:
- if its length is greater than the minimum length, add it to the result list.
- Return the result list.
Called: whenever the child moves the slider.
Test inputs:
| Minimum length | Expected result |
|---|---|
| 0 | all dinosaurs |
| 9 | only Spinosaurus (9 is not greater than 9) |
| 20 | an empty list |
In Swift, that plan becomes:
struct Dinosaur {
// MARK: - Stored properties
let name: String
let lengthInMetres: Int
let ateMeat: Bool
}
let allDinosaurs: [Dinosaur] = [
Dinosaur(name: "Triceratops", lengthInMetres: 9, ateMeat: false),
Dinosaur(name: "Velociraptor", lengthInMetres: 2, ateMeat: true),
Dinosaur(name: "Spinosaurus", lengthInMetres: 15, ateMeat: true),
]
func keepDinosaurs(longerThan minimumLength: Int, from dinosaurList: [Dinosaur]) -> [Dinosaur] {
var longDinosaurs: [Dinosaur] = []
for dinosaur in dinosaurList {
if dinosaur.lengthInMetres > minimumLength {
longDinosaurs.append(dinosaur)
}
}
return longDinosaurs
}Your plan comes before your Swift. If you change the plan while you build, change it on paper too.
NOTE
In Swift, the first item in a list is at index
0. On the AP Exam, the first item is at index1. Keep both in mind.
Curriculum connection
AAP-1.D
For data abstraction: a. Develop data abstraction using lists to store multiple elements. b. Explain how the use of data abstraction manages complexity in program code.
AAP-1.D.1 Data abstraction provides a separation between the abstract properties of a data type and the concrete details of its representation.
AAP-1.D.2 Data abstractions manage complexity in programs by giving a collection of data a name without referencing the specific details of the representation.
AAP-1.D.3 Data abstractions can be created using lists.
AAP-1.D.4 Developing a data abstraction to implement in a program can result in a program that is easier to develop and maintain.
AAP-1.D.5 Data abstractions often contain different types of elements.
AAP-1.D.6 The use of lists allows multiple related items to be treated as a single value. Lists are referred to by different names, such as array, depending on the programming language.
AAP-1.D.7 The exam reference sheet provides the notation
[value1, value2, value3, ...]to create a list with those values as the first, second, third, and so on items. For example,
Text:
aList ← [value1, value2, value3, ...]Block:
aList ← [value1, value2, value3]creates a new list that contains the values
value1,value2,value3, and...at indices1,2,3, and...respectively and assigns it toaList.Text:
aList ← []Block:
aList ← [ ]creates a new empty list and assigns it to
aList.Text:
aList ← bListBlock:
aList ← bListassigns a copy of the list
bListto the listaList. For example, ifbListcontains[20, 40, 60], thenaListwill also contain[20, 40, 60]after the assignment.AAP-1.D.8 The exam reference sheet describes a list structure whose index values are 1 through the number of elements in the list, inclusive. For all list operations, if a list index is less than 1 or greater than the length of the list, an error message is produced and the program will terminate.
Link to originalAAP-2.G
Express an algorithm that uses selection without using a programming language.
AAP-2.G.1 Selection determines which parts of an algorithm are executed based on a condition being true or false.
Link to originalAAP-2.J
Express an algorithm that uses iteration without using a programming language.
AAP-2.J.1 Iteration is a repeating portion of an algorithm. Iteration repeats a specified number of times or until a given condition is met.
Link to originalCRD-2.J
Identify inputs and corresponding expected outputs or behaviors that can be used to check the correctness of an algorithm or program.
CRD-2.J.1 In the development process, testing uses defined inputs to ensure that an algorithm or program is producing the expected outcomes. Programmers use the results from testing to revise their algorithms or programs.
CRD-2.J.2 Defined inputs used to test a program should demonstrate the different expected outcomes that are at or just beyond the extremes (minimum and maximum) of input data.
CRD-2.J.3 Program requirements are needed to identify appropriate defined inputs for testing.
Link to originalC1.3
demonstrate the ability to apply the process of functional decomposition in subprogram design;
Link to originalA1.5
describe and use one-dimensional arrays of compound data types (e.g., objects, structures, records) in a computer program.
Link to original