CS 302 Programming Assignment 1: Golf...and fireworks!

Due date: Friday, September 28 at 7:00 pm (CSL time)

Announcements | Overview | Goals | Description | Hints | Handin

Last modified: Wednesday, September 26, 2007


Announcements

Includes:  Additions, Revisions, and FAQs (Frequently Asked Questions).
Please check here frequently.

9/26/2007
  • The handin directories have been created. See the Handin section for details about how to use the Handin program and what to hand in.
  • Edit for clarification: sentence added to make it clear that the print method is required.
9/21/2007 Program released. The handin directories have not yet been created - watch this space (and your email) for an update when they have been.


Overview

In this assignment you will write a class that simulates the motion of physical objects in freefall near the Earth's surface. Then you will use this class to animate golf balls in flight and a fireworks display. (Don't worry; we take care of most of the animation code, and all the physics you need to know is right in this document!)

You will model physical objects by using the instantiable class Particle; each object from this class represents a different physical object. After implementing and testing the Particle class, you will use it in an application class AnimationPlayer, where you will set up the starting position and velocity of Particle objects in such a way that the golf animations will be holes-in-one. You will create multiple instances of Particle objects to represent the different golf balls and use them with an instance of a class called Animator to make the animation come to life. The fireworks display is also based directly on your Particle implementation but in such a way that you don't actually instantiate any new Particle objects. That is, your Particle class is used to implement a fireworks display but how this is done is completely hidden from your point of view! Finally, you will be required to answer some questions in a separate file.


Goals


Description

Physics background

Your program will have to implement some basic physics to satisfy the requirements of the project. The purpose of this section is to give you the physics background you will need in order to complete this project.

We will be representing the position of physical objects in a 2-dimensional coordinate system. In this system, the y-axis is vertical ("goes up") and corresonds to "height", whereas the x-axis is horizontal ("goes to the right"). Every physical object has a location which is uniquely determined by a pair of decimal numbers that represent its position with respect to the origin of the coorindate system, just like in algebra. For our purposes, these numbers are always in units of meters. For example, the ball's x-position is -20.24 meters and y-position is 4.37 meters in the figure below (the ball is white). (Note that the origin is roughly in the middle of the tee box at the right of the figure. Also note that the "fake 3D" look of the program is just for show; it's really a 2D world, i.e., the tee is literally higher than the green.)

We will also need to deal with velocities. Every physical object has an x-velocity (which represents "how fast the physical object is going in the x-direction") and a y-velocity (which represents "how fast the physical object is going in the y-direction"). The values can be positive or negative. For the y-velocity, positive would indicate that the object is going up, whereas negative indicates that the object is going down. In the case of x-velocity, positive values indicate that the object is going to the right while negative values mean the object is going left (for our coordinate system, anyway). A velocity of zero in either case means that there is currently no motion in that particular direction. We will always use units of meters per second to describe velocities (which basically means "how many meters would it go per second, if it kept going at the current speed"). The following illustration shows the x and y velocities of the ball as "arrows" coming out of it. The (very approximate) path of the ball is also shown for reference. Note that the y-velocity is negative since the ball is going down (also note that these annotations won't be displayed when the program is running).

One final note on terminology: when we just say "velocity" or "position" (without specifying x or y), we mean both x and y components. The main thing we will need for the program is to know an answer to the question: "Given we know the current position and velocity of some physical object, how do we figure out what the position and velocity will be t seconds from now (for some given positive value of t)?".

Suppose that we have a physical object and we know its current x-position px, y-position py, x-velocity vx, and y-velocity vy. We want to find the new position and velocity t seconds from now (we will denote these future position and velocity values as x-position Px, y-position Py, x-velocity Vx, and y-velocity Vy). Then these new position and velocity values are given by the following equations. These equations (based upon a 9.8 m/s/s acceleration down the negative y axis as the given number of seconds of time goes by) take into account gravity near the Earth's surface, as well as the original position and velocity of the object and how much time goes by.

Vx = vx
Vy = -9.8 * t + vy
Px = vx * t + px
Py = -4.9 * t2 + vy * t + py

Classes to write

To complete this assignment you will need to write three classes:

The Particle Class

The Particle class is an instantiable class representing a single physical object. Different instances of it will be used to represent distinct physical objects. Each Particle object needs to keep track of its x and y position and its x and y velocity. These values should all be doubles. Another thing that the Particle objects need to keep track of is the starting x and y positions and velocities. This will be needed in order to implement the reset() method which resets the position and velocity back to the ones the particle was given when it was first created (i.e., the ones passed to the constructor).

Constructors

The Particle class must have the following constructor:

Accessor Methods

The Particle class must have the following public accessor methods, each of which returns the corresponding current (x or y) position or velocity value.

Mutator Methods

The Particle class must have the following two public mutator methods:

Other Methods

The Particle class must also have the following public method:

Testing the Particle class

Your ParticleTester class should test each method and constructor of the Particle class to ensure proper function. To be useful, your ParticleTester class should print out something that you can use to verify the functionality of each method or constructor. Note that the ParticleTester class is a type of application class, so it must have a main method (and thus be runnable).

You will also create a text-only file named expected.out that contains what you expect the ParticleTester class to print out when it is run (in other words, the expected output). You can then compare the contents of expected.out with the actual output when you run the test program to determine if the results are correct. It's a good idea to figure out what ParticleTester should print out and record it in your expected.out file before you run the test. (Don't be confused by the file name: expected.out is actually a text file, even though it does not end with .txt! The only difference is that in Windows you will not be able to open the file by double clicking on the expected.out icon. You will instead have to open the editor and do File→Open. Otherwise the file is a perfectly valid text file.)

