CSC 171
Homework 5

Assigned March 9, due March 18

Design Recipe

For each programming problem below, you should follow a 6-step recipe:

  1. Contracts: Choose names and data types as necessary for the problem at hand.

  2. Write test cases: a sequence of things you plan to do, and what effect each one should have, in order to confirm that it works correctly.

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

    1. write a skeleton. For a class, this is the word "class", the name of the class, perhaps an "extends" clause, and a pair of curly-braces. For a method, this is an access specifier (like "public"), the return type, the method name, a pair of parentheses enclosing a parameter list, and a pair of curly-braces.
    2. if you're writing a method, write down a list of available ingredients: any parameters to the method, as well as the implicit parameter "this", along with their data types. Then, if some of them are class objects, start listing their parts, e.g. "this.x", "this.y", etc.
    3. fill in the details until the method does what it's supposed to.

  4. Proofread the code, looking for misspellings, mismatched braces, missing semicolons, etc.

  5. Compile the code. If there are syntax error messages, log them for future reference, fix them, and return to the "Proofread" step above, until there are no error messages.

  6. Test the code. If any of your tests produce answers different from what you expected, log what happened for future reference, fix the error, and return to the "Proofread" step above, until everything works the way you expect.

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. Copy the files SetupWorld.java, TickBuggle.java, and KnightBuggle.java to your own directory. Open BlueJ or DrJava and load these files, together with the usual BuggleWorld.java program. Compile them, and test with new SetupWorld(). This should produce a perfectly normal BuggleWorld with Bagels placed at various positions; to make up your own test cases, you can adjust these positions by modifying SetupWorld.java.


  2. Create a KnightBuggle and invoke the method tick() on it. It should make a "knight's move", i.e. two forward and one to the left. Now invoke ticks(5) on it; it should do the same thing 5 times. Next, try tickForever() on it; it should keep making knight's moves until it hits a wall and produces an error message. Examine the source code for KnightBuggle; note that it merely needs to redefine the method tick(), and all these other things work automagically. Try changing the tick() method to do something different; confirm that ticks(int) and tickForever() still work.


  3. Define a new class named BagelToggleBuggle, extending TickBuggle. This kind of Buggle likes to walk around, picking up Bagels when it sees them and dropping Bagels wherever there isn't one already.
    Write a tick() method that checks whether there is currently a Bagel in the square; if so, pick it up, and if not, drop one. In either case, move forward to the next square. Test this by using ticks(int) and tickForever().


  4. Define a new class named CountingBuggle, extending TickBuggle. A CountingBuggle has a private instance variable named bagels which keeps track of how many Bagels it has picked up. This variable should be initialized to 0 when the Buggle is born.
    Write an access method named getBagels() which simply returns the value of the bagels variable.
    Write a tick() method which checks whether there's a Bagel in the square; if so, it picks it up and adds one to the number of Bagels. Test this with ticks(int) or tickForever(); use the access method to check how many Bagels bart has picked up.


  5. The Buggles have decided to go into the painting business. Only a few Buggles have the artistic talent to decide where the paint should go, and their time is expensive, so they don't actually do the painting themselves. Instead, they leave Bagels wherever they want the paint to start or stop, and give a lowly PaintingBuggle the job of painting between the Bagels. Define a new class named PaintingBuggle, extending TickBuggle, with a tick() method that acts as follows: whenever it finds a Bagel it picks it up and alternates between painting and not painting (i.e. if the brush was up, it goes down, and if it was down, it goes up). See the illustration at right; note that bart started painting at the location of the first Bagel, stopped just before the second Bagel, started again at the location of the third Bagel, and stopped just before the fourth Bagel.

    Hint: The tick() method will be shorter and simpler if you write another method for the common task of switching between painting and not painting, i.e. if the brush is down, pick it up, and if it's up, put it down. I recommend making this method private, because it's used only from inside the PaintingBuggle class.


  6. Once in a while, a Buggle develops wanderlust and decides to explore the world by following some simple rule. For example, one might decide to "go forward until you find a Bagel, then turn left. Keep doing this until you hit a wall.". Define a kind of TickBuggle named WanderingBuggle whose tick() method does this. Hint: Here again, you might find it helpful to write a private method to do a common task.

Writing an Applet

  1. Copy the file mousing.zip to your directory, unzip it, open the resulting project in BlueJ, compile both classes, and run SampleMouseApplet. It should give you a white screen in which, every time you click the mouse, a red dot appears.


  2. Define a class Circle that represents a circle on the screen. It should have the following methods:

    • access methods getCenter() and getRadius()
    • toString()
    • draw(Graphics2D g2) to draw the circle on the screen
    • contains(Point thePoint) which returns true if the point is inside the circle, otherwise false

    Modify the SampleMouseApplet so that it draws a blue Circle with center (110, 120) and radius 100, and whenever the mouse is clicked, it draws a dot there which is green if the point is inside the circle and red otherwise.

CodeLab:
Do the exercises in the section entitled "CH 5: Decisions"

Last-minute change, March 18

Increase the size of the dots to a diameter of 20 pixels, and make sure that wherever you click the mouse becomes the center of a dot, not above and to the left of a dot (or anything else like that).

Grading standards


What to turn in

Problem Contracts Test cases Java Code
BagelToggleBuggle with tick() method /5 /5 /10
CountingBuggle with tick() method /5 /5 /10
PaintingBuggle with tick() method /10 /5 /15
WanderingBuggle with tick() method /10 /5 /15
Circle class /10 /10 /20
Modified SampleMouseApplet /10 /10 /20
Error log, overall /30
Total: /210

Last modified:
Stephen Bloch / sbloch@adelphi.edu