Goals

Part 0:   preparation   (0 points)

  • Do the Bouncing Ball warmup first. This assignment will not be very difficult if you do the warmup first.

  • Download the latest version of the N-body assignment from the course Web page. The course packet contains a preliminary version.

  • Copy the following files from /u/cs126/files/Nbody/ into your public_html directory:
    Nbody.java   Nbody.html   NbodyDeluxe.java   NbodyDeluxe.html
    earth.gif    sun.gif      venus.gif          mars.gif
    mercury.gif  2001.au      starfield.jpg 
    
    You can do this with the following command:
    cp /u/cs126/files/Nbody/* ~/public_html
    

  • Your job is to write the implementation for class Body, an ADT that encapsulates information about a planetary body. The client programs Nbody.java and NbodyDeluxe.java use this ADT to create an applet that simulates planetary motion.

  • Part 1:   class Body members   (2 points)

  • The client program uses the following methods:

  • void move (int dt)

  • void show ( )

  • void resetForce ( )

  • void computeForce (Body b)
  • So, you will need to define these appropriately later on. For now, define these as methods (functions) that do nothing.

  • You also need to think about what data members should be in class Body. As in the Moving Ball warmup, each body should have a position, velocity, image, and applet associated with it. You may also find it convenient for each Body to have a mass and force associated with it. Since all the Body's share the same gravitational constant, you will want to define a static (global) variable G that can be shared among all the Body's.

  • Part 2:   move and show   (2 points)

    To get started, ignore gravity, and simply make the planets move in a straight line, as in the warmup.

  • For move(int dt) you can reuse much of the code from the warmup. The parameter dt is new; in the warmup it was assumed to be 1. Ignoring gravity, the new position should be the old position + dt times the velocity. Also, remove the "bounce" code.

  • For show() the code will be similar, but you will need to rescale the planetary coordinates to Java coordinates. First, understand how the rescaling is done in the show() method from the warmup. In the warmup, the ball coordinates stay between -0.5 and 0.5. The scaled x coordinate xpix must be an integer between 0 and applet.getSize.width(). The planetary coordinates are in a different range than the ball coordinates, so you will need a different formula to transform them into Java coordinates. Hint: recall the Mandelbrot assignment.

  • Part 3:   resetForce and computeForce   (3 points)

  • resetForce() should be straightforward to implement. If class Body has data members for force in the x and y directions, then you can simply reset these values to 0.0.

  • computeForce() requires Newton's law of gravitation. Make sure you get the +/- sign correct.

  • Each time computeForce() it should update the current force acting on the Body.

  • Use the method Math.sqrt to compute square roots. You should not need to use the sine or cosine functions.

  • Part 4:   move   (2 points)

  • Once you have Part 3 working correctly, write code for move() to account for gravity. This requires using Newton's second law and a calculus formula, both of which are described in the Assignment.

  • Make sure that you update the velocity and position, e.g., the new velocity will be the same as the old velocity if there is no force acting upon it. Also make sure that you update the velocity and position "simultaneously." That is, do not update the position using the updated velocity. If you do, the physics will be warped and the planets will spiral into the sun.

  • General debugging hints

  • Use System.out.println instead of printf for debugging.

  • Adjust the time quantum parameter dt for a finer or courser approximation. Adjust the delay parameter pause to prevent hoarding system resources. For smoother graphics, you can change the parameter calcPerDisplay. Drawing to the screen is slow. So instead, the code updates the bodies several times before redrawing.

  • Compiling and executing
  • Compile the standard version with:
    javac Body.java Nbody.java
    
    or compile a deluxe version with extra bells and whistles using:
    javac Body.java NbodyDeluxe.java
    

  • View the corresponding applet with one of the following commands:
    appletviewer Nbody.html
    appletviewer NbodyDeluxe.html
    

  • To view in a web browser, make sure that all of the appropriate files are in your public_html directory and are world readable. Set your Web browser to the page http://www.princeton.edu/~login/Nbody.html. To make the appropriate files world readable, type:
    chmod 644 *.html *.class *.gif *.au *.jpg
    

  • Submission and readme   (1 point)

  • Use the submit command:
    /u/cs126/bin/submit 9 Body.java readme
    

  • The readme file should contain:

  • Name, precept number, high level description of code, description of problems encountered, and whatever help (if any) your received.

  • Extra credit   (1 point)

  • In addition to the extra credit opportunities in the course packet, you can earn extra credit by creating your own "solar system." Extra credit will be awarded based on creativity and entertainment value. Some ideas include adding moons, asteroids, comets, etc.

  • If you do the extra credit, be sure to submit all files necessary to compile and run it.

  • Enrichment Links
  • Check the Java section of the Frequently Asked Questions page for more info and useful links.


  • Kevin Wayne