Goals |
Frequently Asked Questions |
The submission system says my program exceeds the 80 character limit, but I don't see which line it's on. Can you help? The program WidthChecker.java and helper library CharStdIn.java print out all lines longer than a command line input N. Please note that all tabs are converted to 4 spaces during submission.
My computer it too slow to display the animation smoothly. Is there anything I can do? Here are a few suggestions. First, be sure that you are only calling Turtle.pause once at the end of each time step, instead of after each Turtle.spot command. You might also try increasing the delay parameter in Turtle.pause to prevent hoarding system resources. Or do several updates, say 20, before calling Turtle.pause.
Can I combine Steps 1, 2, and 3 into one massive loop? No! This will simultaneously screw up the physics and make your card harder to understand and debug.
I draw the planets, but nothing appears on the screen. Why? Make sure that the drawing coordinates are between 0 and 512. If not, rescale them so that they are. Also, be sure that you call Turtle.pause at the end of each time step.
How do I scale the bodies so that they stay inside the 512-by-512 Turtle graphics window? The first number in the data file is the radius R of the universe. You can assume all of the x and y coordinates of the planets remain between -R and R. Your job is to compute scaled and translated coordinates that are always between 0 and 512. Compute turtleX and turtleY with ax + b and ay + b for appropriate constants a and b. Note: you should not actually change the x and y coordinates of each planet (this will ruin the physics); instead use the temporary variables turtleX and turtleY to temporarily store the scaled coordinates you intend to plot.
I'm confused about all of the &Delta t / 2 terms. Do I need to worry about them to get the physics right? No! The update formulas for velocity and position already take this into account.
What should I use for the initial velocity in the leapfrog method? Use the value from the input file. As a technicality, the leapfrog method should be initialized with the velocity at time t = -Δt / 2, so we'll assume this is the value in the input file.
My planets repel each other. Why don't they attract each other? Make sure that you get the sign right when you apply Newton's law of gravitation. Note that Δx and Δy can be positive or negative.
How should I compute x2? Use x * x. Don't use Math.pow(x, 2).
I can't compile Turtle.java. Any thoughts? Be sure you are running Java 1.4 and not some older version.
Input, Output, and Testing |
Input. Copy the nbody directory from the COS 126 ftp site to your computer.
cp -pr /u/cs126/files/nbody .
Compilation. Your program must be named NBody.java. The capatilization is important. Compile your program with:
Note that it's not actually necessary to javac Turtle.java since this will happen automatically when you compile NBody.java.javac NBody.java
Execution. To redirect standard input from a file, execute your program with:
java NBody < planets.txt
You might also be entertained to substitute dance.txt for planets.txt. This universe was submitted by a Princeton student 5 years ago, and is absolutely wonderful! Set dt = 2500 and (i) either run on a fast computer or (ii) only call Turtle.pause after every 50 updates.
Also try out binaryStars.txt, courtesy of your classmate David Costanzo.
Reference solutions. For reference, we provide our executable code on Windows, Solaris, and OS X.
Submission |
readme.txt. Use the following readme file template and answer any questions.
Submission. Submit NBody.java. Don't forget to hit the "Run Script" button on the submission system to test that it compiles cleanly.
Possible Progress Steps |
These are purely suggestions for how you might make progress. You do not have to follow these steps.
Enrichment |