Page 2: Planning a Quiz App
Unit 2, Lab 2, Page 2
On this page, you will begin to develop a quiz app by creating an abstract data type to pair the questions with their answers.
- Decide what type of quiz you would like to build, and write three to five questions and their correct answers.
- Read the following section. (Don’t build these in Snap!. You’ll build your own quiz later on the page.)
: Sublist
A sublist is a list used as an item of another list.
(The word sublist is also used to refer to some subset of a list.)
You’ll use a list to store your quiz items in a global variable and use a sublist for each question/answer pair. Then you can choose just one pair at a time to work with.
But code like is hard to read and understand. A better way is to use abstraction to organize the quiz items. The abstraction just hides the list
and item of
blocks, so it isn’t complicated to build, but it can make your code much easier to write, read, and debug.
: Data Types
- A data type is what kind of data something is (number, text string, list, etc.). For example, number is the data type for the first input to and list is the the data type for its second input.
-
Each programming language provides some primitive data types (data types that are built-in). For example, Snap! provides numbers, text (words and sentences), Booleans (true or false), lists, and some you haven’t yet used as data (such as sprites and costumes). This menu shows all of Snap!’s primitive types.
: Abstract Data Types
-
An abstract data type (ADT) is a custom data type that’s meaningful to your program. It’s not built into the language; you develop it as you code. This is an abstraction because it hides the details inside the constructor and selectors so that the programmer who uses a
quiz item
has to think only about questions and answers, not about list indices. -
The block is the constructor; it constructs one example of the data structure. By naming the pieces of the structure, the constructor makes it certain that every time you use it, you build the structure in the right order.
-
The and blocks are the selectors; they each select one piece of the data structure.
The word “abstract” is often used casually to mean something harder to understand or more complex, but in computer science, its meaning is almost the opposite. ADTs are things that you, the programmer, create to make your program easier for you and others to read, debug, and improve.
The constructor and selector together implement the quiz item
abstract data type.
- Data abstraction is the creation and use of abstract data types in a program.
-
Build the custom
quiz item
abstract data type (both the constructor and the two selectors). -
Click on the arrow to the right of the input name:
- Choose the data type you want for that input. (For this project, you’ll use the “text” and “list” input types.)
- Click “OK.”
- Create a global variable to store your quiz items and initialize it as a list of items, using your constructor where appropriate.
-
Table View for computer science quiz Watcher (your quiz will be different):
At first the watcher will look like this:
Drag out the resize handle at the lower right to get this:
Then hover your mouse over the letter A near the top. It will turn into a digit 1. Grab it and drag it to the right:
-
List View for computer science quiz Watcher:
- Test both the selectors for different items in your list of quiz items, and debug any problems.
Specifying an Input Type
Your selectors expect a quiz item, i.e., a list, as input. You can make your blocks show what type of data they expect. It’s not necessary in Snap! but, like assigning a color to a block, it can be a helpful reminder of what the block does and what type of input it expects. You’ve already seen input slots of several shapes, indicating different expected data types.
In the Block Editor while creating a selector, click on a plus sign to enter an input name. Then…
Snap! has two different views for lists within lists. You can switch which view you see by right-clicking (or control-clicking) on the quiz watcher (or whatever you called the variable) on the stage.
If you don’t see the watcher on the stage, make sure the checkbox beside the quiz variable in the Variables palette is checked.
: Table
A table is a two-dimensional data structure with rows and columns. If you’ve used a spreadsheet program, what it displays is a table.
In Snap!, a table is implemented as a list of lists, in which each sublist is one row of the table.