Lesson Plan Industry Sector
Information Technology

## Decomposition and Abstraction Working Together

### Lesson Plan Overview / Details

In order to master complex problems, two key tools one needs in their thinking toolbox are decomposition and abstraction, concepts that must be learned for one to become a good computer scientist or in general terms a good problem solver.  Being able to decompose a large problem into smaller pieces and then knowing how to specify what each piece needs to do is a skill this lesson tries to impart to the students.

### Lesson Time

Trying to Leave the Room Activity
15 Minutes
Let's Practice Decomposition Activity
1 Class Period
Decomposition Explained Activity
1 Class Period
Introduction to BYOB Activity
2 Class Periods
Now Let's Try it Together Activity
1 Class Period
Assessment Activity
2 Class Periods

### Objectives and Goals

• Be able to modify existing code to meet requirement of different problems
• Understand that abstraction is a mechanism for hiding detail
• Be able to use the BYOB Scratch IDE
• Be able to use decomposition and abstraction methodologies in coding their projects

### Activities in this Lesson

• The purpose of this activity is get students interested in what can be done with BYOB Scratch.

At the beginning of a class session load the escape.ypr project file into the BYOB version and run it in a manner so all the students can view it.  Inform the students that the exact same code is used to have Karel escape from each wall enclosure by the user defined block start here avenue .. street..direction..beepers.   The hidden code in the block has Karel search for the door and when he finds it to exit.

• Lets Practice Decomposition - Check Understanding

In this activity students will build upon what they've learned in the previous lesson.  They must program Karel to climb stairs and pickup up beepers on the stair if there is one and put down a beeper on stairs that do not have a beeper.

Have the students start a new program by following the,  Activity Worksheet: Let's Practice Programming in a Karel World .  They can save it with the name big-Stair.   Provide
Big Stairs With A Beeper On Every Other Stair  background image and have the students to import it into their program.

The difference between this activity and the previous climbing stair activity is that Karel must pickup a beeper at every other stair and put down a beeper on stairs that don't have one.  Before the students begin ask them the following;

• Since the problem at hand is similar to the previous one can you use some of the code from the simple-stair problem?
• If you do use the same code must you change it in some way, if so what changes should be made?

After the discussion have the students to begin their programming.  Monitor their progress and give assistance when necessary.

• Big Stairs With A Beeper On Every Other Stair [ View Image ] [ Download Original ] This is a stage image that must be imported into the program
• Ask for volunteers to present their big-Stair program solutions and to explain how they came to a decision on what to do.  Did they use scripts from the simple-Stair program by slightly changing them?  If so, that's an example of program decomposition .  What remained the same between the two programs is the script that had Karel climb the stairs; the difference between the two is that in first one Karel had to pickup a beeper on every stair and in this one the assignment Karel had to pickup a beeper on every other stair and put down beepers.

Open the website CSE131: Introduction and use it to discuss abstraction and decomposition.  You can have the students read the definition and/or examples of the abstraction and decomposition from the site.

Discuss the definition; breaking down a problem (or system) into smaller sub-problems (or components); let them know that this is what they did when they initially examined their first iteration of the simple-Stair problem.  They gathered the blocks that were repeated and then placed them into a broadcast script.

Doing decomposition well makes it easier to modify the solution later by changing individual components, and also enables the reuse of components in solution to other problems.   Isn't this what was done for the big-Stair program?  The script that had Karel climb the stairs was reused with a slight modification.

Now it is time to begin using decomposition and abstraction which is   a mechanism for hiding detail so we need a different version of Scratch known as  BYOB ( B uild Y our O wn B locks).

• CSE131: Introduction A website by Kenneth J. Goldman that gives definition and examples of abstraction and decomposition.
• Prior to beginning this activity make sure that all the students have the BYOB version of Scratch installed on their machine and that they have access to the BYOB project files needed for the remaining activities.

Using both the Scratch and BYOB IDEs have the students to open both Karel project files (the Scratch version and the BYOB version) so the students can switch between the two IDEs.  Have them to first examine the Scratch version carefully and then the BYOB version.  During the examination have them to click on the Karel sprite so they can see the Karel scripts; click on the Motion icon so they can see the Karel motion blocks.  Have them to write down what they see as the difference between the two IDE versions.  This is what they should have as a difference:

