cs 101 - problem solving and structured programming lab 3 ...blerner/cs101/labs/lab3/lab3.pdfcs 101...

5
CS 101 - Problem Solving and Structured Programming Lab 3 - If Statements, Loops, Lists in Alice Due: September 30/October 1 Pre-lab Preparation Before coming to lab, you are expected to have: Read Adams, Chapter 4 and chapter 5, skipping section 5.2. Used Alice to do the bouncing ball example in the book in Section 4.4.4. Used Alice to do the bee example in the book in Section 5.1.1. Introduction to the Assignment Here is the English description of the world you will create and program for parts 1 and 2 of this lab assignment: Several people watch a parade. The parade begins with soldiers marching neatly in line. There are 9 soldiers arranged in 3 lines. Behind the soldiers comes a ragtag bunch who don’t seem to have the marching down so well. They are easily distracted, start and stop at the wrong times, walk into each other or even wander off the street. Here are some snapshots from the animation. You can see a movie of the lab on the sched- ule page of the course website. Initially, we see just the neatly lined up soldiers. 1

Upload: others

Post on 05-Jul-2020

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS 101 - Problem Solving and Structured Programming Lab 3 ...blerner/cs101/Labs/Lab3/Lab3.pdfCS 101 - Problem Solving and Structured Programming Lab 3 - If Statements, Loops, Lists

CS 101 - Problem Solving and Structured ProgrammingLab 3 - If Statements, Loops, Lists in AliceDue: September 30/October 1

Pre-lab PreparationBefore coming to lab, you are expected to have:

• Read Adams, Chapter 4 and chapter 5, skipping section 5.2.• Used Alice to do the bouncing ball example in the book in Section 4.4.4.• Used Alice to do the bee example in the book in Section 5.1.1.

Introduction to the AssignmentHere is the English description of the world you will create and program for parts 1 and 2 of this lab assignment:

Several people watch a parade. The parade begins with soldiers marching neatly in line. There are 9 soldiers arranged in 3 lines.

Behind the soldiers comes a ragtag bunch who don’t seem to have the marching down so well. They are easily distracted, start and stop at the wrong times, walk into each other or even wander off the street.

Here are some snapshots from the animation. You can see a movie of the lab on the sched-ule page of the course website.

Initially, we see just the neatly lined up soldiers.

1

Page 2: CS 101 - Problem Solving and Structured Programming Lab 3 ...blerner/cs101/Labs/Lab3/Lab3.pdfCS 101 - Problem Solving and Structured Programming Lab 3 - If Statements, Loops, Lists

Soon the disorganized marchers come into view.

After a while, the soldiers stop marching. The others don’t notice and walk right up to the soldiers before stopping.

2

Page 3: CS 101 - Problem Solving and Structured Programming Lab 3 ...blerner/cs101/Labs/Lab3/Lab3.pdfCS 101 - Problem Solving and Structured Programming Lab 3 - If Statements, Loops, Lists

Here is a view down the street as we watch the last, very distracted marcher go past.

Step 1: Getting 1 Soldier to MarchTo get started, create the basics of your parade scene consisting just of the road and one soldier. (Save adding details of buildings and spectators until the end.) The Toy Soldier can be found in the People category. Add the soldier and orient him to march down the road. You will need to make the road larger to get the scaling reasonable.

Define a soldierMarch method in the World object. You can define this method similarly to the march method presented in the book in Section 2.2.2. Be sure to place this method in the World object, though, rather than the Toy Soldier object because we will want to use the same method for all the soldiers we create.

At this point, the soldierMarch method should only cause the soldier to take 2 steps: 1 with the left foot and 1 with the right foot. Add a parameter to the soldierMarch method called numSteps. This will be a number that determines how many steps the soldier takes.

Put a for loop inside the soldierMarch method that counts up to numSteps. Move all the code that does the marching to be inside the for loop. Change the call to soldierMarch so that the sol-dier takes 3 steps with each foot.

Step 2: Creating a List of Soldiers who march togetherOne soldier does not make for a very interesting parade. Add 8 more soldiers, lining them up carefully in 3 rows.

Add a variable to “my first method” named soldiers. Make it be a list of objects. Add the sol-diers to the list. (You need to hit return, not just click on the item to add in the menu that comes up.)

