CSC 171
Homework assignment 8

Fall, 1998

Buggles that Count

Download the hw8Buggles folder from my download directory on panther.  It should contain

Buggles learn to say numbers

Our friendly Buggles have learned (some of) their ABC's, and are now ready for a beginning (say, first-grade) math class.  But they have a problem: whenever the teacher calls on one of them, they can't answer because they don't have mouths so they can't speak.  What Buggles are really good at is painting, so our Buggles will answer questions in math class by painting various numbers of squares: if the correct answer is 3, they'll paint three squares, and so on.  Eventually we'll teach them to count how many bagels are ahead of them and "say" the answer by painting the appropriate number of squares.

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.) [picture with 3 painted squares, Buggle in lower left corner]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.

Buggles learn to count bagels

Your next task is to teach the Buggles to count bagels.  More precisely, we want a Buggle to figure out how many bagels are between it and the next wall, not including the square the Buggle is on right now.  Write a method named "countBagelsAhead" to do this.  Write a contract and an example in Java code of how the method would be called.  Of course, the correct answer will depend on what bagels are on the screen where, so you'll need to draw out some examples on paper and decide what you think the correct answer should be in each case.  The method header is already provided for you.  Be sure your method has no visible side effects: no painting, no moving the Buggle, no picking up or dropping bagels, etc.
Hint: The only way we've seen to count something this semester was countNovels, a method which called itself on "the rest" of a pile of books.  We can do something similar here, except that countBagelsAhead will call itself on "the rest" of the row.  Unfortunately, we don't have a variable for "the rest" of the row; what we'll do instead is have the Buggle take a step forward, find out how many bagels are ahead of it NOW, then look down to see whether it's over a bagel, then determine from this how many bagels WERE ahead of it before it took a step forward.  Of course, you want to leave the Buggle where it was before, so you'll need to take a step back before returning the answer.
Here are some examples of correct runs.





 

Extra Credit

If you get all of the above examples to work correctly, you've probably satisfied the requirements of this assignment.  But if you want to try adding some more features, I suggest any or all of the following:
  • [7 bagels sprinkled across bottom row, incl. left corner; 6 bagels at left end of second row]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.



  • Last modified: Wed Dec 2 2:00 PM EST 1998
    Stephen Bloch / sbloch@boethius.adelphi.edu