Programming Assignment Checklist: Atomic Nature of Matter

Pair programming. On this assignment, just like the previous assignments, you are encouraged (not required) to work with a partner provided you practice pair programming (same rules as last assignment).

Frequently Asked Questions

Do I need to follow the prescribed API? Yes, we will be testing the methods in the API directly. If your method has a different signature or does not behave as specified, you will lose a substantial number of points. You may not add public methods to the API; however, you may add private methods (which are only accessible in the class in which they are declared).

Are the PowerPoint slides from precept available? Here they are.

How do I read in the .jpg files? Re-read the Picture API, which is available in the cheatsheet section of the booksite. To see it in action, program takes the name of a picture file as a command-line argument, displays it on the screen, converts all pixels to a luminance value, and displays all those pixels with a luminance value ≥ 180 in white. It relies on the helper program (Program 3.1.3) to convert from color to luminance.

Are diagonal pixels considered adjacent? No, use only the 4 ordinal neighbors (N, E, S, and W).

Do I have to output the blobs, beads and displacements in the same order as shown on the assignment web page? No, although keeping it in the same order makes it easier for you (and us) to check. However, you must follow the instructions for the BlobFinder.main() which says to print out the list of beads with size at least P before printing out the list of all the blobs.

What should I do if several of the beads in frame t+1 have the same bead in frame t as their closest bead? That happens from time to time, but don't worry about it. For our purposes, it is fine to ignore this case since the beads aren't supposed to get too close. If they do get close, there's no good way to track them anyway. Our posted solutions do not check for that rare case and we just let the same bead in frame t get paired a second time.

I am able to find beads and blobs correctly, but my BeadTracker gives a few errors, despite that it is mostly working. Why could this be? Make sure you exactly follow the given instructions: for each bead in each frame, you should be finding the closest bead to it in the previous frame. Doing the reverse gives slightly different results. (See also the next question.)

Why do I have to compare each bead in frame t+1 to each bead in frame t? Why can't I do it the other way around? It's an arbitrary choice. We require you do it this way to make it easier to check your answer.

My physics is a bit rusty. Do I need to worry about converting units? No, we have provided all of the constants in SI units. The only conversion you should need to do is to convert from distances measured in pixels (the radial displacements) to distances measured in meters using the conversion factor of 0.175 × 10-6 meters per pixel.

Can I assume that all of the frames be 640-by-480 and that all of the runs will consist of 200 frames? No, do not hardwire any of these constants into your program. Use picture.width() and picture.height() for the width and height, and use args.length for the number of command-line arguments.

Is there a way to make the toString() method format numbers in a nice way? Yes. String.format() works like System.out.printf(), but returns the resulting string instead of printing it. Here is our toString() method in Blob.

