Your first task is to write a method named "say" which allows
a CountBuggle to "say" a number by painting that number of squares in a
row. Be sure to write a contract and an adequate list of examples
before trying to write the method; the method header is provided for you
in the file Count_BuggleWorld.java. Make sure also that
your "say" method has no side effects other than painting a couple of squares:
in particular, it should leave the Buggle in the same position and direction
as it was before.
To test your "say" method, note that the "run" method in Count_BuggleWorld.java
already creates a CountBuggle, asks it how many bagels are ahead of it,
and then tells it to "say" that number. The "countBagelsAhead" method
is currently a dummy, returning the number 0, and for now we can use it
as a convenient way to test "say": if countBagelsAhead returns 0, your
Buggle will attempt to "say(0)". Compile, run, and see whether this
works correctly. (Ignore any bagels that may be on the screen; we're
not concerned with them yet.)
If
it works, change countBagelsAhead to return 1, 3, or some other number
that you want to test; compile, run, and see whether it works correctly.
(At right is a picture of a correctly-working "say(3)".)Don't go on to
the next step until "say" works correctly on all your test cases.
Say
it with Bagels: Change your "say" method so that rather than painting
a specified number of squares in a row, it drops a specified number of
bagels in a row. To avoid confusing the answer with the question,
I suggest taking a step to the left (i.e. into the second row) before dropping
all these bagels. In the example at right, there are 7 bagels in
the bottom row, with spaces in between them. One of them is under
the original position of the Buggle, so it doesn't count. The Buggle
has counted the remaining 6, stepped into the second row, and dropped 6
bagels in a row.
Compare and
Subtract: Suppose you have a bunch of bagels sprinkled throughout the
bottom two rows. Count how many are in the first row, step left,
count how many are in the second row, step left, subtract one number from
the other, and "say" the answer. Of course, Buggles don't know about
negative numbers yet (they're only in first grade), so if the first row
has more bagels than the second, say the difference in green, and if the
second row has more bagels than the first, say the difference in red.
(If you want to try this, you'll need some bagels sprinkled in the second
row. This requires modifying the BagelWorld.java file. If you
can figure this out for yourself, more power to you; otherwise, feel free
to ask me for help.)
Presti-Digit-ation:
Modify your "say" method so that, rather than painting a stripe of a specified
number of squares, it paints a digit ("0", "1", "2", etc.) in a 3x5 rectangle,
as we did back in the CSC171_BuggleWorld assignment. You'll presumably
want to write ten methods named "write0", "write1", etc. and have "say"
simply choose among them. This is a good opportunity to try the "switch"
statement discussed in chapter 6 of the Arnow & Weiss book.