Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 104 – Fall 2011 Exploring Computer Science Build Your Own Blocks September 19, 2011.

Similar presentations


Presentation on theme: "CS 104 – Fall 2011 Exploring Computer Science Build Your Own Blocks September 19, 2011."— Presentation transcript:

1 CS 104 – Fall 2011 Exploring Computer Science Build Your Own Blocks September 19, 2011

2 Agenda Today  Discuss Team Project #1  Building Your Own Blocks  Review Wing paper Wednesday  Quiz 3  Hand back Quiz 2  Individual HW2 Due at midnight

3 Three Types of Blocks Command  A method; it performs a given set of instructions  It can receive zero or more inputs  It does not return any values Reporter  A function; it performs a given set of instructions, but it returns a value  It can receive zero or more inputs Predicate  A boolean function; given two or more inputs, it performs a given set of instructions and returns true or false

4 Procedural Abstraction Software can often be used in multiple contexts; the design of a program should consider generalizing common tasks Procedural abstraction is about using parameters and code written in a way that can be reused in many contexts For example  Instead of a single square that is 10 pixels long, an improvement would be a way to draw a shape of any number of sides with any given length

5 Draw a Square What would be advantageous about building our own block that only draws squares? Are there any disadvantages?

6 Inputs (Parameters) What if we wanted to draw squares of different sizes? Does it make sense to create a block for each different sized square? Parameters allow us to generalize a problem.

7 Adding Inputs Two methods:  Create the block and then use the plus signs  Use ‘%’ signs to designate the input value while creating the block

8 Draw any shape How would you build a block to draw ANY shape of ANY size?  What inputs would be needed?  What type of block would be used?

9 Abstraction The idea of hiding the details  Loop example Instead of a repeat loop, we can use a do until loop and a counter, but the repeat loop hides the counter from us  Square Draws a square and hides the details of how the square is drawn from us

10 Max Function The max function should take two inputs and return (give an answer) the higher of the two values. This is a reporter block because we want to return a value We named our inputs val1 and val2, but since they are both numbers, this is not necessary.

11 Validating Parameters What happens if the user accidentally enters a letter instead of a number for the max problem? It’s important to validate the information users enter  Common practice in software development, particularly in web application development DOB Credit card info Telephone numbers Social Security How would we validate that the inputs are numeric?

12 Validating Parameters

13 <= This comparison or relation does not currently exist. How would we write it? Because <= is a relational operator, it returns true or false; therefore, it is a predicate block

14 Why build custom blocks?

15 Summary We can create our own blocks to simplify our code and achieve abstraction. These blocks improve code reusability. Parameters, or inputs, can be used to further the generality and reusability of these blocks. Possible quiz question: you will be given a function to create and will have to describe how to create it. Try creating an average operator that takes 3 inputs.


Download ppt "CS 104 – Fall 2011 Exploring Computer Science Build Your Own Blocks September 19, 2011."

Similar presentations


Ads by Google