Art Show | Course Home | Homework | Labs | Programming | Schedule & Lecture Notes | Submit

Saint Louis University

Computer Science P125
Introduction to Computer Science

Michael Goldwasser

Spring 2005

Dept. of Math & Computer Science

Programming Assignment 05

Ball Simulation

Due: Monday, 11 April 2005, 8pm

Please see the general programming webpage for details about the programming environment for this course, guidelines for programming style, and details on electronic submission of assignments.

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.

Please make sure you adhere to the policies on academic integrity in this regard.


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. For this assignment, we will provide all of the other portions of the code, including a driver for gathering input from the user as well as all of the necessary graphics for visualizing the simulation. However you must define and implement the Ball class properly for the simulation to be complete.


Your Tasks

Your main task is to define and implement a class Ball as described here.

How you choose to represent the private aspects of your class is up to you. In some manner, you will have to define data members which can represent the state of a ball, namely its current position and current velocity.

As far as the public interface of your class, you must include each of the following method prototypes, defined precisely as described here. The precision is necessary because your code must be integrated into the remainder of the project.

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. For each tick of the clock, the world will call the method Update for the Ball. It is the responsibility of this Update method call to perform all necessary actions in simulating that time unit. In particular you will need to update the current position and velocity of the ball accordingly. 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 with a simple user interface titled sim. This text-driven menu first directs you through a series of questions to define the characteristics of the World. After setting up the world, the driver prompts you to introduce a ball with a given set of initial characteristics. It then simulates the motion of that ball so long as the ball remains in the visible portion of the world. Once the ball leaves the world it is destroyed and the driver will again prompt you to create a new ball.

You should note that our driver intentionally suggest default responses for each question posed, and you may signify that you are willing to accept the default by simply pressing return. This should make it much easier for you to repeat certain tests by a series of accepted defaults.


Files You Will Need

We are providing the following files for your use. You may either download from a browser or copy them directly to your current directory on turing with the comand:

cp -Rp ~goldwasser/csp125/programs/simulation .

There are several files:


Files to Submit


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. The original set of files we have provided will not even compile properly. To get to this point, you will have to define your Ball class, so that its public interface precisely matches the specifications described above.

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

  3. Start to implement the Update 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.


Examples

For the sake of comparison, we have made our solution available for you to execute. You will find it at:

~goldwasser/csp125/demos/simulation/sim

Here is an image based upon the default configuration settings used in the driver:


Grading Standards

The assignment is worth 10 points. If you worked as a pair, you will each be given the same grade.


Extra Credit (1 point)

We will award one additional point to students who successfully meet the following challenge. In the required assignment, the gravity of the world was viewed as a constant and was aligned with the Y-axis. For extra credit, we wish to have you adjust your ball implementation so that it reacts properly to the gravity of a star (in addition to the gravity of the World in the standard case).

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 actual strength of the force depends on the distance between the center of the ball and the center of the star, as well as the mass of the star. Specifically, the magnitude of the force should be equal to the star's mass 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 may query information about the world through the following methods of the World class:

The remaining three routines should only be called if you have already verified the existence of a star. (that is, they will return values in either case, but they are only meaningful when HasStar()==true.)

For the sake of comparison, here is an image based upon the default extra credit configuration settings used in the driver:


Michael Goldwasser
CS-P125, Spring 2005
Last modified: Thursday, 07 April 2005
Art Show | Course Home | Homework | Labs | Programming | Schedule & Lecture Notes | Submit