The Magnet Lab
- Magnet lab is due this week
- Bring in design for part 1
- I will grade it, but not record the grade this week.
The goal of this week's lab is to practice designing classes and
methods. You will be designing a Magnet class that can generate
objects representing magnets that can be dragged around the screen.
The lab handout describes the program in detail. However, we'd like
to remind you of a few important points here.
- This week we ask that you to bring a "design" of the Magnet
and MagnetGame classes to lab. You only need to provide a design
for part 1 of the lab (though you should think about the rest as
well). Your design should consist of the following for each
class:
- A list of all constants and instance variables for the
class (with their associated types). Constants should be
provided with their values.
- A list of all methods of the class. Each method should include
- the return type of the method. (void if it is a
mutator)
- the list of parameters of the method.
- the comment that you will include at the top of the
method. For example, for a move method with parameters x
and y, the comment might be: moves the laundry item x
units to the right and y units down.
- a description (in English) of what the body of the
method does. As an example, consider the Tshirt dragging
example above. The design for the onMouseDrag method
might say: If the t-shirt is being dragged, move it from
lastPosition to point and update lastPosition to point.
- I will circulate through the lab at the beginning of the
period checking the designs and giving comments on their
completeness. You will not be graded on your design this week -
we'll simply check that you have one.
- Your Magnet class will interact with the Pole class that we
wrote. You must define the methods of Magnet exactly as we
specify or Pole will not work.
One interesting thing about the Pole class is that it needs to keep
track of which magnet it is part of. Thus when you create a Pole, you
must supply the Magnet as one of the parameters of the constructor.
The Magnet has to somehow say "Here I am" to the Pole.
Java's way of saying I or me is with the keyword "this" that we
encountered earlier. If an object needs to send itself along as a
parameter, say to the constructor of Pole, it does this by executing
new Pole(this, ...). If this is being done inside a method or
constructor of Magnet, then the type of this is Magnet. Lo and
behold, if you look at the declaration of the constructor for Pole,
the first parameter is of type Magnet, so this invocation of the
constructor is correctly formed.