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

5. Programming with Alice John Dougherty CS100: The World of Computing Haverford College

Embed Size (px)

Citation preview

5. Programming with Alice

John DoughertyCS100: The World of ComputingHaverford Collegewww.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 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 …

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 …

a[0] a[1] a[2] a[3] a[4]

Array/List a[], with n = 5

x

TooFar!

i 012345

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

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

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

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 …

Nestable as Hierarchy(“Inheritance-ish”)

Pen

Body Cap Ink Cartridge

Cylinder Bottom Cylinder Pocket Tab Top

Metal Strip

Fastner

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

Virtual Worlds in 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

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 AliceBut 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)

A B C D E

Space-Team represented as a list

headtraverse()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)

first last

-swap-

A C D EBE AD B

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