470 Appendix A
Additional Lab Exercises
General Programming Techniques
Solution 1—Apple Bagger
Figure A-1 shows one solution to the Apple Bagger exercise.
Figure A-1. Apple Bagger, Solution 1
Key Points
n Optimal Solutions: To optimize the performance of programs, use fewer
objects, if possible. This solution uses six objects. The program could
also be implemented with 10 objects, as Figure A-2 shows.
n Until Break and Break Objects: Use these objects for loops that require
testing a condition. In this example, the loop should stop when the total
weight of the apples is greater than 10 pounds.
n Accumulator: Use the
Accumulator to keep a running total.
n Counter: Use the
Counter to keep a running count. In this example, the
Counter is used to track the total number of apples in the basket. Note
that when the total weight is over 10, only the
Then pin fires on the
If/Then/Else object giving the correct answer in the Counter.