• The Scratch version has four sprites; Karel, Beeper, Checker and stage .  The BYOB version only has three;   Karel, Beeper and stage .
• The Scratch version has five scripts; when green flag clicked ; when I receive Init ; when I receive Move ; when I receive TurnLeft when I receive TurnRight .  The BYOB version only has 1 script; when green flag clicked that contains only one block.

Give each student a copy of the Comparison of Scratch and BYOB Scripts; then enter into a discussion about abstraction as the students reexamine the scripts.

Begin the discussion by reminding the students about the short definition of abstraction; a mechanism for hiding detail; you can give them examples of abstraction such as someone who drives a car; they know how to step on the gas to make the car move forward or backwards; they know how step on the brake to make the car stop.  Chances are they don't know what is happening behind the scenes; how the engine works, how the transmission works; what components make up the drive train; those are all things that are hidden from the driver. Well the same thing can be said about the BYOB version of Scratch..

Look at the BYOB Karel script on the comparison handout.  When the green flag is clicked there is only one thing that we see happening; a block that states start at avenue 1, street 1, direction 90, with 0 beepers .  Where did this block come from?  It wasn't a part of the Scratch IDE that we have previously worked with; that's correct it is a new block; it is a user defined block.  That is what the BYOB version of Scratch allows you to do; create your own blocks.  BYOB is an acronym for   Build Y our O wn B locks

Think back to the previous Karel Scratch projects you worked on; you had to set the initAvenue and initStreet variables to a numbered avenue and street you wanted Karel to start on.  You also had to indicate in which direction you wanted Karel to face and how many beepers he should start with.  Then you had broadcast to the Init script.  Well in the BYOB version you really are doing the same thing but you don't see the detail of how that is done.  Remember abstraction is a mechanism for hiding detail.

There are other motion blocks that were created to hide detail; move, TurnLeft, TurnRight, check, PickBeeper and PutBeeper.

Do the following to find out how this detail was hidden.

• Right click on the Start at avenue ... block then click on edit.  A Block Editor should popup displaying the blocks that were used to build the Start at avenue........... block

The Start at avenue... user defined block is a good example of decomposition and abstraction working together.  In the original Scratch version of the project there were two scripts that were used to place Karel at his starting position; when green flag clicked and when I receive Init .  In the BYOB version those two scripts are combined into one script   (decomposition) and the detail of how it is done is hidden (abstraction).

Do the same process with the other user defined blocks except the PickBeeper and PutBeeper blocks; there are more things happening with those two that is beyond the scope of this unit.

• Comparison of Scratch and BYOB Scripts [ Download ] Document with images of Scratch and BYOB blocks that will be used to compare the different methods used in the two implementations of the Karel project.
• Now Let's Try It Together - Guided Practice

Now that the students have a basic understanding of BYOB Scratch it is time for them to practice. We'll start by giving the a project to work on with which they are familiar, simple stair.  The blocks we need are available to us; move, turnLeft, turnRight, pickBeeper but remember we now know how to use decomposition and abstraction so let's use them.  We could use decomposition to combine all four of the actions into a single block but remember that if we create a block we want to be able to reuse it in another project if necessary.  Another project may only want Karel to climb stairs but not pickup beepers.  So we'll use abstraction to hide the move, turnLeft, turnRight   actions.  We'll create a block that only has Karel climb the stairs, a climbStep block.

1. Open the BYOB Karel project.
2. Click on the stage sprite then click on the background tab .
3. Import the simple-stair stage image
4. Click on script tab
5. Click on the Karel sprite
6. On the Start at avenue.... block change the avenue parameter to 2 and the street parameter to 1
7. To create our own block we must first click on the variable button   then click on the make a block button.

The Make a block dialogue box pops up;

• since we want Karel to move up the stairs next click on the Motion tab
• since we want this to be a command block click on the command button
• since we only want Karel to climb stairs click on the for this sprite only radio button
• now we need to give the block a name so in the text box type in climb A Step  then click OK
• Now we can add the blocks that will have Karel climb the steps; first he has to turn left so we add the turnLeft block
• next we add the move block;  then we add the turnRight block; then we another move block and finally we add the pickBeeper block.
• Click OK

On your own add the necessary blocks to have Karel climb the stairs and pickup all the beepers.

• In this activity Karel must climb the stairs and pickup the beepers then he should go down the stairs and put down the beepers on the opposite side.  Karel must be facing to the right when he begins and at the end of the program run.  Hint: Click on the Can Rotate Button before you start the program.