Top Down Design with PB&J



Corresponding Material

Top Down Design



Discussion

Top Down Design is the process of taking a large problem and breaking it down into smaller, more manageable pieces. This is a helpful coding concept because sometimes it is difficult to determine where to begin when met with a difficult challenge and this process makes it easier to find a place to start.

Have you ever made a peanut butter and jelly sandwich? If someone asked you right now to make one, you could probably do it without much thought because you’ve made them multiple times before. But what if an alien came to Earth and was asked to make a PB&J? They have never done it before so going from the raw materials (bread, knife, peanut butter, jelly) and turning them into a finished sandwich might seem like a daunting task.



Class Exercise

Write a code that an alien (you can assume the alien has hands) could follow to make a PB&J if they knew Python (don’t worry about using actual Python commands we’ve learned. Just stick to following the rules we’ve learned so far.) Be sure to break your problem down into smaller tasks and use functions, loops, and comments to make your code more readable.

The beginning of the code has been written for you:



def gather_materials():

get_jelly()

get_peanut_butter()

get_two_slices_bread()

get_knife()

get_plate()



Complete the code in the box below.

1 answer

Certainly! Below is a Python-like pseudocode that breaks down the process of making a peanut butter and jelly sandwich using the concept of Top Down Design. I've defined various functions to represent each step of the process, along with comments to make the code clear and understandable for an alien.

# Function to gather all necessary materials
def gather_materials():
    get_jelly()          # Step 1: Get jelly
    get_peanut_butter()  # Step 2: Get peanut butter
    get_two_slices_bread()  # Step 3: Get two slices of bread
    get_knife()          # Step 4: Get a knife
    get_plate()          # Step 5: Get a plate

# Function to spread peanut butter on bread
def spread_peanut_butter():
    take_slice_of_bread()  # Step 1: Take one slice of bread
    use_knife_to_spread_peanut_butter()  # Step 2: Use the knife to spread peanut butter on the bread

# Function to spread jelly on the other slice of bread
def spread_jelly():
    take_second_slice_of_bread()  # Step 1: Take the second slice of bread
    use_knife_to_spread_jelly()    # Step 2: Use the knife to spread jelly on the second slice of bread

# Function to combine both slices into a sandwich
def combine_slices():
    place_peanut_butter_slice_on_plate()  # Step 1: Place the peanut butter slice on the plate
    place_jelly_slice_on_top()            # Step 2: Place the jelly slice on top of the peanut butter slice

# Main function to make a PB&J
def make_pb_and_j():
    gather_materials()  # Step 1: Gather all ingredients and tools
    spread_peanut_butter()  # Step 2: Spread peanut butter on the first slice
    spread_jelly()  # Step 3: Spread jelly on the second slice
    combine_slices()  # Step 4: Combine the two slices into a sandwich
    serve_sandwich()  # Step 5: Serve the sandwich on a plate

# Function to serve the sandwich
def serve_sandwich():
    print("Your delicious Peanut Butter and Jelly sandwich is ready to eat!")

# Call the main function to execute the steps
make_pb_and_j()

Explanation:

  1. gather_materials(): This function calls other functions to simulate gathering all the necessary materials.
  2. spread_peanut_butter(): Defines how to spread peanut butter on one slice of bread.
  3. spread_jelly(): Defines how to spread jelly on another slice of bread.
  4. combine_slices(): This function combines both slices to create the final sandwich.
  5. make_pb_and_j(): This main function coordinates the entire process.
  6. serve_sandwich(): Final step to let the alien know the sandwich is ready.

Each function represents a smaller, manageable task, making it easier to follow the instructions step by step.