Download presentation
Presentation is loading. Please wait.
Published byEmilie Kimbro Modified over 10 years ago
1
5. Programming with Alice John Dougherty CS100: The World of Computing Haverford College www.cs.haverford.edu
2
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 2Lab 2
3
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
4
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 …
5
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 Process A Process B Two-Process Interaction
6
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-wordnext-word… …
7
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
8
Variables & Data Structures Simple, often built-in Integer, boolean, character Real approximation (float, double) Linear structures Arrays, lists, strings Non-linear structures Trees, graphs
9
Consider a function to determine if an array/list contains an item x … a[0]a[1]a[2]a[3]a[4] Array/List a[], with n = 5 x Too Far! i 012 34 5
10
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 } Control Structures are Nestable selection repetition sequential
11
Your Turn: Describe a function to see if a list contains any duplicate items … a[0]a[1]a[2]a[3]a[4] Array/List a[], with n = 5 i
12
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 } selection repetition sequential
13
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
14
Objects are also Nestable Pen Properties body, cap, ink cartridge Body: cylinder, bottom Cap: cylinder, top, pocket tab Ink cartridge properties … Pocket tab properties …
15
Nestable as Hierarchy (Inheritance-ish) Pen Body CylinderBottom Cap Cylinder Pocket Tab Metal Strip Fastner Top Ink Cartridge
16
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
17
Virtual Worlds in Alice
18
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
19
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
20
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
21
Basic Example in Alice Boat looping Control Structures Sequential Selection Iteration Methods Parameters Integer Boolean Recursion Concurrency
22
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?
23
Advanced Example in Alice Space-Team Objects Methods Properties Methods Lists List as Parameter Functions ( sizeof ) Iteration Recursion Concurrency Representation
24
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
25
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!
26
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
27
Space-Team Recursive Methods: traverse(space-team) ABCDE Space-Team represented as a list head traverse() if (team is not empty) visit head(team) traverse(rest(team)) rest
28
Space-Team Recursive Methods: reverse(space-team) reverse() if (team is not (empty or singleton)) swap(first, last) reverse(all but first and last) first last -swap- ACDEBEADB
29
Working with Alice for Lab 2 Download from www.alice.org or in KINSC H204/5 www.alice.org Download examples and lab templates from either course website or BlackBoardcourse website Save often, and two copies Submit on storage in folder ( lastname.2 ) or BlackBoard Do not wait until last week to start
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.