CS 102 Lab # 3
Monopoles

Objective: To gain experience implementing classes and methods.
Note that for this lab you must write a program design before writing any code.
Big hint: Read Chapter 6 of your textbook carefully before attempting the harder parts of this lab.

The Scenario

A magnet with only one pole is called a magnetic monopole. Whether monopoles really exist is an open problem in modem physics. Nonetheless, for this lab, we would like you to write a program that simulates the action of two monopoles. Each monopole will be represented by a simple rectangle labeled "N" for north or labeled "S" for south. Your program should allow the person using it to move either monopole around the screen by dragging it with the mouse. You know that opposite poles attract, while similar poles repel each other. So, if one monopole is dragged to a position where it is close to another monopole with the same label, the other monopole should move away as if repelled by magnetic forces. If, on the other hand, opposite poles come close to one another, the free monopole should move closer and become stuck to the monopole being dragged. The monopoles will not be allowed to rotate. They only slide up, down and across the screen.

To make things a bit more interesting one should be allowed to flip the label of a monopole by clicking on the monopole without moving the mouse. This will provide a way to separate the two monopoles if they get stuck together (since as soon as one of them is reversed it will repel the other).

You would not need a deep knowledge of magnetism and mechanics. Instead, you could exploit something every special effects expert or video game author must know. Most humans observe the world carelessly enough that even a very coarse approximation of reality will appear "realistic". Our code takes advantage of this by simplifying the behavior our monopoles. We never compute the force between two monopoles, just the distance between them. If they get too close together, our code moves them apart or makes them stick together.

Remark: There is some potential for variation between the sample solution seen above and the behavior of your program: the sample randomly assigns "N" or "S" to the monopoles, but your program might not do so. This is fine. Also the initial location of the monopoles is not important.

Here is a demo of what we have in mind:



Design of the program

As mentioned earlier, before writing code, you should write a design. The design should show how you plan to organize your classes to accomplish the actions required. Below, we describe what methods are needed and the behavior that they should provide. To write your design, you should write (in English comments, not Java) your plan for how each method will provide the necessary behavior. You should also describe (in English) what variables you think are necessary for each class. For future labs, you'll be required to write designs like this in advance. This level of preparation will allow you to progress much more quickly in lab so that you can take better advantage of your time.

Before implementing your design, review your design: Does it make sense to you? Do you understand what instance variables are needed? When you're satisfied with it, translate it into Java code. Don't erase your comments - they'll help you keep track of what you're trying to do.

We will help you design this program by identifying the classes and methods needed. In particular, you will need two classes: the Monopole class models the behavior of a monopole and a class PoleController that is an extension of FrameWindowController.

You should start by creating a CS102/lab3 folder and create the following files with Dr. Java:

The PoleController class will be similar to the BumpingBall class defined in Lab 2. Look up the code from lab 2 to remind you how it is done.

In the following, we will focus out attention on the Monopole class

The Monopole class:

In this section, we explain how to write the Monopole class and describe the methods that needed to manipulate Monopoles. The idea is that after the Monopole class is defined, you can then write code to work with Monopoles just as you wrote code to work with FilledRects. We will see, however, that the interaction of Monopoles with the rest of your program is a little more complex than that of rectangles.

A monopole is represented by a FramedRect box and and a Text label. The box should have size 50 by 50 pixels, and the label should be center inside the box.

We listed in the following the constructor and methods that you may need in the Monopole class. You may find it necessary or convenient to add more methods.

Interaction between the poles

The monopole class has two more specialized methods: attract and repel. Each of these methods expects to be passed another Monopole as a parameter. If you say,

   currentPole.attract( anotherPole )
then somePole and anotherPole should have opposite polarities. If currentPole is a north pole, then anotherPole must be a south pole and vice versa. The repel method, on the other hand, assumes that the pole provided as its parameter has the same polarity as the object to which the method is applied. Therefore, if you write:
   currentPole.repel( anotherPole )
and currentPole is a north pole, then anotherPole should also be a north pole. When defining the methods, you can assume that the conditions are already satisfied.

Each of these methods checks to see if the two Monopoles involved are close enough together to exert enough force to move anotherPole. If so, they use the move and/or moveTo methods of Monopole to either bring anotherPole next to currentPole or to move anotherPole away so that they would no longer interact.

The good news is that the repel method behaves exactly in the same way as the bump method of the balls defined in Lab 2. Look up the code for lab 2 to remind you how it is done. The attract method will be explained in class.

Grading Point Allocations

Value
Feature

Style (8 pts total)
2 pts. Descriptive comments
2 pts. Good names
2 pts. Good use of constants
2 pts. Appropriate formatting


Design (4 pts total)
2 pts. Good use of boolean expressions
1 pt. Not doing more work than necessary
1 pt. Using most appropriate methods


Correctness (8 pts total)
1 pt. Drawing monopoles correctly at startup
1 pt. Dragging monopoles correctly
1 pt. Ability to move either monopole
1 pt. Moving a monopole to the right place when attracted or repelled
1 pt. moving the monopole not pointed to when attracted or repelled
1 pt. Flipping a monopole
1 pt. Attracting and repelling at the right times
1 pt. No other problems


Adapted from materials developed at the Department of Computer Science, Williams College