For each programming problem below, you should follow the same 6-step recipe as in previous assignments:
Contracts
Write test cases
Write the Java code. This step can be broken down into
write a skeleton.
if you're writing a method, write down a list of available ingredients
fill in the details
Proofread the code.
Compile the code.
Test the code.
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.
Start with your Buggle programs from homework 5.
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.
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.
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
.
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.
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.
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.
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.
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.
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.
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.)
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.)
Start with your completed MouseApplet from homework 5. Make a copy of it before going on, so you still have the original.
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
.
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.
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.
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.
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.
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.)
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?
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:
Your modified TickBuggle.java
(with two versions of
the tickToWall
method: one with no parameters and one with
an int parameter).
Your modified CountingBuggle.java
(with methods
countToWall
and dropAllBagels
).
Your class AddingBuggle.java
(with method
addRows
).
Extra credit: your class MultiplyingBuggle.java
(with method multiplyRows
).
Describe (in words or with a screen-shot) a
situation in which tickToWall
on a
WanderingBuggle
will never return.
A BlueJ project for the "scattering random dots"
problem. The project folder will probably contain at least three or four
classes, including MouseApplet
,
SampleMouseApplet
, and Circle
.
Another BlueJ project for the "escape from the circle" problem. This one, too, will probably contain at least three or four classes.
Development logs. Contracts (names & types) may be in the program file itself, or in a separate log file. Test cases may be in JUnit testing classes, or in the program file itself, or in a separate log file. (For interaction-dependent programs like applets, I suggest writing a "script" that tells what steps you plan to take in testing the applet, and what you expect it to do in response to each step.) Errors may be recorded in a text file, or by using PSP.
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 |