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.

  1. Talk with Your PartnerDecide what type of quiz you would like to build, and write three to five questions and their correct answers.
  2. Take turns speakingRead 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. set (computer science quiz) to (list (list (What is the computer science term for looping or repetition?) (iteration)) (list (What is the name for the abstraction in a program that can hold a value?) (variable)) (list (What kind of variable is available only in the part of the program where it is defined?) (local variable))) ask (item(1) of (item (3) of (computer science quiz))) and wait
if (item (2) of (item (3) of (computer science quiz)) = answer) {
say (That's correct!) for (2) secs
}

But code like item (1) of (item (3) of (computer science quiz)) 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.
set (computer science quiz) to (list (question: (What is the computer science term for looping or repetition?) answer: (iteration)) (question: (What is the name for the abstraction in a program that can hold a value?) answer: (variable)) (question: (What kind of variable is available only in the part of the program where it is defined?) answer: (local variable))) ask (question of quiz item (item (3) of (computer science quiz))) and wait
if (answer = answer from quiz item (item (3) of (computer science quiz))) {
say (That's correct!) for (2) secs
}

: 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 item (1) of () 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.

    primitive types: number, text, Boolean, list, sprite, command, reporter, predicate

: Abstract Data Types

    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.

  • 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 quiz item, question: () answer: () 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 question from quiz item: () and answer from quiz item: () blocks are the selectors; they each select one piece of the data structure.

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.
  1. “U2L2-Quiz”Start a New Project called U2L2-Quiz 

  2. Build the custom quiz item abstract data type (both the constructor and the two selectors).
  3. 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…

    1. Click on the arrow to the right of the input name:
      create input name right arrow
    2. Choose the data type you want for that input. (For this project, you’ll use the “text” and “list” input types.)
    3. Click “OK.”
  4. Create a global variable to store your quiz items and initialize it as a list of items, using your constructor where appropriate.
  5. 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. quiz watcher checked

    • Table View for computer science quiz Watcher (your quiz will be different):
      cs quiz watcher with first column expanded

      My watcher is too narrow.

      At first the watcher will look like this:
      cs quiz watcher at initial size
      Drag out the resize handle at the lower right to get this:
      cs quiz watcher resized
      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:
      cs quiz watcher with first column expanded

    • List View for computer science quiz Watcher:
      cs quiz watcher, list view

    : 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.

    Save your work

  6. Test both the selectors for different items in your list of quiz items, and debug any problems.