Course Home | Homework | Lab Open Hours | Programming | Schedule & Lecture Notes | Submit | Turing Access

Saint Louis University

Computer Science 150
Introduction to Object-Oriented Programming

Michael Goldwasser

Fall 2006

Dept. of Math & Computer Science

Programming Assignment 05

Ball

Due: 8:00pm, Tuesday 7 November 2006


Contents:


Overview

The goal of this assignment is to create, document and implement 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.

Newly Introduced Techniques


Collaboration Policy

For this assignment you must work individually in regard to the design and implementation of your project.

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


Your Tasks

Your main task is to implement and document a Ball class, described briefly with the following class diagram:

Our software will explicitly rely on each of the above methods, so you must be careful to accurately reflect the given specifications. For most, the desired behavior 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 our part of the software. The World is built as an extension of a Canvas but has additional code which acts as a clock in some sense. For each tick of the clock, the world will call the method update method for each object in the world, sending a reference to the world as a parameter. The behavior of the method for a given object should be to update the state of that particular object for one simulated timestep.

In the case of a Ball you will need to update the position and velocity accordingly. For desired precision, please make sure that you initialize your instance variables to use the float data type (rather than an integer data). When drawn on the screen, the ball's position will actually be rounded to the nearest pixel, but you will still find a more accurate simulation by using floating point calculations to represent the ball internally.

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


Good Software Practices

Not only do we want a working implementation, but for full credit your code must adhere to the good software practices as described in Chapter 6. Specifically,


Physics 101


User Interface

We are providing a driver titled Simulation.py. Your Ball class definition, in isolation, is not a complete program. To test your code, run the simulation program with the command python Simulation.py. Please note, you do not need to understand our part of the sourcecode. In fact, you would be wise not even to read it.

This text-driven software 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 suggests 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.

Notice also that the driver allows you to add a sequence of identical balls to the screen (in a staggered fashion) rather than simply one ball at a time. By using a sequence of balls, you can get a more interesting visual trail of the traveling balls, as shown in the following diagram


Files You Will Need

You will need to have the file Simulation.py in the same location in your account as the file Ball.py which you are expected to write. To copy that file to your own directory, type the following from the terminal:

cp ~goldwasser/public/Simulation.py .
If you are working on your own machine, you may download Simulation.py here.


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. You must make sure that you create a file Ball.py which defines the Ball class as described precisely in the earlier diagram.

  2. Ideally, you may as well document your class right away. You know how you want everything to behave, even if you haven't implemented it all.

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

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

  5. Finally, take gravity into account and put everything together.


Submitting Your Assignment

You should create a new file, Ball.py, which contains all of your own code. This file must be submitted electronically. You should also submit a separate 'readme' text file.

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


Grading Standards

The assignment is worth 10 points. The grade will be based upon evaluation of the correctness of your implementation as well as the quality of your software practices.


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 update routine so that the ball is properly affected by the gravity of the Star.

Physics 102:

Incorporating the gravity of a star is more complicated than the gravity of a world, for two reasons. Both the strength of the gravity and the direction of the force depend on the relative position of the ball and the star. For comparison, the gravity of the world was assumed to be a constant force, aligned along 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. The magnitude of that force should depending upon the mass of the star and the distance between the center of the ball and the center of the star. Specifically, the magnitude of the force equals 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 can query the world to return this star to you through the method star = world.getStar(). The star will be returned, assuming one exists. To test whether or not this is the case, you may include the following in your update routine:

star = world.getStar();
if star:
   // star exists.  Add your code here to adapt.

A star's position and mass can be queried through the use of methods getPositionX(), getPositionY() and getMass().

You may find need to compute a square root. A function for computing a square root is included in the math library of Python. You may use it by stating from math import sqrt

If you have everything implemented correctly and use the default parameters for the extra credit, you will get an orbit as shown in the following figure


Michael Goldwasser
Last modified: Tuesday, 14 November 2006