CIS 101

Lab 4.1

IMPORTANT!!! Alice files that you create can only be opened by the Alice program, you cannot just double click on the Alice file you saved to open it!

Alice v2.0 © 1999-2014, Carnegie Mellon University. All rights reserved.

In the next four labs you will learn some fundamentals of object-oriented programming using the Alice environment. It “should” be fun and hopefully will spark your interest in what is involved in creating computer programs. Although computer programming has existed in its modern form for half a century, it still eludes all but a small fraction of society. While programming is an inherently difficult activity, there are currently many barriers, both mechanical and sociological, that prevent large portions of the population from learning to program a computer. Alice addresses both the mechanical and sociological barriers that currently prevent many students from successfully learning to program a computer. Alice addresses the mechanical barriers to programming by making it much easier for students to create programs. Rather than having to correctly type commands according to obscure rules of syntax, students drag-and-drop words in a direct manipulation interface. This user interface ensures that programs are always well-formed. Alice is available for free at the following URL: www.alice.org. If you want to work on these labs at home, you will need to download the zip file. Version 2.4 would be most like what is in our labs. When you unzip it, it will create a folder containing the Alice program. One more thing; as with any application, you will be saving files. Make sure you remember WHERE you are saving your files and give your files names that make sense to you. Alice will give your files names, but they are not very descriptive. Let’s get started:

Step 1:

MAKE SURE YOU PLUG IN YOUR USB DRIVE NOW! Your USB drive must be plugged in before you start Alice!! Also, Alice works better in a 1024X768 video resolution mode. Right click on the desktop, click screen resolution (or properties depending on your Windows version) from the pop-up menu, click the settings tab and using the slider, change the resolution to 1024X768 if it is not already at that setting.

Start the Alice program. In our labs, Alice is in the programs menu. When the program opens, there will be a “Welcome” window on top of the main window. Click on the “Templates” tab. Click on the “grass” environment and then click open.

Step 2:

We need an object to “program.” A program is a series of instructions. You deal with human programs all the time. When you follow a recipe, read someone’s directions for how to get to their house, or follow assembly instructions for something, you are executing a program of sorts. A computer is also an “object” that can be programmed. Actually there are already three objects in this world. Look at the “Object Tree” in the upper left area of the screen.

You can see that there is a camera (it’s what lets us see the world), a light (so we can see the world) and the ground. The sky is not actually an object. In the World View window, there is an Add Objects button. Click it.

Step 3:

You should now see a larger version of the World as well as an objects gallery at the bottom of the screen. Click the ANIMALS group so we can add an animal to our world. Using the horizontal scroll bar at the bottom of the screen, scroll to the right until you see the ladybug. Click on and drag the ladybug to the World window. When you have placed the ladybug where you would like it, release the mouse button. Now click the folder icon that has a bent arrow in it so we can go back up one level in the Gallery and choose another object to add.

This time find the Nature gallery and add a palm tree to the world and put it anywhere you like. We are finished adding objects so press the Done button on the right hand side of the screen.

Step 4:

We are now going to add a series of instructions to the method called “my first method.” The Program Code Editor (PCE) is the large yellow pane in the lower right quadrant of the Alice environment. Notice that right now, it says Do Nothing. Programming is usually done in the following order. The programmer designs the program on paper. This is usually the longest part of a programmer’s job. If the programmer develops a good plan on paper first, the rest of the task will be much easier. After developing and testing the plan on paper, the programmer begins to code the program. The programmer chooses an appropriate “programming language” which can depend on several variables. The coding process is a matter of the programmer typing instructions into an editor. This creates what is called “source code.” The CPU of the computer does not understand this source code however. The source code must then be translated into “machine language.” This is the binary form of the instructions that the CPU does understand. This translation process is most often called “compiling.” Once the source code has been compiled, the programmer can run and test the program. In the Alice environment, life is much easier. We don’t have to type much of anything. This avoids a common area of frustration for the beginning programmer. Using Java, C++, FORTRAN or one of the other commonly used high level languages, if one word is misspelled, a semicolon is left out or any number of tiny errors is made, the compiler cannot turn the source code into machine language. In Alice, we “drag” instructions into the PCE. Being good programmers, we will start with a plan. We want the ladybug to fly to the top of the palm tree and then back to us. Here is the plan:

