Lab 4.4

advertisement
Lab 4.4
Iteration and Branches
Chapter 18 and 21
Setting up the Assignment
Before doing this Lab, make sure you view the Iteration&Branches PowerPoint slide show.
This lab, and your assignment for the week, is to complete the exercises below by filling in code inside
the file called Lab4.4IterationBranch.htm, which appears as web links beside the Lab4.4 link on the
CSIS1 web page. These exercises will take you through the some activities using loops and branches to
repeat and select different code segments.
1. Right click on the link Lab4.4IterationBranch.htm which is on the CSIS1 web page. Then
Choose File>Save Target As and save the file either on your U: drive (on campus) or My
Documents folder (at home).
2. Make sure turtle.js is in the same folder, downloading if necessary.
3. Find the file where you saved it, then double click on it to open it in Internet Explorer. You
should get a blank web page. Choose View>Source to bring up the same file in notepad for
editing.
4. Before starting, make sure you enable debugging:
a) In Internet Explorer, click Tools>Internet Options
b) Click on the Advanced tab
c) Click the box to "Display a notification about every script error"
5. Also, in Notepad,
a) click Format and make sure Word Wrap is NOT checked AND
b) click Format>Font and select Courier New for the font
6. To activate each Exercise, simply put one more / in front of /*-----in other words,
//*------------When you’re finished with an exercise, remove the extra / to deactivate it.
To begin the exercises, notice the definition of the functions square, triangle, jumpTo, jumpFwd,
jumpBwd and randColor in the <head> section of the file. You won’t need to write any more functions
for this assignment unless you want to.
Exercise 1: Using a for loop, repeat the commands
draw a circle.
forward(1); right(1);
enough times to
Exercise 2: Using a for loop, repeat any combination of the commands
jumpFwd, right, and
square
10 times to draw 10 squares in different locations and/or angles
1
Exercise 3: Do not activate this exercise, just experiment with color and line width in your solution
for exercise 2.
a) Check the "Hex Color Code Pallete" link off the CSIS 1 web page to pick a color.
b) Set Background color by modifying the HTML <body> tag already at the top of the file to say
<body bgcolor="#XXXXXX"> where XXXXXX is your hex code
c) Set line thickness before your Exercise 2 solution using
width(X); where X is a number between 5 and 20
d) set line color before your Exercise 2 solution using
color("#YYYYYY"); where "YYYYYY" is another hex color code.
Exercise 4: Draw 100 squares in random locations and with random sizes by repeating the following
statements in a for loop:
randX=rand(-400,400);
randY=rand(-300,200);
jumpTo(randX, randY);
size=rand(10,100);
square(size);
Exercise 5: Don’t activate this exercise, just change line color and line width before your solution to
Exercise 4.
Exercise 6: Don’t activate this exercise, just modify your solution to 4 to randomly choose between
triangles and squares. Simply replace the line in Exercise 4 that says:
square(size);
with the following statements (carefully!)
choice=rand(1,2);
if (choice == 1)
square(size);
if (choice == 2)
triangle(size);
Exercise 7: Don’t activate this exercise, just modify your solution to 4 to select a random color by
inserting the statement randColor(); before the line that says choice=rand(1,2);
Exercise 8: Develop a solution for one of the following images using a loop and repeated calls to the
appropriate functions. Make a screen capture of your final solution and staple it to your code printout.
a)
b)
c)
d)
2
Download