In: Computer Science
What we want the program to do: We need to write a program, in Java, that will let the pilot issue commands to the aircraft to get it down safely on the flight deck. The program starts by prompting (asking) the user to enter the (start) approach speed in knots. A knot is a nautical mile and is the unit used in the navy and by the navy pilots. After the user enters the approach speed, the user is then shown the menu and the pilot picks a command. The effect of this command is then calculated and the updated information about the aircraft is displayed. If the aircraft has crashed or has landed, then the information is different. If the aircraft does not land, overshoot or crash, then the program presents the menu again and the user makes another choice (or the same choice again) and the program calculates the updated position of the aircraft and displays the information to the pilot (the user)' This continues until the aircraft successfully lands on the flight desk, the aircraft crashes or the aircraft overshoots the landing area. To successfully land, the aircraft must have its hook down and its gear down when it is in the valid landing zone. To be in the correct landing zone with the hook up and gear down, means the aircraft doesn't stop in time and has to go around, this is known as an overshoot. To be in the correct landing zone with the gear (landing gear) up means that the aircraft crashes. (Whether the hook is up or down doesn't matter) At any time, the user can issue the command Eject. This displays a message and stops the program, the aircraft crashes into the ocean. The commands and the changes they make are listed below. Note that commands must be case insensitive. That is, HOOK Down and hook dOWN must have the same effect. If the aircraft is still on approach, that is, it hasn't landed, overshot or crashed, the new height is shown along with the distance (which is to the back (stern) of the flight deck) and the glide path angle. The starting state of the aircraft is gear up, hook up and flaps level (= 0). An invalid command should not change anything about the conditions of the plane.
Sample starting run;
/*
Run 1: > java Ball
Enter speed (in knots) >> 120 //System in
Starting distance from carrier = 1000.00 feet
Starting height from carrier deck = 65.00 feet
Starting angle = 3.73 degrees
Flight Menu
Gear up
Gear down
Hook up
Hook down
Flaps up
Flaps down
Maintain
Power up
Power down
Eject
*/
To land an aircraft, the aircraft needs to begin its approach 65 feet above the height of the carrier deck and 1000 feet from the stern of the ship. The speed can vary and is set by the pilot. Once the aircraft speed is set, it remains the same until the aircraft lands. This is known as trimming the aircraft. Anything that decreases the power of the plane, makes it lose height, anything that increases the power of the plane, increases the height, the speed stays the same. To land successfully on the flight deck of a carrier, the aircraft must touch down in the first 400 feet after it crosses over the stern. If the aircraft does not land in this distance, the aircraft has to accelerate down the flight deck and try again. This is known as a "go round" or a "bolter". To help the aircraft land on the deck, the plane deploys (lowers) a 15 foot long hook. This the arrestor hook and is designed to catch one of the three arrestors wires, strung across the deck. What this means is that the aircraft does not have to get down to 0 feet above the flight deck, so long as it is within 15 feet above the flight deck, then the hook will catch a wire and drag the plane down onto the deck. This is perfectly acceptable and counts as a good landing. (Carrier landings are often described as a controlled crash) A successful landing follows a glide path of 3 degrees. See the diagram on page 5. This will be modelled in the program as any angle between 1 and 5 degrees. There is an aid on the port (left) side of the flight deck, which consists of a Fresnel lens, otherwise known as the "Ball" As the pilot approaches the flight deck, the "Ball" shows different coloured lights If the angle is between 1 and 5 degrees (both inclusive), then the light is green, that means continue on the current glide, on track for a good landing. Amber, angle is between 5 (exclusive) and 10 (inclusive) means too high, the aircraft is going to overshoot the arrestor wires (recall that this is the first 400 feet of the flight deck starting at the stern (rear) of the ship). The pilot needs to do something to reduce the power of the aircraft to get it back to the correct flight path. If the distance is greater than 0, and the angle is between 10 (exclusive) and 15 (inclusive), the light is red (not blinking).
If the angle is less than 1 and the distance is greater than or equal to 0, then the light flashes blinking red (about to crash into the ship or the ocean) If the distance is greater than 0 and the angle is greater than 15, the light is blinking red, the aircraft is way too high. Only display the angle if the distance is greater than or equal to 0, once over the flight deck, do not show the angle or the colour of the light. Each time the pilot makes a change to the aircraft (and remember that at the very least it is always travelling forward towards the carrier) the pilot checks the updated status of the "Ball", and makes corrections if required.
How to work out the angle. One way is take the arctan of the angle formed by height and the distance. tan θ = a / b = height / distance what we want is the actual angle, so once we know tan θ, we need to take tan-1 to give us the figure for the angle in degrees. All of this uses methods that are in the java Math class that we have been using since the start of the semester. The Math class java method for finding the actual angle in degrees, from a tan figure, that is, doing tan-1 is atan(number) NOTE: all interactions with the Math class methods have to be in radians, not angles, even though we want the final result to be an angle in degrees. All calculations in this program need to be in feet, see the diagram above. The conversion factor, between knots per hour and feet per second is 1.688. Each time a valid command is issued the distance to the carrier deck is calculated. Each loop through the menu (valid commands only) represents 1 sec of time. So, for example, if the speed was set at 100 knots, then the aircraft would travel 100 * 1.688 feet in one second. Carrier sea level (0 feet) rear of the flight deck 65 ' above the carrier deck (height) 1000 ' horizontal distance glide path angle, should be 3 degrees aircraft starts here ' = feet
The new distance to the carrier would be distance = distance - (knots * 1.688) (new distance = old distance - ( knots * 1.688)
If this was after the first second, then at 100 knots, the new distance would be distance = 1000 - (100 * 1.688) = 1000 - 168.8 = 831.2 feet
The commands and their effects are listed below. Recall that the aircraft is always moving forward at the speed set at the start of the program. The allowed commands and the change they make: Gear Up This increases the height + 20 feet (this is a one off effect) If the gear is already up, then an appropriate message is displayed to the screen and the height is not changed. Gear Down This decreases the height 20 feet (this is a one off effect) If the gear is already down, then an appropriate message is displayed to the screen and the height is not changed. Hook up This increases the height + 10 feet (this is a one off effect) If the hook is already up, then an appropriate message is displayed to the screen and the height is not changed. Hook Down This decreases the height 10 feet (this is a one off effect) If the hook is already down, then an appropriate message is displayed to the screen and the height is not changed. Flaps Up This increases the height + 15 feet (this is a one off effect) If the flaps are already fully up, then an appropriate message is displayed to the screen. The height is changed by +15, even if the flaps are already fully up. Flaps Down This decreases the height 15 feet (this is a one off effect) If the flaps are already fully down, then an appropriate message is displayed to the screen. The height is changed by - 15, even if the flaps are already fully down. To go from flaps fully up to flaps fully down requires 2 steps, the first time the Flaps down command is issued the flaps go to level, then the next time the flaps down command is issued, they go to fully down. If the flaps are level, they do not change the height on the next time through the menu. This is the same for flaps up, if the flaps are fully down, then the command flaps up brings the flaps to level, the next flaps up command would see the flaps go from level to fully up. Maintain This command does not change the height, just flies straight and level. Note that the one thing this command must do is set the flaps to the level position. The distance changes. Power Up This command increases the height by 5 Only certain commands are allowed, these are entered as text (not numbers) Invalid commands should be ignored
Power Down This command decreases the height by 5 Eject ends the program, after a screen message The distance, angle and height figures must be displayed to 2 decimal places. Your program must correctly deal with the user entering an invalid command (not one of the commands listed above). If the user enters an invalid command, an appropriate message is displayed to the screen, and nothing is changed, just go straight back to the menu again. The program ends when the aircraft lands, crashes or overshoots, or the user selects the Eject command. Sample runs of the program are included below (user input is in bold): (Note: not all options, functionality and messages are shown.)
1) Run 1: > java Ball
Enter speed (in knots) >> 120
Starting distance from carrier = 1000.00 feet
Starting height from carrier deck = 65.00 feet
Starting angle = 3.73 degrees
Flight Menu
Gear up
Gear down
Hook up
Hook down
Flaps up
Flaps down
Maintain
Power up
Power down
Eject
2) Enter command >> power down
Distance from carrier = 797.44 feet
Height from carrier deck = 60.00 feet
Angle = 4.32 degrees
Light showing
GREEN
On the glide path
Flight Menu
Gear up
Gear down
Hook up
Hook down
Flaps up
Flaps down
Maintain
Power up
Power down
Eject
3) Enter command >> hook down
Distance from carrier = 594.88 feet
Height from carrier deck = 50.00 feet
Angle = 4.83 degrees Light showing
GREEN
On the glide path
Flight Menu
Gear up
Gear down
Hook up
Hook down
Flaps up
Flaps down
Maintain
Power up
Power down
Eject
4) Enter command >> power down
Distance from carrier = 392.32 feet
Height from carrier deck = 45.00 feet
Angle = 6.60 degrees Light showing
AMBER
TOO HIGH!!! Reduce power
Flight Menu
Gear up
Gear down
Hook up
Hook down
Flaps up
Flaps down
Maintain
Power up
Power down
Eject
5) Enter command >> hook down
Hook is already down!
Flight Menu
Gear up
Gear down
Hook up
Hook down
Flaps up
Flaps down
Maintain
Power up
Power down
Eject
6) Enter command >> gear down
Distance from carrier = 189.76 feet
Height from carrier deck = 25.00 feet
Angle = 7.59 degrees
Light showing
AMBER TOO HIGH!!!
Reduce power
Flight Menu
Gear up
Gear down
Hook up
Hook down
Flaps up
Flaps down
Maintain
Power up
Power down
Eject
7) Enter command >> flaps down
Distance from carrier = -12.80 feet
Height from carrier deck = 10.00 feet
Congratulations!!
Landing on a carrier deck is the hardest thing
well done
import java.lang.*;
import java.util.*;
public class Ex
{
public static void getA(double x,double y)
{
if(x>=1 &&
x<=5)
{
System.out.println("Light Showing");
System.out.println("GREEN");
System.out.println("On the glide path");
}
else if(x>5 &&
x<=10)
{
System.out.println("Light showing");
System.out.println("AMBER");
System.out.println("TOO HIGH!!! Reduce power");
}
else if(x>10 &&
x<=15)
{
System.out.println("Light showing");
System.out.println("RED");
System.out.println("Not blinking");
}
else if(x<1 &&
y>=0)
{
System.out.println("Light showing");
System.out.println("Blinking RED");
System.out.println("about to crash into the ship or ocean");
}
else if(x>15 &&
y>0)
{
System.out.println("Light showing");
System.out.println("Blinking RED");
System.out.println("the aircraft way is too high");
}
}
public static void main(String[] args)
{
Scanner sc=new
Scanner(System.in);
double sd=1000.0;
double sh=65.00;
double ang=3.73;
System.out.println("Enter speed (in
knots)");
int speed=sc.nextInt();
System.out.println("Starting
distance from carrier="+sd+" feet");
System.out.println("Starting height
from carrier deck="+sh+" feet");
System.out.println("Starting
angle="+ang+" degrees");
System.out.println("Flight
Menu");
System.out.print("Gear up\nGear
down\nHook up\nHook down\nFlaps up\nFlaps down\nMaintain\nPower
up\nPower down\nEject\n");
String a[]=new String[10];
a[0]=Gear up;
a[1]=Gear down;
a[2]=Hook up;
a[3]=Hook down;
a[4]=Flaps up;
a[5]=Flaps down;
a[6]=Maintain;
a[7]=Power up;
a[8]=Power down;
a[9]=Eject;
int
gu=0,gd=0,hu=0,hd=0,fu=0,fd=0;
do
{
String ch;
System.out.println("Enter command");
String ip=sc.next();
for(int i=0;i<10;i++)
if(ip.equalsIgnoreCase(a[i]))
{
ch=a[i];
break;
}
switch(ch)
{
case "Gear
up":if(gu==1)
System.out.println("Gear is already up");
else
{
sd=sd-(speed*1.688);
sh=sh+20;
angle=java.lang.Math.atan(sh/sd);
System.out.println("Distance
from carrier="+sd+" feet");
System.out.println("Height
from carrier deck="+sh+" feet");
if(sd>=0)
{
System.out.println("Angle="+ang+" degrees");
getA(angle,sd);
}
gu=1;
}
break;
case "Gear
down":if(gd==1) System.out.println("Gear is already down");
else{
sd=sd-(speed*1.688);
sh=sh-20;
angle=java.lang.Math.atan(sh/sd);
System.out.println("Distance
from carrier="+sd+" feet");
System.out.println("Height
from carrier deck="+sh+" feet");
if(sd>=0)
{
System.out.println("Angle="+ang+" degrees");
getA(angle,sd);
}
gd=1;}
break;
case "Hook
up":if(hu==1) System.out.println("Hook is already up");
else
{sd=sd-(speed*1.688);
sh=sh+10;
angle=java.lang.Math.atan(sh/sd);
System.out.println("Distance
from carrier="+sd+" feet");
System.out.println("Height
from carrier deck="+sh+" feet");
if(sd>=0)
{
System.out.println("Angle="+ang+" degrees");
getA(angle,sd);
}
hu=1;}
break;
case "Hook
down":if(hd==1) System.out.println("Hook is already down");
else{sd=sd-(speed*1.688);
sh=sh-10;
angle=java.lang.Math.atan(sh/sd);
System.out.println("Distance
from carrier="+sd+" feet");
System.out.println("Height
from carrier deck="+sh+" feet");
if(sd>=0)
{
System.out.println("Angle="+ang+" degrees");
getA(angle,sd);
}
hd=1;}
break;
case "Flaps
up":sd=sd-(speed*1.688);
sh=sh+15;
angle=java.lang.Math.atan(sh/sd);
System.out.println("Distance
from carrier="+sd+" feet");
System.out.println("Height
from carrier deck="+sh+" feet");
if(sd>=0)
{
System.out.println("Angle="+ang+" degrees");
getA(angle,sd);
}
break;
case "Flaps
down":sd=sd-(speed*1.688);
sh=sh-15;
angle=java.lang.Math.atan(sh/sd);
System.out.println("Distance
from carrier="+sd+" feet");
System.out.println("Height
from carrier deck="+sh+" feet");
if(sd>=0)
{
System.out.println("Angle="+ang+" degrees");
getA(angle,sd);
}
break;
case
"Maintain":sd=sd-(speed*1.688);
angle=java.lang.Math.atan(sh/sd);
System.out.println("Distance
from carrier="+sd+" feet");
System.out.println("Height
from carrier deck="+sh+" feet");
if(sd>=0)
{
System.out.println("Angle="+ang+" degrees");
getA(angle,sd);
}
break;
case "Power
up":sd=sd-(speed*1.688);
sh=sh+5;
angle=java.lang.Math.atan(sh/sd);
System.out.println("Distance
from carrier="+sd+" feet");
System.out.println("Height
from carrier deck="+sh+" feet");
if(sd>=0)
{
System.out.println("Angle="+ang+" degrees");
getA(angle,sd);
}
break;
case "Power
down":sd=sd-(speed*1.688);
sh=sh-5;
angle=java.lang.Math.atan(sh/sd);
System.out.println("Distance
from carrier="+sd+" feet");
System.out.println("Height
from carrier deck="+sh+" feet");
if(sd>=0)
{
System.out.println("Angle="+ang+" degrees");
getA(angle,sd);
}
break;
case
"Eject":System.out.println("Distance from carrier="+sd+"
feet");
System.out.println("Height
from carrier deck="+sh+" feet");
System.out.println("Angle="+ang+" degrees");
System.out.print(Congratulations!!\nLanding on a carrier deck is
the hardest thing\nwell done");
break;
default:System.out.println("Invalid command");
}
if(ch=="Eject")
break;
if(sd<0 &&
ch!="Eject")
{
System.out.print(Congratulations!!\nLanding on a carrier deck is
the hardest thing\nwell done");
break;
}
}
}
}
//I have struck in evaluating flaps and i am pretty sure of remaining commands.