Ladybug turns to face the palm tree

Ladybug moves to leaf1 of the palm tree

Ladybug turns to face the camera

Ladybug moves forward ?? meters

Ladybug moves down 3.66 meters

Click on the ladybug in the object tree, then click on the methods tab at the top of the object details pane.

Methods are things an object can do. There are several methods you can do, such as walk, speak, write, blink, etc. Sometimes methods have what are called parameters, other information the object needs before it can perform the method. For example, if I told you to walk, you would also need to know how far, so the distance would be a parameter I would add. I might say Homer walk 5 meters. Homer already knows how to walk. The actual information--5 meters-- would be referred to as an argument telling him how far.

Click and drag the tile to the PCE where it says Do Nothing (once you add this tile, the Do Nothing will disappear). You may have to scroll down the methods pane to find that tile. On the menu that pops up (the parameter menu), point to palm tree which will cause another menu to pop up. On this menu, click on the entire palm tree.

IMPORTANT NOTE: When you drag a tile to the PCE window, if the tile is outlined in red, you are trying to drop it in a place where it cannot be dropped. Make sure the tile is outlined in green before you release the mouse button.

Before we go on, let’s see if that worked. Click the Play button in the upper left area of the screen. A new window should pop up and you should see your ladybug turn to face the tree. You can restart as many times as you wish. Click the stop button when you get bored.

Step 5:

The next two should be easy, just drag the tile and drop it below the turn to face instruction and select leaf1 of the palm tree from the parameter menus. Then drag the tile and drop it below the move to instruction selecting the camera as the object it should face from the parameter menus. Keep in mind, the computer follows the instructions in the exact order we place them, so the sequence of steps in a computer program is VERY important.

Step 6:

Drag and drop the tile and choose forward 1 meter from the parameter menus. We will probably have to go back and modify that after testing. Drag and drop another tile and choose down 3.66 meters. Notice that 3.66 is not one of the options. You will have to choose other… from the menu and then click 3 . 6 6 on the calculator.

Step 7:

TESTING

Play your movie. Does it do what you expected?? Depending on where you placed the palm tree, the lady bug probably does not move close to you at the end. Adjust the value of how far the ladybug moves forward by pulling down the amount parameter menu and choosing some other value. Keep trying until the ladybug lands pretty much at the bottom of the world window.

Step 8:

Let’s make one more modification. Did you notice that the ladybug flies straight towards the camera, and then moves straight down? Not very ladybug-like is it? It would be better if she were flying down and towards us at the same time. Alice lets us do that with a Do Together block.

Drag the tile (from the bottom of the PCE) into the PCE and drop it between the face the camera and move forward instructions. Did you notice the green line that shows where the tile will be dropped as you move it around? Now drag the last two instructions into the Do Nothing part of the Do together block. Again, keep in mind, the Do Nothing will disappear when you add the first instruction. Test your program and see how it looks. Better?

This is what your method should look like except for the move forward part of the Do together block. Your palm tree may not have been so far away.

I hope you enjoyed your first experience with programming. You are now ready to try something on your own.

WHAT YOU WILL TURN IN:

1. Create a new movie: (NOTE: if you still have your ladybug movie open, you will need to save it using the File menu and then choose New World from the File menu. Pick any environment you would like.)

a. add a circle (it’s in the shape library)to your environment.

b. add three animals. Pick any three you like.

c. make one animal(1) turn to face the circle, (2)move to the center of the circle and then (3)MOVE down into the circle

d. make the two other animals turn to face the circle at the same time

e. make the two other animals move to the circle at the same time.

f. make the other two animals go down into the circle at the same time

g. print out your source code* and turn it in when you show me your ladybug movie in class.

*TO PRINT SOURCE CODE:

1. From the File menu, choose Export Code for Printing

2. Fill in your name.

3. Alice will then create a web page containing your code. Make sure you notice where Alice is going to save the exported code. You probably want to save it in the same folder where you saved your program.

4. Open the web page created in step 3 and print it.