handout #5

CS227 ¾ Program Design & Development

Programming Assignment #2

due 3 pm Wednesday, September 8th

In this assignment you will write your own class definition to complete a game program that allows a user to try to safely land on the moon.  Your class will keep track of the current state of the lunar lander.  This assignment will count for a full 20 points.

Your class will keep track of the lander’s current altitude, speed and fuel.  To keep things simple, we will use simple ints for all calculations and updates will occur once every second.  In the lunar landing simulation, the user will have the option to request thrust from the lander’s engines.  For each thrust request that is made, a certain amount of fuel will be expended to generate a certain negative acceleration.  If the user makes more than one thrust request in a given second, then you apply the appropriate number of fuel units.  For example, if the user makes four thrust requests in a given second, then you burn four times the normal amount of fuel and give the lander four times the negative acceleration of a single thrust.

Your class is to be called LunarLander and must include the following public methods:

Method

Description

getAltitude()

returns the current altitude in meters

getVelocity()

returns the current velocity in meters/second

getFuel()

returns the current number of thrust units remaining

reset()

resets the lunar lander to the initial situation

thrust()

remember to apply one more unit of fuel on the next update (can be called multiple times between updates)

tick()

called once every second, should update the simulation variables appropriately, applying whatever thrust has been requested since the last update

In resetting the simulation, you will need to know what values to use initially for altitude, velocity and fuel.  The following constants should be used:

public static final int INITIAL_VELOCITY = 40;   // meters/second
public static final int INITIAL_ALTITUDE = 1000; // meters
public static final int INITIAL_FUEL     = 25;   // thrusts vfa

Assume that the reset method is called before the simulation begins.

Keep in mind that the thrust method should not update anything, it should just keep track of how many thrust units have been requested during the current second.  All updating should be done when method tick is called.  The method should first recompute the velocity.  Because we are computing once per second, this calculation is rather simple.  You should use the following constants for the calculation:

public static final int GRAVITY = 2;  // gravitational acceleration
                                      // in meters/second/second
public static final int THRUST  = 4;  // thrust acceleration in
                                      // meters/second/second

The first constant indicates that the Moon’s gravity will increase velocity by 2 meters/second every second of the simulation.  So to account for gravity, you simply add this number to the velocity once every second.  The second constant indicates that each thrust unit will decrease velocity by 4 meters/second.  For example, with one thrust unit, the overall effect is to decrease velocity by 2 (add 2 for gravity, subtract 4 for one thrust unit).  With two thrust units the velocity is decreased by 6 (add 2 for gravity, subtract 8 for two thrust units).  With three thrust units the velocity is decreased by 10 (add 2, subtract 12).  And so on.

Once you have updated the velocity in the tick method, you should recompute the altitude.  Because we are storing velocities as meters per second, you can simply subtract the velocity from the current altitude.  For example, if the altitude is 900 and the velocity is 54 meters/second, then you would reset the altitude to be 846 meters (900 minus 54) to account for one second of movement at that velocity.  You do not have to make a special case for negative velocities.  If the user has applied so much thrust that the velocity has gone negative, then the new altitude will be higher than the old one because you are subtracting a negative number.

Finally, once you have updated the velocity and altitude in the tick method, you should reset the fuel by subtracting the number of thrust units requested from the current available fuel.  This is a simple computation because we are expressing the fuel level in terms of thrust units (i.e., one unit is the amount of fuel necessary to produce one thrust unit).

You have to handle one minor problem in the tick method.  It is possible that the user will request too many thrust units.  For example, suppose the fuel level is at 3 units and the user requests 5 thrust units.  You have to limit the user to the number of fuel units available.  You would normally accomplish this in your code by using an if or if/else statement.  You are welcome to solve the problem that way, but there is a simpler way.  The Math class provides a method called min that returns the minimum of two integers, as in:

int x = 3, y = 5;
int z = Math.min(x, y);
// z would now be 3

Using this method, you can reset the thrust request to the smaller of the thrust request and the available fuel.  That way the user will never be allowed to use fuel that doesn’t exist.

There is a final constant defined in the LunarLander class, although you won’t need to use it in your code.  It keeps track of the maximum velocity for a safe landing:

public static final int SAFE_LANDING = 4;  // speed at which lander can
                                           // safely land in meters/second

The user interface uses this constant to determine whether the lander crashes or lands safely.

All of the constants described above should be public constants in the LunarLander class.  To make this easier for you, they have all been included in the class definition that you are to complete.  As in the previous assignment, you will be editing a file that has two classes: the lunar lander class and the class that includes method main.  You can ignore the class that includes method main.  Your task is to complete the lunar lander class by defining methods and variables that implement the behavior described above.

As in the previous assignment, there is a folder called prog2 in the cs227 source folder in the Harvill lab and a zip file on the download page of the class web page.  You are to edit the file LunarLanderMain.java.  Remember not to declare the class LunarLander to be public (only one class per file can be public, and the class with main needs to be public).  The files will not compile until you add stub definitions for the methods outlined on page 1.  In particular, if you fail to include one or more of the methods for the lunar lander class, then other classes like LunarLanderFrame, LunarInfoPanel and LunarPicture will fail to compile.

Be sure to provide a reasonable level of commenting.  Include a comment at the beginning with your name, grader and the date.  Also include a brief description of the lunar lander class.  Also comment every method that you define and use meaningful variable names.

Once your version compiles and runs properly, print the file LunarLanderMain.java.  You should turn this in during class.  You do not have to print the other files in the prog2 folder.