public String toString() {
    return String.format("%2d (%8.4f, %8.4f)", mass, cx, cy);
See p. 125 of the textbook to learn more about formatted printing.

How do I specify the 200 image names on the command line? One way is to type them all in.

% java BeadTracker 25 180.0 25.0 run_1/frame00000.jpg run_1/frame00001.jpg run_1/frame00002.jpg ...
An easier alternative is to use the wildcard capability of your command line: for example, the following specifies (in alphabetical order) all .jpg files in the run_1 directory.
% java BeadTracker 25 180.0 25.0 run_1/*.jpg

How can I estimate the running time of my BeadTracker program? Use the Stopwatch object from Section 4.1. Remember to remove it and any additional print statements from your submitted version of Alternatively, you may use the command-line switch java -Xprof BeadTracker (usually the flat profile is the most appropriate). Either way, when you execute BeadTracker, redirect the output to a file to avoid measuring the time to print the output to the terminal. Run BeadTracker with a variety of input sizes which allow you to get good timing data and form a doubling hypothesis.

How do I run BeadTracker with a variety of input sizes? Don't all the runs have 200 frames? They do, but when you are investigating the timing, you don't care about the actual displacements, only how long it takes to compute them.

On OS X or Linux, you can use the wildcard capability of the command-line to run 10 frames, 20 frames, 40 frames, 80 frames, and 160 frames:

% java BeadTracker 25 180.0 25.0 run_1/frame000[0]*.jpg    > temp.txt

% java BeadTracker 25 180.0 25.0 run_1/frame000[0-1]*.jpg  > temp.txt

% java BeadTracker 25 180.0 25.0 run_1/frame000[0-3]*.jpg  > temp.txt

% java BeadTracker 25 180.0 25.0 run_1/frame000[0-7]*.jpg  > temp.txt

% java BeadTracker 25 180.0 25.0 run_1/frame000*.jpg run_1/frame001[0-5]*.jpg > temp.txt

On Windows, one simple way is to create several subdirectories with the desired number of frames. Another way is to use the limited wildcard capability of command prompt to run 100, 200 and 400 frames.

> java BeadTracker 25 180.0 25.0 run_1\frame000*.jpg      > temp.txt

> java BeadTracker 25 180.0 25.0 run_1\*.jpg              > temp.txt

> java BeadTracker 25 180.0 25.0 run_1\*.jpg run_1\*.jpg  > temp.txt

How long should my program take? It depends on the speed of your computer, but probably between a few seconds and 30 seconds for 200 frames.

How much memory should my program use? Ours uses less than 5 MB. You will receive a deduction if you use substantially more. The most common way to waste memory is to hold references to an array of Picture objects in BeadTracker instead of only two at a time. You can use the -Xmx option to limit how much memory Java uses: for example, the following command limits the memory available to Java to 5 MB.

% java -Xmx5m BeadTracker 25 180.0 25.0 run_1/*.jpg

When I test with very small luminance, I get a StackOverflowError, but my code works for larger luminance. What does this mean? Since DFS is recursive, it takes up memory proportional to the height of the recursive call tree. If the luminance is small, the blobs can be very big, and the call tree height may be very large. We won't test it on cases with such big blobs. But if you are really interested in getting it to work when testing at home, try using the command-line option java -Xss20m to increase the stack space.

How accurate of an estimate should I get? You should get within 10% or so of the exact value for Avogadro's number (6.022142 × 1023). The standard deviation of the radius of the beads is about 10%, so you shouldn't expect results more accurate than this.

However, your estimates for a given run should agree exactly with ours. Our output from Avogadro for run_1 is given on the assignment page. Our output from Avogadro for run_2 is given below in the Testing section. Here is our output from run_6.

% java BeadTracker 25 180.0 25.0 run_6/*.jpg | java Avogadro
Boltzmann = 1.3482e-23
Avogadro  = 6.1670e+23

Why is it recommended to not use DrJava to run the code? DrJava does not support using wildcards like *.jpg and also requires that you escape each \.

Input, Output, and Testing

Testing. For testing, create main() methods in BlobFinder, BeadTracker, and Avogadro.

Possible Progress Steps

These are purely suggestions for how you might make progress. You do not have to follow these steps.


What is polystyrene? It's an inexpensive plastic that is used in many everyday things including plastic forks, drinking cups, and the case of your desktop computer. Styrofoam is a popular brand of polystyrene foam. Computational biologists use micron size polystyrene beads (aka microspheres, latex beads) to "capture" a single DNA molecule, e.g., for a DNA test.

What's the history of measuring Avogadro's number? In 1811, Avogadro hypothesized that the number of molecules in a liter of gas at a given temperature and pressure is the same for all gases. Unfortunately, he was never able to determine this number that would later be named after him. Johann Josef Loschmidt, an Austrian physicist, gave the first estimate for this number using the kinetic gas theory. In 1873 Maxwell estimated the number of be around 4.3 × 1023; later Kelvin estimated it to be around 5 × 1023. Perrin gave the first "accurate" estimate (6.5 - 6.8 × 1023) of, what he coined, Avogadro's number. Here's a reference on estimating Avogadro's number. The most accurate estimates for Avogadro's number and Boltzmann's constant are computed using x-ray crystallography: Avogadro's number is approximately 6.022142 × 1023; Boltzmann's constant is approximately 1.3806503 × 10-23 J K-1.

Where can I learn more about Brownian motion? Here's the Wikipedia entry. You can learn about the theory in ORF 309 - it's likely the first subject you'll be asked about if you interview on Wall Street.