Assignment 3 - Classes and
Objects
Due Before class on Oct 31st (Halloween)
The objective of this assignment is to try to get
familiar with the idea classes, and objects. In class I've been
playing around with an applet that draws Pacman. I would like you
create a class of simple graphical objects such as my Pacman example,
and create an applet which uses objects of your class. Then post this
applet on the web and email me the URL so I can view it. I'll link
them to the class website as well. As always, you are welcome to
propose a different assignment for yourself as long as you run it by
me and I agree to it.
Your shape class should at leasthave the following
interface. By all means, I encourage everyone to add any methods they can
think of that might be useful for their class. For example, my
Pacman class includes a setMouthAngle() method.
- public Pacman(int x, int y)
- A constructor (substitute the name of your class if you aren't
making Pacman) that takes two integers as arguments representing the
starting position of the object on the screen.
- public void draw(Graphics g)
- A method that takes a Graphics object as an argument and
draws the shape in that graphics context.
-
- public void setSize(int size)
- A method that takes and
integer argument that determine size the object in pixels. Optionally,
you may also want to make another setSize() method that takes
two arguments, one for width and one for height.
- public int getSize()
- a method that takes no arguments and returns an integer
representing the current size of that object in pixels.
- public void setColor(Color c)
- A method to change the
color of the object. It takes a Color
object as an argument.
Post it to the web.
Pacmen - My random Pacman applet created with a Pacman class.
I started
from scratch and incrementally added new capabilities to my applet
and debugged each one before continuing. I reccomend doing the same
thing in this assignment (and all programming you do). Here's are some
of the higher level steps I went through.
- I copied the Circles.java applet and its HTML file, Circles.html to
use as a starting point. Make sure the copy compiles and runs so I
know Everything was renamed right and I'm starting from code that works.
- Then I created a new file, Pacman.java to hold my Pacman class. first I
gave it a draw() method that just drew my shape at a default
size, color, and location. Then I went back and called it from the
applet's paint() method passing in the Graphics
object from that method. Then I compiled and tested it.
- Next I made "fields" or "class variables" with default values to
hold the size and position of each Pacman and updated the
draw() method to use the variables instead of the "hard
coded" numbers.
- Then I added a constructor method that took the position as
arguments and set my x and y position variables. Recompile, test,
debug, etc.
- I added setSize(), getSize(), setColor(),
setMouthAngle(), setHeading(), etc. one at a time,
updating my applet code to use each new method, and testing it before
going on to the next.
Lastly, I want to stress that even if you haven't been able to get
it work by the due date you should still send me what you have
(preferrably post the source code to the web). Don't wait to turn it
in until you have it perfect. I want to see what you've got each week
even if it is taking you longer to complete the assignments.
Helpful Links