Abstraction Contraption

(solutions)

9/21

9/26 Work on this after class lab

http://snap.berkeley.edu/snapsource/snap.html#present:Username=whuangpha&ProjectName=drawing%20starter2

Fill in the scripts to draw the same size square 3 different ways. Make sure you follow the directions in the comments so you're using the right blocks for each script. Press space key to clear the stage in between each test of your scripts.


9/25 DO NOW: Guessing Game (Refer to Handout)

Lab: Go to the starter project at http://bitly.com/aelcs-lab2-5 then follow the directions on the handout.


9/22 DO NOW: Boolean Practice

Open the following Startercode

There are three rooms: Start, Checkpoint and End.

Sonic should be able to move to each room by moving left and right. Start <--> Checkpoint <--> End

Find the errors and complete the code!


TEALS Survey!

Please take 5-10 minutes to take the survey below! Thank you!

https://www.surveymonkey.com/r/StudentPre2017-18


Abstraction is a fancy word in Computer Science that means hiding complexity. Your brain like a computer (remember we are computers) has a limited amount of memory and it works fastest when it is less cluttered. Abstraction is the tool we will practice using today in order to clean up some of that clutter.

Building Blocks

Variables hold on to values and serve as containers for numbers or letters. Blocks are the same as variables but for scripts instead of values.

http://snap.berkeley.edu/snapsource/snap.html#present:Username=MrBurgess&ProjectName=Schwifty

Above is a link to some code that has a lot of repetition. Try to find the places where the code is identical. Look for collections of blocks that look exactly same.

Press the left arrow, press the right arrow, press down. Take note of similarities but do not worry about understanding the code. Your goal is to look for patterns. Think less about the overall program and focus on just the repeated parts. This is a form of abstract thought. Thinking about less at any given time so you can solve smaller, more focused problems.


In the same way you created variables to store values try to create blocks to contain the repeated code.

Use the "make a block" button to create a "dance" block with the code that is shared from both the left and right key presses.

Use this same block in both the left and right key press sections.

This removes the duplication that exists in the current code.

Change the down key press code so that it uses the new block as well.

This allows us to hide code so that we do not have to be thinking about it when we are solving other problems.

This also keeps us from repeating ourselves, which can be tedious and prone to errors.

Schwifty Solution

When solving the above problem you are looking for large sections of repeated code. Note in the image below that most every line is repeated minus the line where we set the costume.

This big mess of code can end up as the image pictured to the right. The difference in number of blocks used in each solution is

1st Image: 47

2nd Image: 18

The code inside of Dance is only written the one time and any changes to it are reflected on each of the key press. This code can be shortened even more


Blocks can have what are called "parameters". These are variables they use inside the block that are passed in. In this case we are taking the values: "left" and "right" and using them inside the block to eliminate the need for an external costume change. Just like a Move block takes an input that tells it how far it should go, you can add any variables you'd like to custom blocks to make them more powerful.

This solution is more flexible, less bug prone and reduces your total number of blocks to 14!!!

Not only is it smarter... it is lazier and in Computer Science that word isn't so bad. Make the bits do your work for you.

Consider how you can use custom blocks that take in inputs (either explicitly typed out inputs like the word "left" or variable blocks like Costume#) to help in your coding. Look for areas in your Project 2 that are repetitive and consider how abstraction can help the code read more nicely and also significantly decrease the number of blocks you need to manage.

If you need a non coding break or finish everything, try these boolean brain teasers

http://puzzlewocky.com/brain-teasers/knights-and-knaves/

http://philosophy.hku.hk/think/logic/knights.php

If you need more to code

Try implementing variables and custom blocks in your Project 2 or try implementing the Pong project from the Projects page.

Abstraction is a fancy word in Computer Science that means hiding complexity. Your brain like a computer (remember we are computers) has a limited amount of memory and it works fastest when it is less cluttered. Abstraction is the tool we will practice using today in order to clean up some of that clutter.