Use the "PB&J" sandwich activity to introduce important components of algorithms.

  • The PB&J activity demonstrates that developing successful algorithms requires students know the following:
    • what data is available,
    • what basic functions/methods/operations are available,
    • how to handle special cases,
    • how to sequence operations properly,
    • and how to determine the level of detail required.
  • This activity underscores how easily instructions can be misinterpreted, even when the steps seem clear to a person.
    • Students assume that computers interpret instructions like they would. This leads to nonspecific algorithms that the computer misinterprets.
      • Tell students to write code with the mindset that their instructions will be misinterpreted.
    • Pro-tip from Myra Deister: Don't use real peanut butter for this activity! Some people are deathly allergic; use something else, like butter, instead.
    • Pro-tip from Leslie Aaronson: Buy crackers instead of bread to save money and time.
      • Making sandwiches with crackers is much faster!
      • Tell students that your "computer" understands the word bread as synonymous with the word cracker.
    • Pro-Tips from Brad Ashley: Introduce psuedocode using this energetic video of college students completing this activity from Harvard’s EdX course, CS50 (cued at the 18 minute mark, activity ends at 26 minute mark)!
      • Talk about abstraction!
        • If you use the same series of steps to get the peanut butter and the jelly out of their jars, comment on how these actions could be abstracted to get students thinking about loops and function calls.
    • Activity Prep:
      • Buy supplies for creating peanut butter and jelly sandwiches.
    • Activity:
      • Ask students to individually make a list of how to make a PB&J sandwich.
      • Using the instructions students provide you, attempt to make PB&J sandwiches.
      • Follow the instructions as literally as possible.
        • The instructions might be at such a high level, you (the computer) cannot even open a jar—that’s okay.
      Extra activity challenges:
      • Use jars of jelly that have a pry top, rather than a screw top.
        • This emphasizes that it’s necessary to consider special cases.
      • Include jars that have seals and bags of bread closed with twist-ties.
      • If you’re using a knife, carefully grab it by the blade instead of the handle, unless students instruct otherwise.
      • Use plastic knives of different colors.
        • If students ask you to pick up a knife, you won’t know which knife to pick up unless they’re specific about which one you use.
      • After the first round of instructions from individual students have students work on creating instructions in groups after completing the task alone.
        • This helps them see the input from other students can help them identify problems in their proposed solutions, which will lead to better algorithms.
        • Collect these new instructions and try again to make PB&J sandwiches.