1.2.4.a AnalyzingAProgram
1.2.4.a AnalyzingAProgram
4 Analyzing a Program
Now that you have a basic feel for App inventor, its time
to explore it more deeply through the lens of our eight
roles of variables. Remember that these roles are: fixed
value, most recent, accumulator, aggregator,
stepper, walker, best-so-far, and one-way flag.
This activity includes complete source code for an AI2
App. You are provided with this code at six different
stages so that you can see how complexity can be added to
a program over time.
Introduction
Materials
Procedure
1. Form pairs as directed by your teacher. Meet or greet each other to practice
professional skills.
As there is little "driving" to be done in this activity, you may wish to have the
code open on both computers to make navigating and analyzing the code easier.
Part I: First Look at New Code
2. Follow teacher instructions to place bounceBall.aia on your computer. Most
likely your teacher will have you download the program from the online course
materials.
3. Using Google Chrome, navigate to http://ai2.appinventor.mit.edu/ . Log in to
your Google account, making sure that the sign in screen is using the https
protocol from the google.com domain.
4. Choose Continue to move beyond the splashscreen. Choose Project > Import
project (.aia) from my computer. Browse to select your computers copy of
bounceBall.aia.
5. Open the program by selecting its name in the projects view.
6. Once the program opens in the GUI Designer view, choose Blocks to switch to the
Blocks editor.
7. Compare the behavior of the app on your Android device to what you see on the
Blocks screen in App Inventor. Analyze the program following these steps:
a. There are many global variables in this program. Each one is created by a
single block as shown below. A global variable can be used in any part of the
program. How many global variables are there?
c. The other large chunks are event handlers. These tell the program what to
do when the user or program causes an event, like a mouse click, to occur.
The event is described after the . in the name of the block. Look through
the code and fill in the names of the events that are being handled by the
chunks of code shown below.
1.
8. Choose one block of code that is accounting for some behavior. Discuss with your
partner what parts of the block you think are accounting for the aspects of that
behavior.
2.
Role
Purpose
TO_FLI
Role
Purpose
MAX_BALLS
11. Load bounceBallStage3.aia in the block editor. As in previous steps, identify the
newly introduced variables roles and explain their purpose.
This stage in the code introduces pseudorandom numbers. Pseudorandom
numbers are the random numbers generated by a computer program. Although
they are unpredictable because they are constructed from the computers
memory using parts of memory unrelated to the program, they are not actually
random. We perceive pseudorandom numbers as being random because App
Inventor takes care of seeding the sequence by picking an unrelated location in
memory using the system clock.
Variable Name
currentColor
Role
Purpose
colorsClicked
12.Load bounceBallStage4.aia and repeat for the new variables listed below:
This stage introduces using a procedure to break up code into more
manageable and logically arranged chunks. A procedure is a separate piece of
2014 Project Lead The Way, Inc.
Computer Science Principles Activity 1.2.4 Analyzing a Program Page 4
code that can be called upon from multiple locations in your code. This procedure
handles the displaying of scores once the game is finished.
Padding is a common name for blank space placed around the elements of a
display, and these variable names incorporate this term.
Variable
Name
TITLE_PAD_X
Role
Purpose
TITLE_PAD_Y
Role
Purpose
RED_CHANNEL
GREEN_CHANNEL
BLUE_CHANNEL
SCORE_PAD_Y
SCORE_PAD_X
blueValue
greenValue
redValue
14.Open the supplemental activity document "1.2.4.Aa Blocks.docx". The steps of the
pattern associated with each role are shown below. Choose a variable that serves
each role. Record the line numbers from the final program that demonstrate each
step for each role.
a. Fixed Value:
i. Assign value:
ii. Use value:
b. Most Recent:
i. Assign value:
ii. Use value:
c. Stepper:
i. Initialize value
ii. Increment value
d. Walker:
i. Assign element from collection
ii. Use element
e. Accumulator:
i. Initialize
ii. Accumulate
iii. Use accumulation
f. Aggregator:
i. Initialize
ii. Aggregate
iii. Use aggregation
Part III. Extension (Optional):
15. Adding in variables that take on the roles of Best-so-far and One-way flag will
certainly make the program a little more engaging, but they also add a significant
amount of complexity to the code for the program. If you have extra time, you
should open bounceBallStage6.aia in the blocks editor and fill in the table
below. In addition, complete parts a. and b. as you did in the last step.
Variable Name
Role
Purpose
colorRed
colorGreen
colorBlue
splitColor
currentRed
mostRedYet
sufficientRed
sufficientRedSeen
a. Best-so-far:
i. Initialize to impossibly bad
ii. Check for record breaker
iii. Set new record
iv. Use overall record
b. One-way flag:
i. Clear flag
ii. Check for condition
iii. Raise flag
iv. Check if flag ever raised
Conclusion
App Inventor 2 and Scratch are two different programming languages. They are similar
in look and feel. The roles of variables that you have learned in these two languages will
appear again in any computer language.
1. For each of the eight roles of variables, write about an app that you use or that
you would like to make that could or would need to use a variable to fulfill that
role. You might be able to think of one program or app that uses all 8 roles for
variables. Explain how each role will play an important role in your app.