Programming Assignment 2

Due: 8pm, Wednesday 11 February 2004


Contents:


Overview

The goal of this assignment is to create a new class, Ball, which simulates the movement of a ball under the effects of gravity. We will provide you with the necessary user interface, but you must create the new class properly for the simulation to be complete.

Newly Introduced Techniques


Collaboration Policy

For this assignment, you are allowed to work with one other student if you wish (in fact, we suggest that you do so). If any student wishes to have a partner but has not been able to locate one, please let the instructor know, so that we can match up partners.

In this regard, please make sure you adhere to the policies on academic integrity.


Your Tasks

Your main task is to implement a class Ball as described briefly with the following UML Class diagram:

We have not explicitly listed any instance variables in the above diagram, because the rest of the project will interact with a Ball solely through the listed methods. We live it to you to decide what instance variables you may need to define in order to maintain the current state of a Ball.

Our software will explicitly rely on each of the above methods, so you must be careful to accurately reflect the specifications from the above UML diagram. The desired behavior for most of the above methods should be self-evident from the method names. The last method, however, warrants further explanation. Your Ball objects will live in a World which has been created by us. The World is built as an extension of a Canvas but it has additional code which acts as a clock in some sense. For each tick of the clock, the world will call the method doTick(World w) for each object in the world. It is the responsibility of that method call to perform any necessary actions in response.

In the case of a Ball you will need to update the position and velocity accordingly for each tick of the clock. You will notice that the position and velocity coordinates are represented using the data type double, which is a double-precision floating point number. When drawn on the screen, the ball's position will actually be rounded to the nearest pixel, you will still find a more accurate simulation by using such double's to represent the ball.

For the sake of completeness, we next review some elementary physics to explain the desired behavior.


Physics 101


User Interface

We will be providing you a user interface through a driver titled BallDriver. You will start the application by invoking the "main" method of this class (as with the last program, no additional arguments are expected). The initial interface is the following window:

You must press "Start" to bring up a new World. The world will have size of 500 x 400 pixels. If you then wish to add a Ball to the world, press the "Add" button while "Ball" is selected above it. That will cause a new dialog box to appear, which prompts you for the initial position and velocity of the new ball.


Balls can be added either while the simulation is running or while paused. If paused, the ball will not actually appear until the simulation is continued. You can also use the dialog box to add a series of balls with similar initial parameters, by repeatedly pressing the "OK" button on the dialog for adding balls, creating pictures such as the following:

A final note: if a ball starts out of the viewing area of the world, or ever leaves the world, our program immediately destroys the ball (despite the possibility that its trajectory may have eventually brought it back into view).


Files You Will Need

We have placed a template copy of the project, Ball, in each of your home directories on patel2.slu.edu.

We are providing the following file:

However, the project will not properly compile, much less run, until you create the required class Ball. You can create the new class in BlueJ by selecting "New Class..." from the "Edit" menu. When you do this, BlueJ tries to get you going by giving you some sample code for such a class. Please be aware that the template they give you is not based on this assignment, and you will probably want to delete or revise most of the initial code.


Suggested Steps of Progress

You may wish to tackle the assignment in stages, making sure to succeed in each stage before continuing to the next.
  1. For the BallDriver class to successfully compile, you will have to define your Ball class, so that it precisely matches the syntax suggested by the UML Class diagram.

  2. For the ball to appear properly on the screen, you will already need to use instance variables and to successfully implement the methods to set and get the Ball's position.

  3. Start to implement the doTick routine, ignoring gravity for the time being. You should use the velocity to control the movement of the ball's position.

  4. Finally, take the gravity into account and put everything together.


Submitting Your Assignment

Please see details regarding the submission process from the general programming web page, as well as a discussion of the late policy.

If working as a pair, only one student should submit the project, but please make sure that you clearly document the existence of both authors.


Grading Standards

The assignment is worth 10 points.


Extra Credit

We will award one additional point to students who successfully meet the following challenge.

The interface we provided also allows you to add a Star to the world. Adjust your doTick(w) routine so that the ball is properly affected by the gravity of the Star (in addition to the gravity of the World).

Physics 102:

Incorporating the gravity of a star is more complicated than the gravity of a world, for two reasons. First, the gravity of the world was assumed to be a vector aligned with the Y-axis, thus of the form (0,g) for some value g. The gravity between the star and a ball should be a vector pointing from the ball towards the star. Secondly, the star will provide a gravity coefficient, but the actual strength of the force depends on the distance between the center of the ball and the center of the star. Specifically, the magnitude of the force should be equal to the star's coefficient divided by the square of the distance between the star and ball's centers.

Coding Issues:
There are some additional issues to cope with, though this is extra credit after all. For simplicity, at most one star can exist in the world at any given time. You can query the world to return this star to you through the method getStar(). In the case that there is no star, this will return a null reference. In short, you can add the following to your doTick routine:

Star star = w.getStar();
if (star!=null) {
   // star exists.  Add your code here to adapt.
}

To query a star's position and gravity strength, see the documentation for the class Star.

To handle the additional mathematical calculations, you may rely on additional Java classes. For example, if you represent vectors using the java.awt.geom.Point2D.Double class, you will find that it supports a method distanceSq to compute the square of the distance between one point and another. Alternatively, you may find methods of the class java.lang.Math to be of assistance.

Sample Parameters:
To get a ball in a (roughly) circular orbit around a star, place a star at position (250,200) with gravity coefficient 1000. Then add a ball at initial position (250,50), with initial velocity of (2.5,0), for an approximately circular orbit. For an eliptical orbit, change the initial velocity of the ball to (1.5,0) or to (2.75,0).


Michael Goldwasser
Last modified: Saturday, 07 February 2004