CS 201: Data Structures (Winter 2017)

HW02: Lunar lander

Due: Friday, 01/13 at 22:00

This is a pair programming assignment. If you are on a team, this means that you and your partner should be doing the entirety of this assignment side-by-side, on a single computer, where one person is "driving" and the other is "navigating." Take turns every so often who is driving; you should each spend approximately 50% of the time driving.

Review the pair programming guidelines.

1. Introduction

Computer simulation is used by engineers to test out ideas before actually building expensive machines or putting people in dangerous situations. Simulation is a critical part of the the space program by NASA, for example.

For this program, you will create a simulation of a vehicle landing on the moon. (It turns out that this assignment is also a rather old computer game that has been around for decades.)

Here is how it works: You are in control of a lunar lander ship, descending to the surface of the moon for a landing. Gravity steadily accelerates your ship faster and faster toward the surface of the moon. You, the astronaut piloting the ship, have a single control: a button with the label "thrust" on it. Applying thrust slows your ship down. Your goal is to get your ship to land on the moon at a slow enough speed so that it doesn't crash on impact. What's the catch? You have only a limited amount of fuel. If you slow down your ship too much too early, you will run out of fuel and crash into the surface of the moon.

Your mission: Program this simulation in Java.

2. Requirements

You should create two classes: LunarLander and Simulation. The LunarLander class represents the ship itself, and is the class that you will create an object from. The Simulation class is the one that has the main method in it, and controls how the simulation works. In your LunarLander class, you will need to keep the following information in instance variables:

Your LunarLander object should have the following methods (you may have more if you wish):

  1. Expend fuelUnits of fuel from the fuel tank. Make sure you check to see if you actually have this much fuel. If not, burn all that you have.
  2. For each unit of fuel that you burn, call the thrust() method to decrease your velocity.
  3. Determine the new altitude for your lander. Your new altitude is your old altitude minus your velocity. Note that gravity adds 2 meters/second to your velocity every second, which you should calculate before updating your altitude.

Your Simulation class should have one method: public static void main(String[] args). This method should create a LunarLander object, provide the player with a welcome message, then repeatedly show the player the current information for the lander followed by a question as to how many units of thrust to burn. If the lander ends up hitting the surface of the moon with a velocity of 4 meters/second or less, the ship lands successfully! If the lander hits the surface with a velocity of 5 meters/second or more, the ship crashes. You'll find a sample game at the bottom of this assignment.

3. Bonus fun

If you want to push yourself further, create an alternative simulation class called SimulationRace that is very similar to Simulation but has two LunarLander objects descending to the moon, each controlled by different users. The two players alternate back and forth entering thrust for their own landers, until one them lands or until they both crash. I'm leaving the specifications of this fairly vague: have fun with it.

4. Hints, suggestions, etc.

Here is a sample simulation:

Welcome to Lunar Lander! Alt = 1000 Vel = 40 Fuel = 25 How much thrust this round? 0 Alt = 958 Vel = 42 Fuel = 25 How much thrust this round? 0 Alt = 914 Vel = 44 Fuel = 25 How much thrust this round? 0 Alt = 868 Vel = 46 Fuel = 25 How much thrust this round? 0 Alt = 820 Vel = 48 Fuel = 25 How much thrust this round? 0 Alt = 770 Vel = 50 Fuel = 25 How much thrust this round? 0 Alt = 718 Vel = 52 Fuel = 25 How much thrust this round? 0 Alt = 664 Vel = 54 Fuel = 25 How much thrust this round? 0 Alt = 608 Vel = 56 Fuel = 25 How much thrust this round? 0 Alt = 550 Vel = 58 Fuel = 25 How much thrust this round? 0 Alt = 490 Vel = 60 Fuel = 25 How much thrust this round? 0 Alt = 428 Vel = 62 Fuel = 25 How much thrust this round? 0 Alt = 364 Vel = 64 Fuel = 25 How much thrust this round? 0 Alt = 298 Vel = 66 Fuel = 25 How much thrust this round? 0 Alt = 230 Vel = 68 Fuel = 25 How much thrust this round? 0 Alt = 160 Vel = 70 Fuel = 25 How much thrust this round? 2 Alt = 96 Vel = 64 Fuel = 23 How much thrust this round? 0 Alt = 30 Vel = 66 Fuel = 23 How much thrust this round? 0 Alt = -38 Vel = 68 Fuel = 23 Oh no, you crashed!

5. Submission and grading

Your program must consist of at least the files LunarLander.java and Simulation.java, but you may write others. Please make sure that every file you submit has your name and your partner's name at the top.

Submit all of the Java source files that you wrote that are necessary to make your program work. Do not submit any .class files for the classes that you write.

To submit your files, prepare a zip (or tar) archive. Name the file [yourUsername]-[yourPartner'sUsername] (this makes it easier for the grader). Also, make sure to not include extraneous files or directories. When you're satisfied that your submission is ready, one of you should upload the zip file to Moodle.

Grading

Start early, have fun, and discuss questions on Moodle.