For each programming problem below, you should follow a 6-step recipe:
Contracts: Choose names and data types as necessary for the problem at hand.
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.
Write the Java code. This step can be broken down into
Proofread the code, looking for misspellings, mismatched braces, missing semicolons, etc.
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.
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.
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
.
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.
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()
.
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.
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.
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.
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.
Define a class Circle
that
represents a circle on the screen. It should have the following
methods:
getCenter()
and
getRadius()
toString()
draw(Graphics2D g2)
to draw the circle on the
screencontains(Point thePoint)
which returns true if
the point is inside the circle, otherwise falseSampleMouseApplet
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.
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).
BagelToggleBuggle.java
, and if you've made up your own
test cases for it, the modified (or extended)
SetupWorld.java
with your test cases.CountingBuggle.java
, and if you've made up your own
test cases for it, the modified (or extended)
SetupWorld.java
with your test cases.PaintingBuggle.java
, and if you've made up your own
test cases for it, the modified (or extended)
SetupWorld.java
with your test cases.WanderingBuggle.java
, and if you've made up your own
test cases for it, the modified (or extended)
SetupWorld.java
with your test cases.Circle.java
, and if you've written test cases for it
as a JUnit, the associated CircleTest.java
. SampleMouseApplet.java
. Development logs. Contracts (names & types) and test cases may be in the program file itself (for example, the test cases for an applet or a Buggle problem would be a script, perhaps in comments: I'll do this, and I expect the computer to do that). Errors may be recorded in a text file, on paper, or using PSP.
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 |