New version page

# BOWDOIN CS 210 - Program Structure

Documents in this Course

3 pages

25 pages

39 pages

16 pages

8 pages

12 pages

5 pages

3 pages

22 pages

2 pages

15 pages

4 pages

21 pages

12 pages

3 pages

5 pages

2 pages

16 pages

25 pages

41 pages

40 pages

15 pages

2 pages

3 pages

15 pages

32 pages

Unformatted text preview:

Program StructurePlaying with magnetsThe Scenario. We would like you to write a program that simulates the action oftwo bar magnets. Each magnet will be represented by a simple rectangle with oneend labeled “N” for north and the other labeled “S” for south. Your program shouldallow the person using it to move either magnet around the screen by dragging itwith the mouse. You know that opposite poles attract, while similar poles repeleach other. So, if one magnet is dragged to a position where one or both of itspoles is close to the similar poles of the other magnet, the other magnet shouldmove away as if repelled by magnetic forces. If, on the other hand, opp osite polescome close to one another, the free magnet should move closer and become stuckto the magnet being dragged.To make things a bit more interesting one should be allowed to flip a magnetfrom end to end (swapping the poles) by clicking on the magnet without m ovingthe mouse. This will provide a way to separate the two magnets if they get stucktogether (since as soon as one of them is reversed it will repel the other).To decide whether two magnets interact (attract or repel), we need to look at thedistance between their opposite poles. So it is the poles rather than the magnetsthat really matter when deciding whether something should be attracted or repelled.As a result, instead of just manipulating magnet objects in your program, you willalso need objects that explicitly represent poles.Questions: How do you approach this problem? How do you decide how manyclasses to write? How do you decide what each class should do?You could write the whole program in one class. But that is bad style. Notto mention cumbersome. To solve the problem e legantly you’d write at least twoclasses: one to handle magnets, one to put them on the window and handle themouse.A magnet has geometry (location, width, height), two poles, can move, andinteract with the other magnet.The MagnetController has to create two magnets, handle mouse movement bycalling the appropriate magnet methods, and handle interaction (magnets do notknow about each other).Before looking at the solution, try to come up with an outline of the classes.Note that the way you design your solution depends on a few decisions you needto take: what class handles the rendering of the magnets? the interaction betweenmagnets?csci210: Data Structures Spring 20082 · Program StructureQuestionsRead the implementation of the Magnets game in the handout and try to under-stand the logic behind it. Here are some questions:(1) Make a diagram showing the data and methods in each class and draw arrowbetween classes to show how they interact.(2) How is the rendering of the magnet handled (and where)?(3) How is the interaction between magnets handled (and where)?csci210: Data Structures Spring

View Full Document