CSC 171
Homework 6

Assigned March 22, due April 13

Design Recipe

For each programming problem below, you should follow the same 6-step recipe as in previous assignments:

  1. Contracts

  2. Write test cases

  3. Write the Java code. This step can be broken down into

    1. write a skeleton.

    2. if you're writing a method, write down a list of available ingredients

    3. fill in the details

  4. Proofread the code.

  5. Compile the code.

  6. Test the code.

Error logging

I want a log of everything that went wrong in the development of your programs. You may do this on paper or in a text file, as in homework 2, or using the on-line PSP forms.

Programming assignments

Buggle problems
  1. Start with your Buggle programs from homework 5.

  2. Modify the BagelToggleBuggle class by adding a new method named tickToWall which keeps walking forward until it reaches a wall (use the built-in method isFacingWall() to detect this), picking up bagels where they are and dropping them where they're not.

  3. Modify the CountingBuggle class by adding a new method named tickToWall which keeps walking forward until it reaches a wall, counting how many bagels it picks up along the way.

  4. If you've written tickToWall properly, you should notice that the two tickToWall methods are identical. (If not, figure out how to write them identically before going on!) Writing the same thing twice is a waste of your time. So to prevent ever having to write it again, modify the TickBuggle class with a tickToWall method. You should now be able to remove it from BagelToggleBuggle and CountingBuggle, and it'll still work on them because they're kinds of TickBuggle. Furthermore, tickToWall should also work on KnightBuggle, PaintingBuggle, and WanderingBuggle.

  5. Modify the CountingBuggle class by adding a method countToWall which walks forward to the wall, counting bagels and picking them up, then returns to where it started (you may assume that it started with its back to a wall; for extra credit, drop this assumption) and returns the number of bagels.

  6. Modify the CountingBuggle class by adding a method dropAllBagels which walks forward, dropping a bagel in each square, until it runs out of bagels or runs into a wall, and then returns to where it started (you may assume that it started with its back to a wall; for extra credit, drop this assumption). Note: if it has more bagels than will fit between here and the wall, it should drop all it can, then return to its starting point with the remaining bagels.

  7. Write a new class AddingBuggle with a method addRows that works as follows: starting with its back to a wall, it walks forward to the next wall, counting bagels and picking them up, then returns to where it started. Then it moves one square to the left and does the same thing again in this new row. Then it moves one square to the left and drops the total number of bagels it's picked up in those two rows (if there's room for them). Hint: You should be able to write this method in about ten lines.

  8. Extra credit: write a new class MultiplyingBuggle with a method multiplyRows that works as above, picking up all the bagels in the first and second rows, but in the third row it drops a number of bagels found by multiplying the number in the first row by the number in the second row.

  9. So much for CountingBuggles; let's go back to WanderingBuggles. With KnightBuggle, BagelToggleBuggle, CountingBuggle, and PaintingBuggle, you could always be sure that tickToWall would eventually stop, because the tick method always moves forward so eventually it'll reach a wall. It's not so obvious, however, that a WanderingBuggle is guaranteed to eventually stop wandering. Construct a situation in which tickToWall on a WanderingBuggle never returns. (Test this to make sure; if it hasn't returned within a few seconds, you can be pretty sure it's "gone infinite". If you successfully get your Buggle into an infinite loop, you can stop things (in DrJava) by clicking the "Reset" button, or (in BlueJ) by choosing "View -> Show Debugger" and clicking the "Terminate" button.

    To avoid wandering forever (and never getting home for dinner), the sensible Buggle puts a time limit on his/her wandering. (Come to think of it, perhaps all TickBuggles should know how to do this.) Write another version of the tickToWall method in TickBuggle which takes in an int parameter indicating how many steps to walk (at most). It works exactly like tickToWall, except that if it hasn't found a wall within that number of steps, it stops. Note that if you put this method in TickBuggle, not only WanderingBuggles but all other kinds of TickBuggles will immediately know how to put a time limit on their ticking.

An extra-credit Buggle problem

Add to your WritingBuggle class from homework 4 a method named drawMessage which takes in a String, a Color, and two ints, and draws each of the characters in the String in the specified color with the specified width and height. You may assume that the only characters in the String are 'c', 's', '1', and '7', but you may not assume what order they're in, or how many characters there are. You may assume that all the letters will fit in the width of the BuggleWorld, so you don't need to check for walking through walls.


