Review Part 01 - Missile Defense
You're working for the US defense department on a missile defense program. The goal is to create a program that will shoot down any missile that comes within 30 miles of the White House. Unfortunately, the missile tracking reports you receive come from your allies in France who are reporting everything in kilometers.
You will write a program that will take a missile distance given in kilometers, convert it to miles, and decide whether or not to launch the interceptor.
The program will then ask you if there are more missiles in the air. If so, repeat the program. If not, end the program with a message of peace and congratulations.
Assignment
The following are example runs of the program. User input is underlined.
Super Secret Missile Defense Program
Enter the distance of the missile: _25_
Distance is 15.53 miles, fire!
Are there more missiles incoming? _N_
Congratulations! Peace on Earth!
Super Secret Missile Defense Program
Enter the distance of the missile: _100.7_
Distance is 62.57 miles, hold your fire.
Are there more missiles incoming? _Y_
Enter the distance of the missile: _20_
Distance is 12.43 miles, fire!
Are there more missiles incoming? _N_
Congratulations! Peace on Earth!
Requirements
- The program should display prompts and responses as given.
- Your program should have three methods besides
run
andmain
:
float getMissileDistance() {
// ...
}
float convertKilometersToMiles(float km) {
// ...
}
boolean shouldFireMissile(float distance) {
// ...
}
- The
getMissileDistance
method should prompt the user for the distance. - The
convertKilometersToMiles
method should do what it says. - The
shouldFireMissile
method should take a distance in miles and return a boolean value indicating whether or not the interceptor missile should be fired.
Hints
-
There are 0.62137 miles in a kilometer
-
Only fire the interceptor if the incoming missile is within 30 miles.
-
Work out what each method should do, then have
run
execute those methods in the proper order. -
Don't forget to add the ACM library.
-
Don't worry about the loop until you get everything else working.
-
Starter Code:
class Main extends ConsoleProgram {
public void run() {
}
public static void main(String[] args) {
new Main().start(args);
}
}
Stretch Goals:
If you get all of the above working, try the following stretch goals:
-
The army has developed a new, ultra missile. This missile is much faster than the standard missile, however they are expensive to manufacture and are in limited supply. Modify your program such that if the missile is between 0 and 10 miles away, you will use the super-fast missile, but if it is between 10 and 30 miles away, you'll use the standard missile.
-
New Simulations have shown that if the missile is less than 3 miles out, there is no chance of intercepting it, even with the ultra missile. Modify your program so that if the missile is 0 - 3 miles away, an evacuation order is given.