Style guidelinesTopThe Magnet Lab

The Magnet Lab

  1. Magnet lab is due this week
  2. Bring in design for part 1
  3. We 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.

  1. 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:
  2. The TA's and 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.
  3. 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.


Style guidelinesTopThe Magnet Lab