Mini-Assignment D: Fireworks
2
Due Tuesday, April 29th at 7:00 PM
Java concepts I want you to learn from this assignment:
-
Learn how to use inheritance
Other goals:
-
Give you a big head start on P4 part 2
-
Give you the opportunity to do more fun graphics on your own
Files:
miniD.jar
miniDsource.zip
Grading Standard:
Grading
Standard
Intro
Since people seemed to like miniC, we're continuing along the same lines.
I basically give you a complete solution to miniC, and you use inheritance to
make it even better. Inheritance is an extremely powerful idea, and this
assignment attempts to demonstrate it concisely. If you understand
inheritance, this assignment is not very long. Not counting comments and
blank lines, my sample solution is less than 80 lines of code (almost a third of
which are just end braces). If you have more than 40 lines of code in one
file, you are likely doing something wrong. By the way,
IF THE CLASSES DO NOT INHERIT FROM EACH OTHER
PROPERLY EXPECT HEAVY PENALTIES.
You will also
get stiff penalties if you make data members from Particle no longer
private.
To get started, download miniD.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 miniDSource.zip. Extract the .java files from it and put
them where the rest of your source code goes. FireworksController.java and
probably won't compile if you haven't written all your classes.
Inheritance
Hopefully you have noticed that a major principle of computer science is
eliminating duplicated code. We write methods and classes so that we can call
things and create things many times while only writing it once. We use
parameters and conditional statements so we can let one pice of code do many
different things when it is called. We use loops and arrays so we can use code
and values over and over while only writing it once.
We used interfaces so that classes that were very similar could be treated as
a single type. That way we use any of several completely different solutions
in one place. However, the interfaces had no code in them, and the classes
could not share any code. If we have classes that are very similar, but with
several important differences, we may be forced to write a single very
complicated class, or we may have to write the shared code several times. As
computer scientists, we don't like this. Duplicated code takes time to write,
and it's much harder to debug and maintain.
This is why we use inheritance. Given some class that has code we'd like to
use, we can extend that class. The new class (the subclass) has the all the
instance fields and methods of the original class (the superclass). However,
it can also have additional methods and fields. The subclass is a special,
more specific type of the superclass. For example, if we write an Animal
class, we create Bird and Fish as subclasses. If we wish to write a Parrot and
a Flamingo class, they can use Bird for their superclass (thus Animal is also
a superclass). All animals might have a weight and length, but only Fish would
have an isSaltWaterFish() method.
This project relies heavily on inheritance. To complete this project, you
should have a solid grasp of inheritance. Read this section and the book again
if you are still unclear. To complete this project, you will need to
understand many things about inheritance. Among them are these:
-
To extend a class, you need to add extends className to the class header
-
Subclasses may be stored in a variable with with a type that is a super
class.
-
Subclasses share data members, but that does not mean they can access
them--the variables are probably private.
-
Subclasses share methods. If the subclass declares a method with the same
header (name, return type, parameters) it will override the super
method--the object will use the overriding method instead. Otherwise, it
will just use the super classes method
-
Subclasses can call methods from its superclass even if they are overriden
by calling super.methodName(...)
-
When constructing an instance of a subclass, it is built on top of an
instance of the super class. Thus the first line of sub classes should
include a call to a constructor from the super class using super(...)
-
While a class can implement several interfaces, they can only extend one
class.
Inheritance and Particles
So how does this relate to Particles? Well, anyone that spent some time
playing with the Particle in Mini-assignment C will know that minor
modifications can make very nice effects. This way we can get these really
neat effects
Here is a summary of the classes in
javadoc
form and in MS Paint
UML
form.
Provided classes
There is a lot of classes that we are providing for
you. There are a few that are in miniD.jar, which is almost exactly the
same as miniC.jar. There are a few that we gave you as java files last
time. There are a few that are the classes you wrote for miniC.
There are a few that are brand new, provided as examples of inheritance and to
make your application more fun.
There are some changes to the classes we provided and wrote last
time. Here are a few changes to get you started:
-
The Particle class now has methods to retrieve its position, color and
velocity
-
Particles are no longer affected by Gravity (you'll add gravity back in
later)
-
The Particles are now bigger so you can see what's happening.
-
You can set the Position and Velocity of Particles
-
The ParticleList and FireworksController, which you worked on last time are
supplied. The ParticleList is the same, but the FireworksController is
considerably different.
-
Size is handled more elegantly. The FireworksController has a new
windowSize field.
-
The FireWorks controller now accepts keyboard input. You can press the
number keys to select a type of Particle to fire. Clicking will now
launch a stream of Particles of that type. More on this later.
We've provided some Particle subclasses for you as examples. If you learn
by example, be sure to check them out. The OrbitParticle gravitates to a
single point. The LineParticle draws itself as a line instead of as a
circle. The FunkyParticle is a type of LineParticle that moves according
to equations discovered in MiniC by Mikie Ruskin. Be sure to check them
out.
Particle subclasses you will
write
GravityParticle
This adds gravity back into Particle. Each step adds some small constant
to the y-velocity. This small value would make a really great constant....
SquareParticle
This class draws itself as an unfilled square instead of a filled circle.
You should use the the
drawRect(int,
int, int, int) method from java.awt's
Graphics
class. It's very similar to the fillOval(int, int, int, int) method we use
in Particle.
The size of the square should be passed into the constructor for SquareParticle.
BoundsParticle
TeleportParticle and BouncyParticle both need to know the size of the window to
function. Rather than write the code for tracking that information twice,
we will write it once in BoundsParticle. The Bounds particle is told about
the width and height of the window. It also has methods to retrieve the
data. Then TeleportParticle and BouncyParticle will extend BoundsParticle
and we only need to write bounds-storing code once but we can use it in two
classes.
It doesn't make much sense to create a plain BoundsParticle since it doesn't
interact with the bounds in any way. (If you want to know a better way to
do this that we won't cover in class, read
this
).
TeleportParticle
When these Particles exit the screen, they instantly reenter from the opposite
side. So if a particle leaves the left side, it enters the right side
going the exact same speed and direction.
BouncyParticle
When this Particle hits the screen, it bounces off. To make the bounce
look good, we'll want it to basically follow the laws of physics--just like a
specular
reflection. The math looks like it might be sort of hard, but it's
actually extraordinarily easy--if you are using trigonometric functions,
exponentiation, division, addition, multiplication, or addition, you're making
things too hard. Try some simple examples until you figure it out.
Extra Credit
If you'd like to make some new Particles, you can easily earn some extra
credit. Be sure to add them to FireworksController. I'll let
you come up with your own ideas.
I'd also offer extra credit for some well thrown or caught Exceptions, a good
File I/O or some analysis on the pros and cons of inheritance.
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:
GravityParticle.java
SquareParticle.java
BoundsParticle.java
TeleportParticle.java
BouncyParticle.java
If you want any extra credit:
extracredit.txt
If you modified any of the files I gave you, I want them. Tell me how you
changed them in extracredit.txt.
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.
Last updated 4-12-2008