5. programming with alice

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

Upload: prisca

Post on 06-Jan-2016

41 views

Category:

Documents


2 download

DESCRIPTION

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 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: 5. Programming with Alice

5. Programming with Alice

John DoughertyCS100: The World of ComputingHaverford Collegewww.cs.haverford.edu

Page 2: 5. Programming with Alice

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

Page 3: 5. Programming with Alice

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

Page 4: 5. Programming with Alice

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 …

Page 5: 5. Programming with Alice

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

Page 6: 5. Programming with Alice

Consider a Word Processor Find

Input: the-word to findOutput: 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 documentdisplay message that the-word is not found

the-word

this-word next-word …

Page 7: 5. Programming with Alice

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

Page 8: 5. Programming with Alice

Variables & Data Structures

Simple, often “built-in” Integer, boolean, character Real approximation (float, double)

Linear structures Arrays, lists, strings

Non-linear structures Trees, graphs

Page 9: 5. Programming with Alice

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

TooFar!

i 012345

Page 10: 5. Programming with Alice

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) // foundreturn true

else // keep looking

increase i by 1}return false // not found

}

Control Structures are Nestable se

lect

ion

repe

titio

n

sequ

entia

l

Page 11: 5. Programming with Alice

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

Page 12: 5. Programming with Alice

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 trueelse // keep

lookingincrease i by 1

}return false // no duplicates

}

sele

ctio

n

repe

titio

n

sequ

entia

l

sequ

entia

l

Page 13: 5. Programming with Alice

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

Page 14: 5. Programming with 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 …

Page 15: 5. Programming with Alice

Nestable as Hierarchy(“Inheritance-ish”)

Pen

Body Cap Ink Cartridge

Cylinder Bottom Cylinder Pocket Tab Top

Metal Strip

Fastner

Page 16: 5. Programming with Alice

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

Page 17: 5. Programming with Alice

Virtual Worlds in Alice

Page 18: 5. Programming with Alice

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

Page 19: 5. Programming with Alice

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

Page 20: 5. Programming with Alice

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

Page 21: 5. Programming with Alice

Basic Example in Alice

Boat looping Control Structures

Sequential Selection Iteration

Methods Parameters

Integer Boolean

Recursion Concurrency

Page 22: 5. Programming with Alice

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?

Page 23: 5. Programming with Alice

Advanced Example in Alice

Space-Team Objects

Methods Properties

Methods Lists List as Parameter Functions (sizeof) Iteration Recursion Concurrency Representation

Page 24: 5. Programming with Alice

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”

Page 25: 5. Programming with Alice

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 AliceBut it is an interesting challenge!

Page 26: 5. Programming with Alice

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

Page 27: 5. Programming with Alice

Space-Team Recursive Methods:traverse(space-team)

A B C D E

Space-Team represented as a list

headtraverse()if (team is not empty)

visit head(team)traverse(rest(team))rest

Page 28: 5. Programming with Alice

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-

A C D EBE AD B

Page 29: 5. Programming with Alice

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