Note: Arithmetic operations in Java using floating point types (which includes double and float) are not exact! For example, you might compute something like 10.0 / 2.0 and wind up with 4.999999999 instead of the expected 5.0. As a rough guideline for this programming assignment, very small differences (say, .0000001 or smaller) in the output and expected output can probably be attributed to round-off errors. Large discrepancies (say, on the order of .001 or bigger) are probably due to errors in your code. The bottom line is that we care whether your source code has errors or not; inaccuracies due to round-off error are not something you need to worry about for this assignment.

The following is a code fragment that you might include in the main method of your ParticleTester class and the corresponding entry for your expected.out file.

ParticleTester code
System.out.println("*** Testing Constructor " +
                   "Particle(0.0, 0.0, 2.0, 0.0) ***");
Particle rock = new Particle(0.0, 0.0, 2.0, 0.0);
System.out.println("*** Testing print() ***");
rock.print();
System.out.println("*** Testing passTime(1.0) ***");
rock.passTime(1.0);
rock.print();
expected.out *** Testing Constructor Particle(0.0, 0.0, 2.0, 0.0) ***
*** Testing print() ***
Particle at (0.0, 0.0) with velocity (2.0, 0.0)
*** Testing passTime(1.0) ***
Particle at (2.0, -4.9) with velocity (2.0, -9.8)

There should be a line in your expected.out file for every line printed out by your ParticleTester class.

The Animator Class

The Animator class has already been written and is provided to you for use in the AnimationPlayer class. Animator provides methods that let you control which golf animations play (and in what order). Refer to Animator documentation for full details.

You will need to download Animator.jar to the Eclipse folder you are using for programming assignment 1.  (You can do this by right-clicking on the link for Animator.jar and then choosing the "Save Link As..." option.)  After you have downloaded,  you still have one more step to do in Eclipse to get it to recognize the Animator.jar file as one containing Java classes:

The AnimationPlayer Class

AnimationPlayer class is the application that creates an Animator object and tells the Animator object to play animations for holes 1,7, and 9 in that order. The calls should be made in such a way that each animation turns out to be a hole-in-one. Then a fireworks display should be given. In order to do this you will need to study the appropriate methods from the Animator class in the javadocs. The playerName parameter to the constructor needs to be a string that matches your real name (or both of your names, if you are pair programming). Note that you may need to experiment with the method calls to get all the holes-in-one. Also, you may want to try the fireworks first once you think you have your Particle class working before going on to try the golf animations.

After the animations for the holes of golf are played, the AnimationPlayer class should do the following, in the order given here:

Look Ahead: Physics Programming in the Real World

Physics simulators are important for achieving realistic physical effects in video games and other simulations.  These systems rely on mathematical modeling like the basic particle acceleration we are modeling in the Particle class.  In a video game, you might imagine that the physical models are larger and more complex, and the number of Particles is dramatically higher.  As a result, it takes increasing amounts of processor power to perform these calculations.

Instead of continually producing chips that simply execute programs faster and faster (as the industry used to do), an important emerging trend in the processor industry has been to increase the number of processors on a chip, so processing work can be done in parallel.  The Intel Core 2 Duo is an example of a chip with two processing cores, and chips like these are now standard in new home computers.  The Cell processor in the Sony PlayStation 3 is an example of how the video console industry is following the same pattern.  However, computer programs have to be designed specifically to take advantage of this parallel processing power.