Now, let’s get all the soldiers to march together. Use a forAllTogether statement, selecting sol-diers as the list to use. What we want to do inside the forAllTogether loop is call the soldier-March method to cause each soldier to march. However, currently the soldierMarch method only causes the first soldier to march. To fix this, add a parameter to the soldierMarch method that

3

Page 4: CS 101 - Problem Solving and Structured Programming Lab 3 ...blerner/cs101/Labs/Lab3/Lab3.pdfCS 101 - Problem Solving and Structured Programming Lab 3 - If Statements, Loops, Lists

you can use to specify which soldier to march. Now, what you would like to do is replace uses of toySoldier in the soldierMarch method with the parameter. This will work for the calls to the move method, but you will run into problems with the methods involved in turning the soldier’s arms and legs. They may currently say something like:

toySoldier.leftLeg.turn (FORWARD, .08 revolutions);

In this case, you can’t replace toySoldier with your parameter. You have to replace “toySoldier.leftLeg” with something that refers to the left leg of the soldier passed in to the pa-rameter. The solution is to declare a variable in the soldierMarch method named legOnLeft. Then, set this value to the left leg of the soldier parameter. Assuming the soldier parameter is called whichSoldier, you will need a statement that looks like this:

legOnLeft.set (value, whichSoldier.partNamed (leftLeg));

This gets the part of the soldier that is called leftLeg and saves it in a variable named legOnLeft. Now, you can change the earlier turn statement so that it says:

legOnLeft.turn (FORWARD, .08 revolutions);

You will need to do the same thing for the right leg and both arms.

Go back to “my first method” and change the call to soldierMarch that is inside the forAllTogether statement to pass item_from_soldiers as the argument.

Play your animation. If it working right, you should see all of the soldiers marching in unison.

Step 3: Adding other marchersMost hometown parades look a bit less organized than our well-trained soldiers. Add some more marchers.

Be creative here. The only additional requirements are these:

• The marchers should all look different.• The marchers should all have different march methods. A few Alice characters come with

a walk method. Feel free to use those as long as you still meet the other requirements listed here.

• The marchers should not be synchronized• Include an if statement somewhere. For example, in my animation, the skater girl’s

march method has a numSteps parameter, like the soldier’s march method does. If the skater girl is taking her first step, she looks to the side. If she is taking her last step, she turns her head forward again.

• Include a while loop somewhere. For example, my soldiers stop walking after 4 steps, but while the coach has not gotten really close to the last line of soldiers, the other marchers keep marching.

Step 4: Timing detailsAs we have seen, a “do together” statement allows multiple instructions to be done at the same time. For example, it is what we used to allow Alice and the white rabbit to jump simultane-ously. “do together” works very well when each of the actions in the “do together” take the same amount of time.

4

Page 5: CS 101 - Problem Solving and Structured Programming Lab 3 ...blerner/cs101/Labs/Lab3/Lab3.pdfCS 101 - Problem Solving and Structured Programming Lab 3 - If Statements, Loops, Lists

When the instructions in a “do together” take different amounts of time, it works less well. In that case, the instructions start at the same time, but end at different times. Whichever charac-ter is done first, will need to wait until the second character completes its action.

Because of this, you may find that some of your characters stop marching while others continue marching because the march methods for different characters take different amounts of time. To solve this problem, first look at each method and determine how long it takes. Remember that each instruction takes 1 second unless you explicitly change its duration. A “do together” in-struction will take as long as the slowest action inside it. Add up the durations of the instructions in each march method to determine how long it takes for each character to march. With this in-formation, you can then adjust the durations of the instructions inside the method so that each march method takes the same amount of time.

Step 5: Finishing touchesThe last thing that you should do is add a few more objects to make the animation more inter-esting, such as buildings and spectators. These objects do not need to be involved in any of the animation methods. Please also use at least one additional camera view or pan the camera dur-ing the animation to follow the marchers.

Grading

10 Step 1: Getting 1 soldier to march

10 Step 2: Creating a list of soldiers who march together.

10 Step 3: Adding other marchers

10 Step 4: Timing details

5 Step 5: Finishing touches

5 Comments

Turning in Your WorkGo to ella. Click on COMSC 101 in the toolbar menu across the top. Then click on Assignments in the left column. Click the submit link for this assignment. Click the Add Attachment button. Use the Browse… button to upload a local file. Click Continue. Then click the Submit button.

5