Week 2

Fundamental Program Elements

Author:Christopher Haynes
Email:chaynes@indiana.edu
Affiliation:Indiana University
Course:BL CSCI A201
Date:2008-01-18
Copyright © 2008, Christopher Haynes—all rights reserved.

Contents

Overview of Week 2

Functions

Another function example

Picture1.png


Alice Built-in Functions

Picture2.png


Demo: mummyDistanceFunction.a2w


Picture3.png


Demo concepts

Solving a collision problem

Picture4.png


Demo: subtracting a literal value

Demo: subtracting a function value

Demo: a complex expression

Help -- This is getting complicated!

Problem

I need a way to organize what I'm learning about a programming language, so I can better remember it.

The Big Picture:

Knowledge about programming languages is organized into:

Major kinds of syntactic elements

Kinds of Alice statements used so far

More kinds (control statements) coming this week

Kinds of Alice expressions used so far

Operators

Simple Control Structures in Alice

Control statements

Conditional execution

Example

Picture8.png


If/Else

Picture9.png


Storyboard

Picture10.png


Demo: If/Else

Picture11.png


Demo concepts

Relational operators in Alice

Picture12.png


Type checking

So what kind of types do I need to know about in Alice?

Common types of Alice values, with examples

General If/Else syntax

clicker In Alice actions are performed by

  1. statements
  2. expressions
  3. relational operators
  4. A and B
  5. all of the above

Answer: A

clicker A literal is used to

  1. test if a condition is always true
  2. print program values
  3. introduce comments in a program
  4. indicate a value particular value in the program text

Answer: D

clicker Which of the following yield a value?

  1. statement
  2. method call
  3. expression
  4. function call
  5. C and D
  6. all of the above

Answer: E

Monster steps

Pseudo-code for left/right steps

Picture13.png


Demo: left / right steps


Dead man walking!

Demo: mummyLoop.a2w


Picture15.png


Loopy programming

Organizing larger programs

Structure of Object-oriented programs (review)

Classes and objects in Alice programs


Alice methods we have used so far

Managing large programs

The BIG picture of the BIG problem of BIG programs

Divide and conquer

Refactoring

Example of need for refactoring

Recall the penguins.a2w program from last week. It works, but the style is not good for it is too complicated for one method.

Penguin1.png
(continued)


penguins.a2w demo continued

Penguin3.png


We now refactor it.

Demo continued: jump method

jump.png

Demo continued: dive off method

diveoff.png

Demo: penguinmethods.a2w

world.png
(continued)

Demo structure

Creating a new method in Alice

  1. To associate the new method with the "World", select World in the object browser
  2. Select the methods tab in the details area
  3. Click on the create new method button


TWO reasons for defining methods

Abstraction

Code reuse

Demo: zombieWalk.a2w

Picture25.png
Picture26.png


Plan ahead!

Demo: betterZombieWalk.a2w

Picture20.png Picture21.png
Picture18.png


Demo concepts

Creating an Alice parameter

Picture19.png


Terminology: Parameter and arguments

A parameter object as an argument

Picture17.png


Alice sounds

Alice parameter types

Parameter concepts and terminology

clicker In Alice, questions are asked primarily using elements in which object detail tab?

  1. methods
  2. functions
  3. properties
  4. any of the above

Answer: B

clicker The best Alice control construct for performing some actions a fixed number of times is

  1. Repeat
  2. While
  3. For
  4. Loop

Answer: D

clicker Two common reasons for defining more than one method in a program are

  1. code reuse and abstraction
  2. efficiency and abstraction
  3. code reuse and efficiency
  4. efficiency and clarity

Answer: A