CPSC 111 2009/10 Winter Term 2, Assignment 3: Beasts
Contents: Intro |
Description |
Design |
Download/API |
Setting Up Your Project |
Reflection Questions |
Deliverables
Intro
Out: Wed Mar 31 2010
Due: Fri Apr 16 2010, 5:00pm, by electronic handin
Value: 4% of final grade (subject to instructor discretion)
Objectives:
- to practice using arrays
- to practice creating classes that implement interfaces
- to practice using interfaces
- to read, understand, and extend an existing code base
- to add functionality to a larger program in manageable pieces
- to test your additions
Description
Beasts are graphical objects that move around an onscreen window,
showing their mood by their color. Your job is to control their
behavior. Beasts are controlled by their brains, which set their
direction of motion and mood. The behavior of a beast can also be
influenced by the members of its pack.
Your mission is to control the beasts by implementing three
interfaces:
- The IBeastBrain interface requires you to create classes that
control how Beasts behave by choosing a mood and direction. A BeastMood is
described by intensity and grooviness. Intensity ranges from 0 to
100, and grooviness ranges from -100 (sad) to +100 (happy). A
direction is specified by an angle in degrees or radians.
For instance, a behavior could be to move in the direction of a sine
wave, with intensity depending on whether the Beast was moving
upwards or downwards. Another behavior would be moving perpendicular
to the pack leader, with high grooviness if you're on the left half
of the screen and low grooviness if you're on the right.
- The IPackFinder interface requires you to create classes that
return a single leader Beast and an array of Beasts that are members
of the pack. The behavior of a BeastBrain may depend on some
computation involving its leader and/or pack.
For instance, a pack could be all of the groovy beasts, or all the
nearby beasts, or all the beasts travelling in a certain direction
that are not groovy.
- The IBeastProvider interface requires you to make a class that
provides an array of Beast objects.
You are provided with example classes that implement these interfaces.
The DumbBrain class is a very simple IBeastBrain that moves in a
straight line, with no mood changes. The RandomWalkingBrain is a
slightly smarter IBeastBrain that needs to access information about
the Beast's state, as stored in the BeastInfo class. It chooses its
direction randomly, and its intensity gets lower as it ages. The
AllBeastsPack is a very simple IPackFinder that returns all created
Beasts as being pack members. The DumbSetup class is a simple
IBeastProvider class that returns an array of newly created Beasts to
the BeastSimulationProgram driver class in its setup method, so that
they can be added to the scene.
The requirements of this assignment are to create many more classes
that implement these interfaces. Specifically:
- IBeastBrain:
- SpinnyBrain directs the beast to spin around in circles, and
leaves the mood unchanged. The pack is ignored.
- FollowerBrain directs the beast towards the pack leader, and
its mood is the same grooviness and half the intensity of its
leader.
- GroupieBrain directs the beast toward the pack
leader, and its mood becomes more groovy the closer it gets,
reaching maximum grooviness when it is 100 pixels or closer to
the leader. Its intensity decreases with the number of other
Beasts in its pack, reaching 0 when there are 5 or more others.
- FlockBrain directs the beast towards the center of
its pack. Its mood becomes groovier when it is close to other
pack members, with an intensity that depends on the number of
Beasts in the pack. If there are 10 or more pack members, it
reaches maximum intensity. If there are no other Beasts in its
pack, it wanders aimlessly by picking a random direction and its
mood is neutral (intensity and grooviness both set to the middle
of the possible range).
- YourBrain is up to you! Design something interesting
and fun, and implement it. Make sure to include comments
explaining the desired behavior of your brain, so that we can
understand whether you've succeeded. Feel free to post English
descriptions of the behaviors of YourBrain to WebCT, but do
not post either code or English descriptions of how to
implement the design.
- IPackFinder:
- GlumPackFinder returns the pack of all the Beasts with
grooviness below 0. The most ungroovy Beast in the pack is the leader.
- NearbyIntensePackFinder returns the pack of all Beasts
within 200 pixels. The leader is the most intense Beast in the
pack.
- CongaPackFinder returns the pack of all beasts
provided by the IBeastProvider. However, it returns a different
pack leader for each beast: it returns the next beast in the
array (wrapping around to the 0th beast for the leader of the
last beast). If the beast passed to getLeader is not in the
array, getLeader should return null.
- YourPackFinder is up to you! Design something interesting
and fun, and implement it. Make sure to include comments
explaining the desired behavior of your pack, so that we can
understand whether you've succeeded. Feel free to post English
descriptions of the behaviors of YourPackFinder to WebCT, but do
not post either code or English descriptions of how to
implement the design.
- IBeastProvider:
- You will need to create setup classes to test your
BeastBrains and PackFinders. To call these different setup
classes, you will need to change the setup method of
BeastSimulationProgram. You must submit appropriate IBeastProviders
to test all of the code that you write. Feel free to post your
IBeastProviders on WebCT, to share ideas on what's fun to make.
- BeastManager is an example of a complex IBeastProvider that
keeps track of all the Beasts so that they can be drawn by the
main driver class, BeastSimulationProgram. Note that you do not need to
change this class at all, and your IBeastProviders are likely
to be much simpler than this one.
Beasts can, and often do, move out of the visible area of the window.
If those descriptions above sound vague, it's because they are! For
instance, how tight should the SpinnyBeast's circles be? It's up to
you. As long as your beasts look right in the way they behave, and
your documentation shows how you've fulfilled the specification,
you'll get full marks. So make sure you comment your code
carefully to justify your choices.
Be careful of what assumptions you make when writing beast brains and
pack finders. For example, remember that you may be dealing with an
empty pack containing no beasts, or that there might be no leader.
Your code should be robust and do something reasonable in these cases,
not crash! Again, document your design choices.
As usual, some bonus points are available for going above and beyond
the call of duty, by implementing more or particularly impressive
brains and packs.
We have provided a framework for a working Beast package. You are
required to create all the new classes listed above, implementing
the three interfaces for brains, packs, and setup. The only code
that you will need to change in the existing framework is the setup
method in the BeastSimulationProgram, marked with "TODO".
Here's a diagram of the three interfaces, with placeholders like
"YOURBrains" for the classes that you need to write:
The full framework is:
All code that you add must be clear, easily readable, and documented
with JavaDoc. Note that it is unlikely that the comments provided for
an interface's methods are sufficient documentation for the methods of
a class that implements it, so don't just copy them!
Any significant or potentially confusing parts of methods should be
documented; code that is reused in different parts of a class should
be abstracted into a method; and methods that become overly long or
complex should be broken down into simpler methods.
Remember to explain in your comments all design decisions you made!
Download the framework: Beasts.zip
Includes these source files (no need to download separately):
BeastSimulationProgram.java,
AllBeastsPack.java,
Beast.java,
BeastInfo.java,
BeastManager.java,
BeastMood.java,
Direction.java,
DumbBrain.java,
DumbSetup.java,
IBeastBrain.java,
IBeastProvider.java,
IPackFinder.java,
RandomWalkingBrain.java
API documentation for Beast package
:
Optional Information (not needed for this assignment): If you're
curious about how we implemented the graphical display, feel free to
browse the documentation for the ACM
packages. If you'd like to read about the design rationale for
that set of packages, see the discussion from the ACM Java Task
Force.
Setting Up Your Project
Setting up in Eclipse: Start by downloading the file
Beasts.zip to
your computer. Next, open Eclipse, and make a new Project named
Beasts. Right-click on the src folder, and
select ImportArchive file
as
the import source and browse to the Beasts.zip
file that you just downloaded. For the Into folder
,
choose Beasts and hit Finish
.
Now, your project contains all the necessary code, but the
acm.jar file is not
on your classpath. Right-click on the
Beasts
project and select
Properties
. In the menu on the left, select Java
Build Path
. Select the Libraries
tab on the
right. Click Add Jars...
. You should see
Beasts
in the dialog box that pops up.
When you double-click on it, you should see the jar file
acm.jar
. Select it and hit OK
.
You should now be ready to solve the assignment in Eclipse.
Setting up on the command line: Start by downloading
the file Beasts.zip to
whatever directory on your computer you'd like to work from. Next,
unzip the file to that directory. (The Home Suite Home CD and the lab
computers have FilZip for unzipping files; you may have another
application.)
To compile from the directory where you installed your code, use:
javac -classpath .;acm.jar *.java
To run from the directory where you installed your code, use:
java -classpath .;acm.jar BeastSimulatorProgram
Note the dot at the start of the classpath arguments! That's crucial
so Java knows to look in the current directory for classes and files.
On some computers, including Macs, you may need to use :
s rather than
;
s in the commands above.
- Start by implementing SpinnyBrain, using the DumbBrain class as a
base. This is the simplest Brain to create, since it does not take
the pack into account at all. You now have a new class that implements the
IBeastBrain interface!
- Then implement a MySetup in order to test the SpinnyBrain,
using the DumbSetup as a base. Change the
setup
method of the
BeastSimulationProgram class to call MySetup instead of
DumbSetup. You now have a new class that implements the IBeastProvider
interface.
- Next, implement the FollowerBrain.
- Adapt your MySetup tester class to add some FollowerBrains to the set
of beasts in your world, to test this class.
- Implement your GroupieBrain, building on what you did for the
FollowerBrain.
- Adapt your tester/setup class to test GroupieBrain beasts as well.
- Next, implement FlockBrain. Make sure to test with flocks of
different size, to make sure that the specified behavior that depends
on flocks size is correct.
- Adapt your tester class to include FlockBrain beasts. Again, be sure to
test with flocks of different sizes, including size 0. You may want
to create multiple different tester classes, instead of continuing to
adapt just one.
- Now, try implementing the GlumPackFinder, the simplest class that
should implement the IPackFinder interface.
- Adapt your tester class (or create a new one) to test
GlumPackFinder. You will need to consider what kind of Beasts are
best to use for testing the packfinder. You might want to choose
specific values for grooviness rather than relying on random ones, to
make testing easier.
- Now implement the NearbyIntensePackFinder.
- Adapt your tester class (or create a new one) to test this new
packfinder. Again, setting specific values, for example initial location and
intensity for your beasts, will help you test things more quickly.
- Implement the CongaPackFinder. It's a good idea to save this one
for last, because it is the most complex one.
- Create (or adapt) a tester class to test this packfinder.
- Now that you have experience with Brains and PackFinders, come up with
an interesting specification for YourBrain and YourPackFinder.
- Implement YourBrain, and YourPackFinder.
- Create setup/tester classes to test these two final classes.
Reflection Questions
Be sure to answer these reflection questions after you
finish the assignment. There are no right answers to these questions, and we
don't expect more than a couple of sentences for each
question. However, we will grade on your effort to answer
throughtfully.
- Describe YourBrain and YourPackFinder. What nifty things can they do?
- Now that you know about inheritance, how could you have used inheritance
in this project to make your program easier to write or more powerful.
- If Java didn't have polymorphism at all, how would you have done this project?
Are interfaces and inheritance worth the complexity they add to the language?
- OPTIONAL: What was especially interesting or
frustrating about this part of the assignment? What could we have done
to make it better for you?
Note: You should include your answers to the
reflection questions in a separate text file from
your code named README.txt. Please do not submit your questions
in any format other than plain text. (Note that neither Microsoft Word
nor PDF is plain text! Whatever editor you use to write your source
code should be usable to write a plain text README.txt file.)
You should answer the reflection questions for all parts of an
assignment in the same README.txt file.
Deliverables
- Source Header: Please be sure to include the following comment statement at the
top of each source file that you write in this assignment, plus your README.txt file:
/*
Authors:
UNIX login IDs:
Student numbers:
Date:
By submitting this file, we acknowledge that the persons whose names
appear above are the only authors of this code except as acknowledged in
the code below.
*/
- What To Submit:
- the source files for all your new classes that implement the
IBeastBrain, IPackFinder, and IBeastProvider interfaces.
- the source file for the BeastSimulatorProgram.java file
- the answers to the reflection questions, in the text file README.txt. README.txt should also contain any other comments you have on your submission (notes about extra files, extensions you wrote, known bugs in the code, etc.)
- Electronic Submission:
- Working in
Teams:
You may work with at most one other person. That person must
also be enrolled
in CPSC 111 this term. If you are working with a partner, the
two of you must
submit only one assignment. Keep in mind that when you work
in pairs, each
of you must understand the work that you submit.
- Late Penalty:
The following late penalties will be applied if your work
is not submitted on time:
- 10% deduction if your work is up to 24 hours late
- 30% deduction if your work is up to 48 hours late
- 100% deduction if your work is more than 48 hours
late
Please note that weekends and holidays are counted!
- A Word of Advice:
The demand for our labs may be extremely
high on the due date of an assignment. To avoid last minute
headaches, start early and plan to submit your assignment at
least one day before it is due!
Last modified: 2010/03/28