Writing an Applet

In this assignment, we'll modify your completed MouseApplet from homework 5 so that it scatters a bunch of dots randomly over the screen, with the ones inside the circle being green and the ones outside the circle green. (The example at right is 100 dots.)

  1. Read about the java.util.Random class in the Java class libraries documentation (accessible from the Help menu of BlueJ, among other places). Try some examples interactively. (If you're using DrJava, just create a Random object, with no "seed", and invoke the nextInt methods a couple of times on it in the interactions window. If you're using BlueJ, use the "Evaluate Expression" dialogue to create a new java.util.Random() object, then "get" it, and it'll appear on the object bench; then try the nextInt methods a couple of times on it.)

  2. Start with your completed MouseApplet from homework 5. Make a copy of it before going on, so you still have the original.

  3. If you haven't already, you might want to write a Dot class which "knows" its own location and color, and knows how to draw itself; rewrite the applet's paint method so it uses Dot rather than creating its own Ellipse2D.

  4. Add to your applet class a method named randomPoint which uses the built-in Applet methods getWidth and getHeight to construct a random Point somewhere inside the bounds of the Applet.

    Alternate approach: you might prefer to write a randomDot method that returns a randomly-located Dot.

    Hint: in either case, you may want to first write and test a similar method that takes in two ints, maxX and maxY, and constructs a random Point whose x coordinate is from 0 to maxX, and whose y coordinate is from 0 to maxY.

  5. Add to your applet class an instance variable numPoints; your paint method should blank the screen, then use a for-loop to create and draw exactly that many dots at random, making each one either green or red depending on whether it's inside or outside the circle. To test this, initialize numPoints to 1, compile and run the applet and confirm that exactly one point was drawn; change the initial value to 5, compile and run and confirm that 5 points were drawn; etc.

  6. All that initializing and recompiling is a pain. Modify your mouseClicked method so that it ignores where the mouse was clicked, but brings up a dialog box (using JOptionPane.showInputDialog) to ask the user how many points to plot.


Writing another Applet

In this problem, you'll again modify your applet from homework 5: you'll click somewhere inside the circle, specify how fast a dot is supposed to move, and it'll move that way until it escapes from the circle.

  1. Start with your completed MouseApplet from homework 5. Make a copy of it before going on, so you still have the original.
  2. Add two double instance variables dx and dy (or xSpeed and ySpeed, or something like that). Modify your mouseClicked method so it brings up two dialog boxes (using JOptionPane.showInputDialog) to ask the user for numeric values of these two variables. (Use the built-in Double.parseDouble method described on p. 111.)
  3. Modify your paint method so that it starts at wherever the user clicked the mouse, and repeatedly draws a dot and moves it by dx and dy (you may want to use the built-in Point method translate), as long as the point is inside the circle. (You may want to set your dot size down to 1 or 2, so you can see what's going on better.) What should happen if the user clicks the mouse outside the circle?
  4. Modify your paint method so that it keeps a count of how many dots it drew before escaping, and draws this number (using g2.drawString) at the dot's last recorded location.

CodeLab:

Do the exercises in the section entitled "CH 6: Iteration"


What to turn in

Grading standards

Problem Contracts Test cases Java Code
TickBuggle tickToWall() method /5 /5 /10
TickBuggle tickToWall(int) method /5 /5 /10
CountingBuggle countToWall() method /5 /5 /10
CountingBuggle dropAllBagels() method /5 /5 /10
AddingBuggle addRows() method /5 /5 /10
XC: MultiplyingBuggle multiplyRows() method /5 /5 /10
WanderingBuggle looping scenario /10
XC: WritingBuggle drawMessage method /5 /5 /10
Scattering Dots
   randomPoint or randomDot
/5 /5 /10
   paint method /5 /5 /10
   mouseClicked method /5 /5 /10
Escape from Circle applet
  mouseClicked method
/5 /5 /10
  paint method /5 /5 /10
Error log, overall /40
Total: /250

Last modified:
Stephen Bloch / sbloch@adelphi.edu