B CUSP 110B – Digital Thinking Homework 9: Tests
and Iterations |
|
Goal: The purpose of this exercise get comfortable with simple if-statements (tests), and begin investigation of iterations.
Here is a bee dropping circles program we will be developing (download, unzip, and double click on the .exe file). Notice:
· The right thumb stick (arrow keys) moves the bee.
· Try clicking:
o Button-A (K-Key): black-white bullseye centered at the bee location!
o Button-B (L-Key): Horizontal circles (right direction) from the bee location
o Button-X (J-Key): Vertical circles (up direction) from the bee location
o Button-Y (I-Key): Diagonal circles (up-right direction) from the bee location
· Notice: bottom echo shows how many clicks has been pressed. Notice, the program does not quit unless you press the Back-Button (F1-Key).
Download this source code, unzip, compile and run. Now, run the program to investigate its behavior. Remember what we have learned when examine a given source code:
· Find/read/understand the declaration of all variables
· Find/read/understand the initialization of the variables: InitializeWorld()
· Find/read/understand how the variables are updated: UpdateWorld()
Part 1.Step 1: Notice that the bee is moving way too slowly. How can you modify the given code to increase the bee’s movement by 10 times?
Part 1.Step 2:
Notice that if you press K or L for more than 5 times, the program quits. This
is rather annoying. Please fix this bad behavior.
Part 1.Step 3:
Notice that when you click K and L keys, the clickCount is updated, but the
update is not happening when you click I/J keys. Please add in code to count
the number of times when I and J keys are clicked.
Part 1.Step 4:
Take a very close look at the behavior of K-key:
·
UpdateWorld()
· Notice we are using the bee.Center as the argument for the center creation parameter! We are saying let’s create a circle where the bee is located!
Now, take a look at how we created bullseye from the lecture. Replace the code here to create a 6 circle bullseye. Remember, we need to loop backwards from bigger circle to the smallest circle. The circles should have radii and color of:
· i=5: radius = 10 + 5*5; color=(100+20*5, 100+20*5, 100+20*5)
· i=4: radius = 10 + 4*5; color=(100+20*4, 100+20*4, 100+20*4)
or saying …
· radius = 10 + i*5; color=(100+20*i, 100+20*i, 100+20*i)
Your code will looks something like:
Submit your ClassExample.cs file, bring a hardcopy (Remember to write your name!!) to class on Wednesday to submit.
Take a look at the behavior when Button-B is clicked, in particular, notice how we use RandomInt().
Part 2.Step 1:
Now, look at the iteration under Button-A (K-Key) and replace the “5” by a
random integer between 5 to 10:
Now when you run your program and press the K-Key, how many
circles do you see? Do you understand why?
Part 2.Step 2:
Implement the Button-B behavior (horizontal circles) by creating a random
number of circles between 2 and 10 at the current location of the bee.
Part 2.Step 3:
Implement the Button-X behavior (vertical circles) by creating a random number
of circles between 2 and 10 at the current location of the bee.
Part 2.Step 4:
Implement the Button-Y behavior (diagonal circles) by creating a random number
of circles between 2 and 10 at the current location of the bee. It is ok if your circles have small gaps in
between them. Extra credit: how can
you make the circles barely touch each other (like the same solution)?
Submit your ClassExample.cs file (combination of Part1 and
Part2), bring a hardcopy (Remember to write your name!!) to class on next
Monday to submit.
In this assignment you have practice (again) that the steps to examine a given source code by:
· Running the program to understand the behavior
· Examine variable declarations
· Examine the initialization of the variables
· Understand how the variables are changed and relating the changes to what you see when running the programs.
In addition, you have:
· Worked with conditional statements (if statements),
· Simple iterations
· Using the iteration counter to parameterize variables: changing color using i
· Iterations with parameterized loop conditions: using RandomInt() in the for loop