Friday, February 05, 2010

Introduction To Programming (week 4) – Starting Out With Alice – Chapter 2 – Programming in Alice

 

Starting Out with Alice: A Visual Introduction to Programming

  1. Writing Methods
    Methods are sets of instructions that will execute. In Alice we create them in the method editor.  By default it is given the name world.my first method . This is dot notation. The left of the dot is the object the method belongs to, the right is the name of the method. We will drag tiles from another window into the editor to program in the Alice environment.
    Primitive Methods
    All objects in Alice have common methods written into them. These primitive methods do things like move, turn, change size, etc. To see them, select an object and go to the method tab on the detail panel. When you drag the tile into the editor it may pop up a window and ask questions about durations, distances or other things. Set up all your steps and hit the play button and see what it does.
    Method Calls and Arguments
    When a programmer execute the method they are said to ‘call the method’ and the instruction that executes it is called a ‘method call’. If we put a method in a program we may need to add information to it (distance, direction) which are called arguments.
    More About Primitive Methods
    [This section is a tutorial on using methods in Alice]
    The more … Editing Tag
    In Alice, at the end of the instruction tile is a more button. The arguments that come up here will vary depending on the method being called. Duration is a common one, the time that it will take for something to happen.  
    Deleting and Copying Instructions
    There are two ways to get rid of instructions that you no longer want to use in the editor. The first is to right-click it and choose delete. The other is to drag the time to the trash can on the toolbar near the top of the screen. A right-click will also bring up a copy option that allows you to make several instances of the same command.
    Objects With Custom Methods
    When an object is created it comes with primitive methods, but it also can come with custom methods that were written just for it. These will be listed on the tile shown when you click to add an object as well as at the top of the method tab.
  2. Naming Conventions
    Each programming language has its rules for names variables, objects, classes and other things in programs. To keep things easy to understand, there are some conventions that should be followed as well. 
    Meaningful Names
    Alice assigns names to objects for you but they reflect what the object is not what it is going to be doing or represents in your program.  So it is common to replace them. Rabbit1 and Rabbit2 might not be as good as whiteRabbit and redRabbit.  Methods should be changed as well to make them more descriptive as well.  Things like hareAnimation might describe the movements of a hare object.
    Spaces
    Alice, unlike other program languages, allows spaces. Since we will learn standard programming, we will not be using them.
    camelC ase Names
    Since we will not be using spaces, to make something like whiterabbit a little easier to read, we capitalize the second and any other words to make it whiteRabbit. This is known as camelCase as the capitalization puts a look similar to a camel’s hump. We will use this for object and method names.
    Class Names and PascalCase
    Capitalizing the 1st word is known as PascalCase and is used to name classes.
    Summary of Our Naming Coventions
    1. We will give objects and methods meaningful names
    2. We will use letters and numbers only
    3. We will not use spaces
    4. camelCase for objects and methods
    5. PascalCase is used for classes
    Renaming Objects
    To rename objects in Alice, right click on it and choose rename and type in the new name.
    Renaming the my first Method
    Select the world object the use the procedure above to give it a new name.
  3. Designing a Program
    Programs require planning so programmers use what is known as the Program Development Cycle as follows:
    1. Design the program
    2. Write the methods
    3. Test the methods
    4. Debug the methods
    5. go back to #1 till it works right

    Designing the Program (the Alice World)
    The first step in the design is to clearly state exactly what the program is to do.  Once you know, you can then break it into the steps that will need to be done to achieve this. This can be done with pseudocode, a line by line description of the program is to be done.  It can also be done with a flow chart, a pictorial representation of the steps.
    Writing the Methods
    Now the designs you created can be translated to the programming language you are using. Usually this will require a lot more detail. 
    Testing and Debugging
    Now that you have written the method, run it. You want to see if it:
    1. does intended task
    If not you will need to fix it till it does
    2. have any logic problems giving bad results
    Logic problems are when it does what it is supposed to do but the results are not what is expected. You need to find out why and fix it if you have them (and many programs will).
    3. works efficiently with no steps that are not needed
    [rest of this section is tutorial on Alice]
  4. Comments
    All programming languages support comments, the use of which is to help another programmer or yourself understand what is going on in a program. In Alice, look on the bar at the bottom of the editor for a tile that has a // on it.  Drag this up to where you want to put comments will put a tile with ‘no comments’ on it. Click on it, then ‘other’ and enter your comments. Comments will not affect execution of program as it is ignored by the Alice interpreter.
  5. Tips for setting Up an Initial Scene
    Using Primitive Methods to Set Up a Scene
    We have so far adjusted the scene to look like we wanted it to. We can use primitive methods to set them precisely with out having them be part of the program. Right click on the object and choose methods. A list of the methods will appear on the screen. Use these to position the items where you want them to be.
    Using Primitive Methods to Position a Character's Arms
    In place of using the mouse to position a character's arms the primitive methods can be used in a similar way to what is described above. In the arms you would choose leftUpperArm and do the motion roll it the direction you want and put in the distance. Small numbers would be good to start out with (book recommends 0.2)
    Moving an Object to the Center of the World
    When doing a primitive move command, it is possible to move one object to the center of another object. By moving one object to the center of the world object, that item will now be in the center of the world.
    Positioning Objects a Specific Distance Apart
    If you want two objects a specific distance apart, then position them so that they are centered on each other using the primitive methods and then again, doing the same, move them the specific distance and direction that you want them apart.
    Moving the Camera to an Object
    Right click on the object and choose ‘camera get a good look at this’.
  6. Executing Instructions Simultaneously
    Usually instructions are done one at a time. If you want to do two at the same time, choose the ‘do together’ tile from the bottom of the editor and drag that up. Now put the instructions that you want done at the same time in the box that appears. This box creates what is known as a structure.
    Timing and the DO together Structure
    Each object in the DO TOGETHER structure will last as long as you tell it to. If you want them to end at the same time you need to adjust timing so that they will.
    The DO in order Structure
    In order for instructions inside a do together structure to be able to take place in a semi-individual manor, you would use the DO in order structure. Now instructions placed in this will happen in sequence in side the together structure.
  7. Exporting Your Code For Printing
    Alice allows for you to print out your code to look at hardcopy of it but to do so you must first use the file menu and ‘export to html’. This file can now be opened in a browser and reviewed or printed.

No comments:

Post a Comment