Intro:
This class is not designed to teach you the easiest ways to be a Java
programmer. Instead, it is designed to give you a complete understanding
of a programming language and to teach you concepts that apply to many other
programming languages.
The ArrayList class mentioned in the book provides many shortcuts that may
hamper understanding. For this reason, you are NOT
ALLOWED TO USED THE ArrayList CLASS for assignments or exams.
However, it is an excellent class that you may wish to use outside of this
class. I feel that there is many good concepts illustrated by the creation
and use of this class. Thus this assignment focuses on writing a class
very similar to the ArrayList (ParticleList) and a class that uses the
ParticleList (FireWorksController).
I am giving you a bunch of classes in miniC.jar. In addition to that I am
providing you with two java files: Particle.java and FireworksApp.java.
When you are finished, your program should display a window. Every time
you click the mouse, it should make a fire work where you click the mouse.
It should look a lot like
this.
To get started, download miniC.jar and add it to the Java Build Path of your
project just like you did with all the other .jar files in this class.
Also download Particle.java and FireworksApp.java and put them where your source
code goes. FireworksApp.java won't compile if you haven't created the
FireworksController class.
A Particle is a colored spot that can move itself and draw itself. I am
providing the Particle class for you since it has a bit of math and
pseudo-physics. Instead of putting it in miniC.jar, I am giving you
Particle.java so you can play with it and improve it if you'd like to.
I recommend taking a look at the code so you understand what is happening.
The code is not very complicated and it's well commented.
The ParticleList is just what it sounds like--a list of particles.
Question: Why are we writing a class that is a list of
Particles? why not just use Java's built in Particle array?
Answer: Java arrays can't change size, so it's a little difficult to add
and remove elements. The ParticleList puts all that code in one
place. In fact, if you modified this class slightly, you could use it to
hold anything. Then you could have a substitute for arrays that is more
powerful. That is essentially what an ArrayList is (which you cannot use
for this course).
The key operations are accessing elements, getting the size, adding and removing
elements. There are a few other methods which you can find described in
the javadoc. When writing it, keep in mind that if someone provides bad
input to your code, it should not crash. If the try to remove a
non-existent element, the code should ignore the request. If they call
get() with an invalid index, it should return null. You may decide what to
do if call add() and pass in null as a parameter.
You can right the class how you'd like, but you may want to consider these
hints:
You will need to process many Particles, so you really need an array of the
appropriate type.
You don't know how many Particles will be added to the list. It might
be three, it might be 500, and it might be 2 million. Thus the size of
the array should grow if there are no spots left. There are good
theoretical reasons (which you can ask me about) that each time you increase
the size of you array you make it twice as big every time you resize
it. You will lose points if you just create a large array and hope
nobody ever adds too many Particles.
It's inefficient to shrink an array when you remove elements.
If you are writing your array class well, your array can be longer than the
number of elements in the list. You will then need a way to tell what
is actually in the array and what is not. The easiest way to do it is
with an extra data member. A harder, less efficient way is to see what
is null.
The size method should return the number of elements in the list, not the
number of spaces in your array.
You may wish to use the isValidIndex() predicate method in some of your
other methods.
I strongly recommend (but do not require) writing a private method resize()
that increases the size of the underlying array when necessary.
You won't be graded much for the efficiency of your code. Most of the
grade will center around whether it is correct. However, it will be much, much
easier for both of us if you right your code in the right way.
You will also write the FireworksController class. This class is
responsible for making sure that the particles that make up the fireworks are
created, drawn, moved and removed correctly.
In addition to the constructor, there are three methods--makeFirework(),
paintAll(), and moveAll() . In order, they create, draw and move the
fireworks.
The FireworksWindow class, which I'm providing you with in miniC.jar will
determine when each of these need to happen and will call the methods
appropriately. It might be easiest if you don't worry about when these
methods are called--just make sure they do the right thing. However, if
you are curious, everything is drawn, then moved, then drawn, then moved over
and over very quickly. This produces the illusion of a moving
picture. The makeFirework() method is only called when the mouse is
clicked.
Here are some hints:
You'll need to control many, many Particles. You will need to add,
remove and access them over and over.
When the mouse is clicked, you should create a bunch of Particles going in
random directions at random speeds. Exactly how many particles and how
fast is up to you. The many Particles in each firework should be about
the same color.
You add Particles when you click. You should remove Particles at some point
to. Other wise, your computer will get slower and slower as it tries
to process the increasing number of Particles. I will allow you to
decide how to remove particles. I'll give you a few ideas. You
can remove them randomly (perhaps around 1% to 10% each turn). You can
remove them when the reach the bottom of the window (the window is 600
pixels high). You can remove them after they have been moved a certain
number of times (perhaps around 30 to 100). You will lose points if
the Particles are not eventually removed.
I recommend, but do not require, a private method to remove unwanted
particles. That way you can get it all in one place. You may
also want a method that adds a single random dot at a given location with a
given color.
You really ought to make a few constants. For example, the number of
Particles made with each mouse click would make a great constant. You
should play around with the constants and find values that make it look
good. You may also want to play around with GRAVITY and
PARTICLE_DIAMETER in the Particle class.
The FireworksApp class
This simple class is a main method with only three statements. I've
provided it for you. Once you have written the two classes, the class
should compile and you should be able to run it.
It creates a FireworksController object, creates a FireworksWindow to control
that object, and shows the window.
Extra Credit
It's not hard to earn a little extra credit on this assignment.
The application I described to you is a little plain, and I encourage you to
play around. You can improve it how you like, but I'll give you some
ideas:
Make the colors vary slightly inside each firework like
this
Make several different shapes of fireworks. Here are
square
and
spiral
fireworks.
Make the Particles draw in a more interesting way--perhaps different sized
rectangles
?
Make several fireworks appear with each mouse click, perhaps like
this.
Make fireworks appear randomly without clicking the mouse.
Make a two-stage firework--each of the Particles explodes into more
Particles (this may require some redesigning)
Make the Particles have a short "tail" (this may also require
redesigning). Here is an
easy
way and a
harder
way.
I'm interested in insightful comments about why you wrote the code the way you
did. How do you deal with resizing the array? When do you
resize? Is your array ever partially filled? If so, how do you track
which elements are valid? How do you deal with removing Particles from a
ParticleList? When do you remove Particles? What constants did you choose
and why? I can give you extra credit if you can demonstrate that you fully
understand your code.
I'd also be interested in some thoughtful comments about the assignment in
general. How could this assignment have been made easier?
Harder? How could it have been put together better? Worse? Why
did I design things the way I did? How could you make the ParticleList
class more useful? What challenges did you face and how did you overcome
them? I'm willing to offer some extra credit if you can demonstrate that
you fully understand the assignment.
If you want any extra credit, turn in a file called extracredit.txt. That
file should contain an explanation of how you improved your code and any
analysis that you have for your code or the assignment.
Turning this in
Be sure to follow the course
style
and
commenting
guides--lots of people lost points here and they are the easiest points to
get. If you work in pairs, follow the
pair
programming guidelines and have BOTH partners send me a
readme.txt. Do not use the course handin program, since it isn't set up
for the mini-assignments.
Recall that the course policy is not to accept late work.
The files I want from everyone are: ParticleList.java FireworksController.java
If you want any extra credit:
extracredit.txt
If you modified Particle.java in any way, I want that: Particle.java
Pair partners: README.txt from both partners
I don't want javadoc or any .class files.
Please email the files to Tim Bahls. Send them to bahls AT wisc.edu.
You can zip them into one file if you wish.