Goals

Announcement

  • Please fill out the following survey on COS 126 readings. Your comments will help us for future versions of this course.

  • Part 0:   preparation  

  • Do the Bouncing Ball warmup first. It will make this assignment much easier.

  • 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  

  • 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  

    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 appropriately rescale the planetary coordinates to Java coordinates.

  • Part 3:   resetForce and computeForce  

  • 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.

  • Use the method Math.sqrt to compute square roots.

  • Part 4:   move  

  • 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 "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. Use 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.

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


  • Kevin Wayne