Monday, March 01, 2010

Introduction to Programming – Week 6 – Starting Out With Alice – Chapter 3 – Variables, Functions, Math, and Strings

Starting Out with Alice: A Visual Introduction to Programming
  1. Variables
    Variable is a named storage location in the computer’s memory.
    Types:
    • Local variables - belong to a specific method. Can only be used in it.
    • Parameter variables – used to hold a value that is passed to a method
    • Class-level variables – belongs to a specific objects
    • World-level variable – a property that belongs to the world

    Creating a local variable
    Variable Names
    In programming, variables names are  unique, they cannot have the same name as they would conflict with each other. They should also be meaningful to what they do. They should also use camelCase convention.
    Variable Types
    Number – holds a number only
    Boolean – true or false values only
    Object – holds an object
    Other – specialized types of variables in Alice, such as a string, color, sound
    Initial Value
    When you create a variable it should be given a value. What is given does not matter as it can be changed. This is called a variable declaration, creating the variable and assigning it a value.
    Variable Assignment
    In Alice we use a ‘set’ instruction to reassign the variable to something else after the initial setting.
    Changing Properties with Set Instructions
    Select the object with the property, then drag it form the detail panel into the method editor and follow  instructions to set the value.

  2. Using Functions
    A function is a special type of method that causes some action to take place. To be run it has to be called, and optionally it may need to have some data know as arguments, passed to it. The difference between a function and a method is that a function returns a value back to the instruction that called it.  In Alice, the world and all objects have some primitive functions built into them. To see them, select the object and look at the details panel’s function tab.
    Using a Function to Ask the User for Input
    The world in Alice has three primitive functions that ask a user to input a value. They can ask for a number, ask for yes or no, or ask for a string. Unlike a method that can be called direct, a function returns a value so it has to be called from something that can use its value. 
    Primitive Object Functions
    The functions that objects have are:
    • Proximity - a value that says how close an object is to another object
    • Size – returns value relating to the size of an object
    • Spatial Relation – returns value indicating position relative to other objects
    • Point of view – returns value relating to the objects point of view
    • Other – returns miscellaneous values
    One proximity is distance to which returns a value in meters of the distance to another object.
  3. Creating Math Expressions
    To perform math in any program it will require math operators.   These include addition(+), subtraction (-), multiplication (*), and division (/).  When we do a equation like 3 + 2 the 3 and the 2 are operands.  In place of actual numbers we can put variables.
  4. Working with Strings and Text
    Strings are a sequence of characters that hold a prompt, warning message, name, etc. 
    Asking the User to Enter a String
    In Alice in order to give a variable a string we start the same way as with a number by declaring a variable and putting something in it move the tile to ask for an entry into it.
    Joining Stings
    Sometimes it is necessary to join two strings together to make a longer string.  The method used for this is the ‘joined with’ method. 
    Converting a Non-string to a String
    In Alice if we need to use a number as part of a string, we will convert it with an object ‘what as’ function.
    3D Text
    Alice allows you to create a 3D text object. It will have 4 properties:
    • text - the text that will display
    • font – the font that will be used
    • extrusion – the depth of the text
    • curvature- the curve that the text will have.
Advertisement
Exploring Wonderland: Java Programming Using Alice and Media Computation

No comments:

Post a Comment