290 likes | 443 Views
5. Programming with Alice. John Dougherty CS100: The World of Computing Haverford College www.cs.haverford.edu. 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
E N D
5. Programming with Alice John Dougherty CS100: The World of Computing Haverford College www.cs.haverford.edu
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 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 CS100 • Browser (IE, Safari, Netscape, Firefox) • Email Client (Eudora, Outlook) • File Server (storage) • Platform Interface (Finder, X) • Word Processor (MS-Word) • Text Editor (NotePad, TextEdit) • HTML Editor (TacoEdit, DreamWeaver) • Course Management (BlackBoard) • PDF Reader (Acrobat, Preview) • Others …
Program and Process Interaction • A Process is a program in execution • A process can interact with a person or with another process Input Process Output Input Process Output Process A Process B Two-Process Interaction
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 (if-else, switch): choose among a set of options • Repetition: repeat a set of instructions • Counting loops (for) • Sentinel loops (while) • Implicit repetition via recursion • Subprogram (function, methods) • This set is Turing complete
Variables & Data Structures • Simple, often “built-in” • Integer, boolean, character • Real approximation (float, double) • Linear structures • Arrays, lists, strings • Non-linear structures • Trees, graphs
Consider a function to determine if an array/list contains an item x … Array/List a[], with n = 5 Too Far! 3 4 i 0 1 2 5
Control Structures are Nestable 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 } sequential repetition selection
Your Turn: Describe a function to see if a list contains any duplicate items … Array/List a[], with n = 5 i
Duplicate Finder Function 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 selection sequential
Objects as Conceptual Tools • Object is defined by its properties and its methods/functions • Pen as example • 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 • body, cap, ink cartridge • Body: cylinder, bottom • Cap: cylinder, top, pocket tab • Ink cartridge properties … • Pocket tab properties …
Stories as Program Metaphors • Consider a script to describe the action in a scene • Actors, props as objects • Each has properties and methods • Script (program) realizes the vision of the author (algorithm) • More detailed blocking becomes object-based programming at various levels • We will begin with very simple, basic action
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 • Functions, objects, lists • Use the Alice tutorials to get started
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 • 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 • Control Structures • Sequential • Selection • Iteration • Methods • Parameters • Integer • Boolean • Recursion • Concurrency
Boat Looping Methods • loop boat(go-left, qturns) • go-left: left or not-left (i.e., right) • qturns: # quarter turns (0.25) • move and turn concurrently • figure8(count) • loop boat left 4 quarters, then • loop boat not-left 4 quarters • clover methods • rounded square? serpentine?
Advanced Example in Alice Space-Team • Objects • Methods • Properties • Methods • Lists • List as Parameter • Functions (sizeof) • Iteration • Recursion • Concurrency • Representation
Space-Team Iterative Methods • Each uses a list to represent team • Helper functions/methods • sizeof(team) function • swap(here, there) method • traverse-loop() • Visit each member of the team (list) • For (all in order) say “visited” • reverse2() • 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 list is either … • 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) • clover-recursive() // starts the recursion clover-rec-guts(4) // 4 loops
Space-Team Recursive Methods:traverse(space-team) Space-Team represented as a list head traverse() if (team is not empty) visit head(team) traverse(rest(team)) rest
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 H204/5 • Download examples and lab templates from either course website or BlackBoard • Save often, and two copies • Submit on storage in folder (lastname.2) or BlackBoard • Do not wait until last week to start