Consequently, real physics systems, like you would find in video game engines, divide the work up into smaller tasks and have multiple units called "threads" work on different parts simultaneously.  In fact, the Animator code in this project is implemented this way.  One thread spends all its time handling the physics calculations, and another thread handles drawing the graphics and taking user input.  One effect this has in our Animator, or in video games, is to allow the simulation code to do most of the work while the graphics code can keep the frame rate (the rate at which graphics are drawn) relatively constant, meaning that the graphics can appear to the eye to be moving smoothly and continuously, when in fact they're being erased and redrawn very rapidly on the screen.

Understanding the difficult problems and challenges associated with writing multi-threaded applications is a major part of CS 537, Introduction to Operating Systems.  Video game design and development is taught in CS 642.

Documentation

For this assignment you are required to generate javadoc documentation (i.e., the html file) for your Particle class. See the section on Generating documentation in Lab 3 for information on how to use Eclipse to generate Javadoc webpages from your source files. Don't forget to look at your Javadoc documentation before you turn it in to make sure it is what you expect!

Make sure also to follow the CS 302 standards for style as well as commenting.

Questions to answer

This section lists several questions that should deepen your understanding of the assignment.  Create a text-only file named questions.txt and answer these questions.  Note that some of the questions ask you to try things out in code and give or explain the results you get.  While you can modify your AnimationPlayer class to do this, it is recommended that you make another Java application class and put the code for you use to answer the questions in this new class.

Be sure to include the descriptive information listed below as well as the answers to each question:

Assignment Number:
Assignment Name:
Date Completed:

Partner 1 Name:
Partner 1 Login:
Partner 1 Lecturer's Name:
Partner 1 Lab Section:

Partner 2 Name:
Partner 2 Login:
Partner 2 Lecturer's Name:
Partner 2 Lab Section:
  1. What happens if you try to call playHole() with a position or velocity that is out of bounds?
  2. Instantiate a Particle at the origin with no velocity and let it fall for 3 seconds by calling passTime 6 times with 0.5 seconds each time. What results do you get (i.e., what are the particle's x and y position and velocity at the end)?  Now run the same experiment again but this time only use a single call to passTime with 3.0 seconds (and give your results). How do the results compare to those you got in the first case? Explain why this phenomenon occurs.
  3. Suppose you wanted to simulate getting holes-in-one for holes 1, 7, and 9 on a planet with 1/10th the gravity of earth.  Which of the classes (Particle, Animator, AnimationPlayer) would need to be modified?  Justify your answer.
  4. Suppose a programmer wants to update the Animator class so that the initial position and velocity of the ball is displayed in the animation window (in the same format used by the print() method).  Could the print() method of Particle class be used to do this (without modifying the source code for Particle)?  If yes, explain why; if not, explain and suggest a solution (which can involve modifying the Particle class).
  5. Have an Animator object animate the same hole twice with two distinct but identical Particles (i.e., same position and velocity, but different objects). Observe what happens when you run the program. Now create just one Particle object and use it in both animations (without calling reset() in between them). Form a guess as to whether or not the same thing will happen. Now observe the results. What happened? Was it what you expected? Why do you suppose it turned out this way?
  6. Modify the Particle's passTime method to do absolutely nothing (you can do this by making that section of your code a multi-line comment temporarily). Then run one of the golf animations. What eventually happens? Now run just the fireworks (with no golf holes). What do you observe? (Don't forget to change your passTime method back!)

Assignment requirements

This section outlines the major requirements of the assignment.  Your solution to the assignment must meet each of these requirements.  Be sure to read the announcements frequently and ask questions if you need clarification.

  1. Include name, login, lecture, and lab section information at the top of all assignment files.
  2. Follow the style and commenting standards for CS 302.  This includes:
  3. Follow the Particle specifications exactly, i.e., each method you implement must have the correct spelling, the correct return type, and the correct parameter type(s) and must behave according to the specifications.
  4. The ParticleTester class must contain code to test every method of the Particle class.
  5. The expected.out file must contain the values that you expect the ParticleTester class to print out.
  6. The AnimationPlayer class must use the Particle class and the Animator class to display the animations as specified above.
  7. Submit your answers to the questions in a text-only file (no Word documents) named questions.txt.
  8. Use the Handin program to hand in all work electronically.

Hints


Handin

Use the Handin program to hand in your work (see the instructions on how to use the Handin program). Students working in pairs will only submit one copy of all program files, except the README file described below.

Only hand in the files listed below; do not hand in any other files.  These files may be handed in any time prior to the due date.  Note: you may hand in your files before the deadline as many times as you wish.  We suggest you use your handin directory to keep your most recent working copy as you develop your solution.

Required files for this assignment


Feedback, questions, or accessibility issues: contact Rebecca Hasti (hasti@cs.wisc.edu)
©2007 Michael Kowalczyk, Sayandeep Sen, Jongwon Yoon, Zhenxiao Luo, and Rebecca Hasti, all rights reserved