We have implemented a program that simulates two monopoles in Lab 3. In this lab, we are going to further extend that program to simulate two magnets. Each magnet has a north pole labeled by "N" and a south pole labeled by "S". Your program should allow the person using it to move either magnet around the screen by dragging it with the mouse. So, if one magnet is dragged to a position where one or both of its poles is close to the similar poles of the other magnet, the other magnet should move away as if repelled by magnetic forces. If, on the other hand, opposite poles come close to one another, the free magnet should move closer and become stuck to the magnet being dragged. The magnets will not be allowed to rotate. They only slide up, down and across the screen.
Similar to monopoles, a magnet can be flipped from end to end (swapping the poles) by clicking on the magnet without moving the mouse. This will provide a way to separate the two magnets if they get stuck together (since as soon as one of them is reversed it will repel the other).
Here is a demo of what we have in mind:
Your program consists of three classes:
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. In particular, you need to describe what methods are needed and the behavior that they should provide. Inside each method, you should write (in English comments, not Java) your plan for how the method will provide the necessary behavior. You should also describe (in English) what variables you think are necessary for each class. 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 describe below a few details that you need to pay attention to:
Grading Point AllocationsValue |
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 magnets correctly at startup |
1 pt. | Dragging magnet correctly |
1 pt. | Ability to move either magnet |
1 pt. | Moving a magnet to the right place when attracted or repelled |
1 pt. | moving the magnet not pointed to when attracted or repelled |
1 pt. | Flipping a magnet |
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