SSEA Newbies Handout 09 Summer 2012 August 17 th, 2012 Assignment 4: Bouncing Ball and Bizz Bazz Buzz For the next week (from today until next Thursday), you ll be implementing a pair of programs that make use of your recently acquired programming skills. The first program works to implement a bouncing ball animation, and the second helps use get better at a game called Bizz Bazz Buzz. The bouncing ball application is complex, but still short enough that you shouldn t feel any pressure to decompose and define your own helper methods. The second, however, in substantial enough that you should definitely decompose and define your own methods. We ll work in class today and on Tuesday to ensure that you re outfitted to succeed in building programs that benefit from these new decomposition techniques. Due: Thursday, August 23 rd at 4:00 p.m. Problem 1: Bouncing Ball Implement a GraphicsProgram that bounces a ball within the boundaries of the graphics window. Your program should begin by placing a red circle (50 pixels in diameter) at the center of the graphics window. Every 10 milliseconds, your program should shift the position of the ball by dx and dy pixels, where both dx and dy initially have the value of one. Whenever the leading edge of the ball touches one of the window boundaries, your program should make the ball bounce by negating the value of dx and dy, as appropriate. (If you re interested, you might have the ball change colors every time it bounces maybe have it rotate through red, orange, magenta, black, green, blue, gray, and cyan.) The bouncing ball s itinerary would take it from the center of the screen in a southeasterly direction. It would eventually bounce and head northeast until it bounced off the eastern wall. The first few seconds of the application would see the bouncing ball trace through the path outlined in the following snapshot. The ball would travel from the center (shown as the checkered ball) through the dashed line to the be-bricked ball and continue on from there.
2 Don t worry about figuring out how to get the program to stop. For this exercise, you should use a while (true) loop so that the program just keeps on bouncing the ball until you close the window. There is one GOval method you should use to shift the location of the oval to a new spot: setlocation. setlocation takes a new x and y value that defines the new upper left coordinate of the rectangle circumscribing the oval. Consider this code snippet: public void run() { GOval circle = new GOval(0, 0, 10, 10); add(circle); pause(1000); circle.setlocation(50, 50); } The above would create a small circle that when added would hug the upper left corner of the canvas. The circle would sit there for a full second (or 1000 milliseconds) before being relocated 50 pixels to the right and 50 pixels down. In particular, don t create a new GOval with each iteration just move the original one around.
3 Problem 2: Bizz Bazz Buzz Bizz Bazz Buzz is a multiple player game where everyone sits in a circle ala duck-duckgoose and counts off consecutive numbers in a round robin manner. One person says 1, the person next to him says 2, the person next to her says 3, and so on in one big, merry loop. Play continues until someone either takes too long or makes a mistake, and the person who messes up leaves the circle and play resumes with those who remain. Now, counting off numbers is fairly easy easy enough that even a circle of drunken imbeciles could play for a very long time. The twist: whenever you get to a number that s a multiple of 3, 5, or 7 and/or includes any one of those three digits, you don t say the number itself, but you say something else. The rules outlined below dictate what you should say instead: If the number is a multiple of 3 and/or the digit 3 appears one or more times, then you don t say the number. Instead, you say the word "bizz" if it s a multiple of three, and you say "bizz" for every 3 that appears. For example: o You wouldn t say 13. You d say "bizz", because 13 contains a single 3. o You wouldn t say 233. You d instead say "bizz bizz", because 3 appears twice. o You wouldn t say 333. You d actually say "bizz bizz bizz bizz, because it s a multiple of 3, and because the digit 3 appears three times. 1 + 3 equals 4. Fun! If the number is a multiple of 5 and/or the digit 5 appears one or more times, then you don t say the number; rather, you say something involving the word "bazz". 5 is to "bazz" as 3 is to "bizz". For example: o You wouldn t say 52. No, you d say "bazz, because a single 5 appears as the first digit. o You wouldn t say 25. You d say "bazz bazz" instead. 25 is a multiple of 5, and the digit 5 appears exactly one time. o You wouldn t say 55. It s actually "bazz bazz bazz". If the number is a multiple of 3 and 5, and/or both digits appear, you say something involving a combination of "bizz" and "bazz". o You wouldn t say 35. It s a multiple of 5, and it contains a 3 and also a 5. So you d say "bizz bazz bazz". The same goes for the digit 7, except it s paired with the word "buzz". o 77 isn t 77. It s actually "buzz buzz buzz". It s a multiple of 7. It has two 7s. Hence, you say buzz" three times. Woo.
o 105 isn t 105. It s really "bizz bazz bazz buzz". 105 equals 3 times 5 times 7, and there s a 5 in there! Four words! Those numbers having nothing to do with 3s, 5s, and 7s are printed as is: 2, 8, 29, and 44 are examples. The Program The program is conceptually straightforward: You need to write a program that generates what should be said for the numbers 1 through 100 during a game of Bizz Bazz Buzz. The program output should look something like this: Here's a program that'll help you master the game of Bizz Bazz Buzz!! ---------------------------- 1.) 1 2.) 2 3.) bizz bizz 4.) 4 5.) bazz bazz 6.) bizz 7.) buzz buzz 8.) 8 9.) bizz 10.) bazz 11.) 11 12.) bizz 13.) bizz 14.) buzz 15.) bizz bazz bazz output for 16 through 94 has been excluded from handout, just to save paper 95.) bazz bazz 96.) bizz 97.) buzz 98.) buzz 99.) bizz 100.) bazz The Approach I ve provided you with starter code, which includes the implementation of a method called countoccurrences. At the moment, the run method includes some calls to the countoccurrences method to demonstrate that it works as intended. Once you get a chance to run the sample program a few times (look at the Newbies web site for a demo) and look over the code I give you, you ll want to start hacking. Here s how I want you to proceed: 1. Introduce a new method called countnumtimestosay, which has the following prototype: 4
private int countnumtimestosay(int number, int digit) { 5 Implement countnumtimestosay to return the number of times a word should appear in what gets printed on behalf of the specified number, either because the digit appears in number one or more times, number is a multiple of digit, or both. Your implementation should call of the countoccurrences method, because the number that countnumtimestosay needs to return is always equal to or just one more than whatever countoccurrences returns. Note that you don t actually pass the word (i.e. "bizz" or "bazz"), because the word itself doesn t influence what gets returned. Once you ve implemented countnumtimestosay, change the last half of the run method. Get rid of the lines that test countoccurrences, and replace it with similar code that tests countnumtimestosay. It s completely up to you what you write here, but you might include these if it ll help you get started. println("number of bizzes in 133: " + countnumtimestosay(133, 3)); println("number of buzzes in 7217:" + countnumtimestosay(7217, 7)); The truly ambitious programmer will write a good number of these to ensure the countnumtimestosay method works the way we need it to. 2. Making use of the countnumtimestosay method you just wrote, introduce a method called printnumber, which has the following prototype: private void printnumber(int number) { The method decides whether the supplied number should be printed as is, or if it should be printed as a combination of bizzes, bazzes, and/or buzzes. It also prints whatever should be printed. Once you have an implementation you suspect it pretty good, go ahead and erase the code that tested countnumtimestosay and replace it with code that tests printnumber. 3. Finally, remove all test code from the run method, and add a simple for loop that prints out the numbers 1 through 100, using the printnumber method. There s a lot on your plate, but with your smarts and your drive to succeed, I am super duper confident you ll finish both of these programs and live to brag about it.