CIT 591 Assignment 2: Lunar Lander
Fall 2003, David Matuszek

Purposes of this assignment:

What you should know:

What you do not need to know:

General idea of the assignment:

Lunar Lander is one of the earliest computer games. With a proper choice of initial values, it is quite a bit of fun to play, even as a text-only program.

You are in a lunar module, some distance above the Moon's surface. Gravity is pulling you toward the Moon at an ever-increasing rate of speed. You have a limited amount of fuel to use, and each time you burn fuel, you reduce your speed by a certain amount. If you burn the right amount of fuel at the right time, you can land safely (that is, when you reach the Moon's surface, you are not going too fast).

The math:

Play progresses in a series of turns. At each turn, you are told:

You then get to specify:

At the end (which you can determine because your altitude becomes negative), you are either congratulated for a safe landing, or told how deep a crater you made (based on your speed).

At each turn, your velocity increases by a constant amount, due to the acceleration of gravity. Your velocity decreases by an amount proportional to the amount of fuel you just burned (zero at the first turn). Your altitude decreases by your velocity multiplied by the amount of "time" each turn takes. (We assume that each turn takes the same amount of time.) Your fuel decreases by the amount you burn, but of course you cannot burn more than you have, so the amount of fuel never becomes negative. (If you ask to burn more than you have, burn only as much as you do have, and set the amount remaining to zero.)

Classes and methods:

For these early programs, I will be telling you exactly what classes and methods to have. In later programs, you will have more responsibility for deciding these things.

public class LunarLanderGame

This class contains the public static void main(String[] args) method. In this method, you should (1) create a LunarLander object, (2) create an IOFrame object, and (3) send messages to these two objects in order to get the burn rate from the user, tell the lander what to do, find out the lander status, and report the status back to the user. When the lander's altitude is less than or equal to zero, the game ends (one way or the other).

When the game finishes, you should ask the user whether he/she would like to play again. Allow as many games as the user wants before quitting the program.

public class LunarLander

This class does not print or display anything. Displaying results is done elsewhere in the program. (Of course, you can use print statements as you are debugging, if necessary.)

LunarLander keeps track of its own altitude, velocity, and remaining fuel. It should have the following methods:

void burn(double amount)
Burns the amount of fuel requested (if that much is available). Computes new values for velocity, altitude, and remaining fuel.
double getFuelRemaining()
Returns the amount of fuel remaining.
double getAltitude()
Returns the current altitude.
double getVelocity()
Returns the current downward velocity (if moving upward, the velocity is negative).

It is up to you whether to write a constructor for LunarLander, or to use the default (Java-supplied) constructor.

public class IOFrame

This is a class that I have written for you; just copy the file from here (right-click to download) and add it to your program. This section explains how to use the class.

First, create an object of type IOFrame. When you create this object, it automatically puts a small window on your monitor screen. The constructor takes one parameter, a String, to use as the title of this window. Call this constructor with new IOFrame("your title, for example, Lunar Lander").

There are three sections to this window. The top two sections are each one line in height. When your program asks for input, it puts a message in the top section, and the user enters the requested input in the second section. The bottom (much larger) section is used to display output.

To request input, send your IOFrame object one of these messages:

getString(String prompt)
getInt(String prompt)
getDouble(String prompt)

depending on whether you want to read a String, an int, or a double, respectively. The prompt is the message to put in the top line of the window to tell the user what you want (For example, "How much fuel should I burn?"). The method will return a value of the appropriate type.

To display results, send your IOFrame object one of these messages:

display(String message)
This is like System.out.print(message), except that the message goes into the output area of the IOFrame.
displayLine(String message)
This is like System.out.println(message), except that the message goes into the output area of the IOFrame.
This clears (erases) the output area. Use this method between games.

Due date:

Your program is due before midnight, Thursday September 25. Zip up all files (.java, .class, and any extra files produced by BlueJ, such as .pkg and .pkh files), and submit via Blackboard. Please turn in one program for your pair, that is, you and your partner should not both turn in a copy.