7: Dance of the Ions
Fall 2007, David Matuszek
A number of ions (charged atoms) are placed in a two-dimensional rectangular box. The ions repel each other with a force inversely proportional to the square of their distance. That is, if they are one unit apart, they repel each other with a force of one; if they are two units apart, with a force of 1/4, or 0.25; if five units, with a force of 1/25, or 0.04; and so on. Every ion is affected by every other ion.
Ions that are close together will move apart, as each ion tries to get as far away from its neighbors as possible. After a while, the ions should settle down into a more-or-less "stable" position, and not move very much more.
In addition, some ions may be "glued in place" and unable to move.
Ion class and an
IonBox class. Ions come
in two flavors, "free" (able to move about) and "fixed" (glued in place).
Ion class will have:
Ion(double x, double y).
void setX(double x)
void setY(double y)
void setXandY(double x, double y)
IonBox class will have:
IonBox(double width, double height). Legal coordinates for
xwill be between
width; legal coordinates for
ywill be between
void putIon(Ion ion)to put an ion in a particular starting location. When this is called, all the ions will move around to reach a new stable position.
void putFixedIon(Ion ion)to put an ion in a particular location, and "glue" it there. When this is called, all the other ions will move around to reach a new stable position.
void removeIon(Ion ion)to remove a particular ion. Again, this will cause the other ions to move around until they reach a new, stable position.
mainmethod to allow the user to create an
IonBox, place and remove
Ionsin it, and find out where they end up. After each change, it should also display the minimum, maximum, and average distance from each ion to its nearest neighbor.
Most importantly, write JUnit tests to convince me that your methods work, and that the ions reach a stable position as far from one another as possible. And follow all the "best practices" that we have been talking about, such as writing tests first, keeping methods short and single purpose, etc.
I'm leaving more unspecified in this assignment than in previous assignments. It's up to you to fill in the details. I do want the above methods, so that I can do a bit of my own testing.
ArrayList<Ion>is like an array of ions, but can expand and shrink as needed. To declare one, you can use this syntax:
ArrayList<Ion> myIonList = new ArrayList<Ion>();The available
ArrayListmethods are described in
java.util.ArrayListin the Java API.
assertEquals(x, y)can't be used for doubles; you need to use
assertEquals(x, y, delta), where
deltatells how close together
yneed to be in order to be considered "approximately" equal.
mainmethod should allow the user to add (or remove) ions one at a time, and see the results.
mainmethod. It is mostly concerned with interacting with the user--getting commands and printing results--it isn't appropriate to try to write JUnit tests for it.
Thursday, November 1, before midnight. Turn in your program electronically, using Blackboard. (See my Instructions for Using Zip Files and Blackboard). Only assignments submitted via Blackboard will be accepted--do not send your program by email. The usual late penalty of 5 points per day (out of 100 points) will apply.