5 Programming with Alice John Dougherty CS 100

  • Slides: 29
Download presentation
5. Programming with Alice John Dougherty CS 100: The World of Computing Haverford College

5. Programming with Alice John Dougherty CS 100: The World of Computing Haverford College www. cs. haverford. edu

Overview of the Module Role of Programming in Computing ¡ Simple Control and Data

Overview of the Module Role of Programming in Computing ¡ Simple Control and Data Structures ¡ Objects as Conceptual Tools ¡ Stories as Program Metaphors ¡ Virtual Worlds in Alice ¡ Representative Examples in Alice ¡ Working with Alice for Lab 2 ¡

Role of Programming … … … … implements an algorithm solves a problem accomplishes

Role of Programming … … … … implements an algorithm solves a problem accomplishes a task transforms input to output provides service to a set of clients recipe to get the job done fundamental to computing

Programs used so far in CS 100 ¡ ¡ ¡ ¡ ¡ Browser (IE,

Programs used so far in CS 100 ¡ ¡ ¡ ¡ ¡ Browser (IE, Safari, Netscape, Firefox) Email Client (Eudora, Outlook) File Server (storage) Platform Interface (Finder, X) Word Processor (MS-Word) Text Editor (Note. Pad, Text. Edit) HTML Editor (Taco. Edit, Dream. Weaver) Course Management (Black. Board) PDF Reader (Acrobat, Preview) Others …

Program and Process Interaction ¡ ¡ A Process is a program in execution A

Program and Process Interaction ¡ ¡ A Process is a program in execution A process can interact with a person or with another process Process A Process B Input Process Output Two-Process Interaction

Consider a Word Processor Find Input: the-word to find Output: location of the next

Consider a Word Processor Find Input: the-word to find Output: location of the next instance of the-word in document (or a message “not found”) Process Algorithm (could be): while more to search if this-word equals the-word display location of the-word in document else set this-word to next-word in document display message that the-word is not found the-word … this-word next-word …

Functions & Control Structures ¡ ¡ ¡ Sequential (default): do the next thing Selection

Functions & Control Structures ¡ ¡ ¡ Sequential (default): do the next thing Selection (if-else, switch): choose among a set of options Repetition: repeat a set of instructions l Counting loops (for) l Sentinel loops (while) l ¡ ¡ Implicit repetition via recursion Subprogram (function, methods) This set is Turing complete

Variables & Data Structures ¡ Simple, often “built-in” l l ¡ Linear structures l

Variables & Data Structures ¡ Simple, often “built-in” l l ¡ Linear structures l ¡ Integer, boolean, character Real approximation (float, double) Arrays, lists, strings Non-linear structures l Trees, graphs

Consider a function to determine if an array/list contains an item x … Array/List

Consider a function to determine if an array/list contains an item x … Array/List a[], with n = 5 a[0] a[1] a[2] a[3] a[4] Too Far! i 4 03 1 2 5 x

selection function contains(x, a) returns boolean // returns true iff x is in array

selection function contains(x, a) returns boolean // returns true iff x is in array a[n] { integer i = 0; while (i < n) { if (a[i] equals x) // found return true else // keep looking increase i by 1 } return false // not found } repetition sequential Control Structures are Nestable

Your Turn: Describe a function to see if a list contains any duplicate items

Your Turn: Describe a function to see if a list contains any duplicate items … Array/List a[], with n = 5 a[0] a[1] i a[2] a[3] a[4]

selection function duplicate(a) returns boolean // returns true iff a has any duplicate items

selection function duplicate(a) returns boolean // returns true iff a has any duplicate items { integer i = 0; while (i < (n-1)) { items rest [a[i+1]. . a[n-1]] item head a[i] if (find(head, rest) // duplicate return true else // keep looking increase i by 1 } return false // no duplicates } sequential repetition sequential Duplicate Finder Function

Objects as Conceptual Tools ¡ ¡ Object is defined by its properties and its

Objects as Conceptual Tools ¡ ¡ Object is defined by its properties and its methods/functions Pen as example l l l ¡ ¡ Properties: body, cap, ink cartridge Methods: write, refill Functions: empty? tip exposed? Program as collection of interacting objects Examples abound in Alice

Objects are also Nestable ¡ Pen Properties l body, cap, ink cartridge Body: cylinder,

Objects are also Nestable ¡ Pen Properties l body, cap, ink cartridge Body: cylinder, bottom ¡ Cap: cylinder, top, pocket tab ¡ Ink cartridge properties … ¡ Pocket tab properties … ¡

Nestable as Hierarchy (“Inheritance-ish”) Pen Body Cylinder Cap Bottom Cylinder Ink Cartridge Pocket Tab

Nestable as Hierarchy (“Inheritance-ish”) Pen Body Cylinder Cap Bottom Cylinder Ink Cartridge Pocket Tab Top Metal Strip Fastner

Stories as Program Metaphors ¡ Consider a script to describe the action in a

Stories as Program Metaphors ¡ Consider a script to describe the action in a scene l l ¡ ¡ ¡ Actors, props as objects Each has properties and methods Script (program) realizes the vision of the author (algorithm) More detailed blocking becomes objectbased programming at various levels We will begin with very simple, basic action

Virtual Worlds in Alice

Virtual Worlds in Alice

Alice Basics ¡ ¡ ¡ Select a virtual world Add objects to the world

Alice Basics ¡ ¡ ¡ Select a virtual world Add objects to the world Compose a story script Play the movie Refine story onto sub-scenes using world methods Build tools as needed l ¡ Functions, objects, lists Use the Alice tutorials to get started

Effective Programming in Alice Always have a story (goal) ¡ Build a new world

Effective Programming in Alice Always have a story (goal) ¡ Build a new world for each new feature for experimentation ¡ Decompose complex stories and build world methods and functions ¡ Test these methods/functions thoroughly, and comment ¡ Build incrementally ¡

Guidance for Alice ¡ Object-based l Avoid “do this” for “who/what does this” Parameter

Guidance for Alice ¡ Object-based l Avoid “do this” for “who/what does this” Parameter values selected on the fly ¡ Can nest before or after ¡ Clipboard to copy between methods/worlds (buggy) ¡ Save often, and even restart occasionally ¡

Basic Example in Alice Boat looping l l l Control Structures ¡ Sequential ¡

Basic Example in Alice Boat looping l l l Control Structures ¡ Sequential ¡ Selection ¡ Iteration Methods Parameters ¡ Integer ¡ Boolean Recursion Concurrency

Boat Looping Methods ¡ loop boat(go-left, qturns) l l l ¡ go-left: left or

Boat Looping Methods ¡ loop boat(go-left, qturns) l l l ¡ go-left: left or not-left (i. e. , right) qturns: # quarter turns (0. 25) move and turn concurrently figure 8(count) l l loop boat left 4 quarters, then loop boat not-left 4 quarters clover methods ¡ rounded square? serpentine? ¡

Advanced Example in Alice Space-Team l l l l l Objects ¡ Methods ¡

Advanced Example in Alice Space-Team l l l l l Objects ¡ Methods ¡ Properties Methods List as Parameter Functions (sizeof) Iteration Recursion Concurrency Representation

Space-Team Iterative Methods ¡ ¡ Each uses a list to represent team Helper functions/methods

Space-Team Iterative Methods ¡ ¡ Each uses a list to represent team Helper functions/methods l l ¡ traverse-loop() l l ¡ sizeof(team) function swap(here, there) method Visit each member of the team (list) For (all in order) say “visited” reverse 2() l l Put the team in opposite order For (half in order) swap with “partner”

Recursion and Alice a method/function that calls (a simpler/smaller version of) itself Example: a

Recursion and Alice a method/function that calls (a simpler/smaller version of) itself Example: a list is either … ¡ l l empty, or an item followed by a list Many algorithms are clearer when expressed recursively … … this is often not the case in Alice But it is an interesting challenge!

Boat-Loop Recursive Methods clover-rec-guts(leaf-count) if (leaf-count > 0) loop-boat(true, 3) // ¾ loop clover-rec-guts(leaf-count-1)

Boat-Loop Recursive Methods clover-rec-guts(leaf-count) if (leaf-count > 0) loop-boat(true, 3) // ¾ loop clover-rec-guts(leaf-count-1) ¡ clover-recursive() // starts the recursion clover-rec-guts(4) // 4 loops ¡

Space-Team Recursive Methods: traverse(space-team) Space-Team represented as a list A B C D E

Space-Team Recursive Methods: traverse(space-team) Space-Team represented as a list A B C D E head rest traverse() if (team is not empty) visit head(team) traverse(rest(team))

Space-Team Recursive Methods: reverse(space-team) reverse() if (team is not (empty or singleton)) swap(first, last)

Space-Team Recursive Methods: reverse(space-team) reverse() if (team is not (empty or singleton)) swap(first, last) reverse(all but first and last) last first -swap- A E B D C D B E A

Working with Alice for Lab 2 Download from www. alice. org or in KINSC

Working with Alice for Lab 2 Download from www. alice. org or in KINSC H 204/5 ¡ Download examples and lab templates from either course website or Black. Board ¡ Save often, and two copies ¡ Submit on storage in folder (lastname. 2) or Black. Board ¡ Do not wait until last week to start ¡