To implement the Air Traffic Control tower simulation, we first need to create the Airplane
Java class based on the specifications you provided. Then, we will create a main class to handle the creation of airplane objects, user input, moving the airplanes, and printing the required details.
Here is a possible implementation in Java:
import java.util.Scanner;
class Airplane {
private String callSign;
private double distance; // in miles
private int bearing; // bearing direction
private int altitude; // in feet
// Default constructor
public Airplane() {
this.callSign = "AAA01";
this.distance = 1.0; // 1 mile away
this.bearing = 0; // 0° (North)
this.altitude = 0; // 0 feet
}
// Parameterized constructor
public Airplane(String cs, double dist, int dir, int alt) {
this.callSign = cs.toUpperCase(); // Uppercase call sign
this.distance = dist;
this.bearing = (dir < 0 || dir >= 360) ? (dir % 360 + 360) % 360 : dir; // Normalize bearing
this.altitude = alt; // No need for checks as per the problem statement
}
// Move the airplane
public void move(double dist, int dir) {
this.distance += dist;
this.bearing = (dir < 0 || dir >= 360) ? (dir % 360 + 360) % 360 : dir; // Ensure bearing is valid
}
// Increase altitude
public void gainAlt() {
this.altitude += 1000;
}
// Decrease altitude
public void loseAlt() {
this.altitude = Math.max(0, this.altitude - 1000);
}
// Get altitude
public int getAlt() {
return this.altitude;
}
// String representation
@Override
public String toString() {
return this.callSign + " - " + this.distance + " miles away at bearing " + String.format("%03d", this.bearing) + "°, altitude " + this.altitude + " feet";
}
// Calculate distance to another airplane
public double distTo(Airplane other) {
// This formula uses the law of cosines for calculating distances
double lat1 = Math.toRadians(this.bearing);
double lat2 = Math.toRadians(other.bearing);
return Math.sqrt(Math.pow(this.distance * Math.cos(lat1), 2) + Math.pow(other.distance * Math.cos(lat2), 2)
- 2 * (this.distance * Math.cos(lat1)) * (other.distance * Math.cos(lat2)) * Math.cos(Math.toRadians(this.bearing - other.bearing)));
}
}
public class AirTrafficControl {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Create the first and second airplanes
Airplane airplane1 = new Airplane();
Airplane airplane2 = new Airplane("AAA02", 15.8, 128, 30000);
// User input for the third airplane
System.out.println("Enter the details of the third airplane (call-sign, distance, bearing and altitude):");
String callSign = scanner.next();
double distance3 = scanner.nextDouble();
int bearing3 = scanner.nextInt();
int altitude3 = scanner.nextInt();
Airplane airplane3 = new Airplane(callSign, distance3, bearing3, altitude3);
// Initial positions
System.out.println("Initial Positions:");
System.out.printf("\"Airplane 1\": %s%n", airplane1);
System.out.printf("\"Airplane 2\": %s%n", airplane2);
System.out.printf("\"Airplane 3\": %s%n", airplane3);
// Initial distances
double dist12 = airplane1.distTo(airplane2);
double dist13 = airplane1.distTo(airplane3);
double dist23 = airplane2.distTo(airplane3);
System.out.println("Initial Distances:");
System.out.printf("The distance between Airplane 1 and Airplane 2 is %.2f miles.%n", dist12);
System.out.printf("The distance between Airplane 1 and Airplane 3 is %.2f miles.%n", dist13);
System.out.printf("The distance between Airplane 2 and Airplane 3 is %.2f miles.%n", dist23);
// Initial height differences
int altDiff1And2 = Math.abs(airplane1.getAlt() - airplane2.getAlt());
int altDiff1And3 = Math.abs(airplane1.getAlt() - airplane3.getAlt());
int altDiff2And3 = Math.abs(airplane2.getAlt() - airplane3.getAlt());
System.out.println("Initial Height Differences:");
System.out.printf("The difference in height between Airplane 1 and Airplane 2 is %d feet.%n", altDiff1And2);
System.out.printf("The difference in height between Airplane 1 and Airplane 3 is %d feet.%n", altDiff1And3);
System.out.printf("The difference in height between Airplane 2 and Airplane 3 is %d feet.%n", altDiff2And3);
// Move airplanes as per instructions
airplane1.move(dist23, 65); // Move Airplane 1 to 65°
airplane2.move(8.0, 135); // Move Airplane 2 to 135°
airplane3.move(5.0, 55); // Move Airplane 3 to 55°
airplane1.gainAlt(); // Increase altitude of Airplane 1 by 3000 feet
airplane2.loseAlt(); // Decrease altitude of Airplane 2 by 2000 feet
airplane3.loseAlt(); // Decrease altitude of Airplane 3 by 4000 feet
// New positions
System.out.println("New Positions:");
System.out.printf("\"Airplane 1\": %s%n", airplane1);
System.out.printf("\"Airplane 2\": %s%n", airplane2);
System.out.printf("\"Airplane 3\": %s%n", airplane3);
// New distances
dist12 = airplane1.distTo(airplane2);
dist13 = airplane1.distTo(airplane3);
dist23 = airplane2.distTo(airplane3);
System.out.println("New Distances:");
System.out.printf("The distance between Airplane 1 and Airplane 2 is %.2f miles.%n", dist12);
System.out.printf("The distance between Airplane 1 and Airplane 3 is %.2f miles.%n", dist13);
System.out.printf("The distance between Airplane 2 and Airplane 3 is %.2f miles.%n", dist23);
// New height differences
altDiff1And2 = Math.abs(airplane1.getAlt() - airplane2.getAlt());
altDiff1And3 = Math.abs(airplane1.getAlt() - airplane3.getAlt());
altDiff2And3 = Math.abs(airplane2.getAlt() - airplane3.getAlt());
System.out.println("New Height Differences:");
System.out.printf("The difference in height between Airplane 1 and Airplane 2 is %d feet.%n", altDiff1And2);
System.out.printf("The difference in height between Airplane 1 and Airplane 3 is %d feet.%n", altDiff1And3);
System.out.printf("The difference in height between Airplane 2 and Airplane 3 is %d feet.%n", altDiff2And3);
// Close the scanner
scanner.close();
}
}
Explanation:
-
Class Structure: The
Airplane
class encapsulates the relevant properties and behaviors of an airplane. It has constructors for default and parameterized initialization, methods to move the airplane and adjust its altitude, a method to calculate distance to another airplane, and a method for a textual representation. -
User Input: The
AirTrafficControl
class gets user input for the third airplane's properties. -
Distance Calculation: A simple method calculates the distance between airplanes based on their distances and bearings.
-
Altitude Management: The program allows for increasing and decreasing the altitude with the specified constraints.
-
Output: All relevant information is printed to the console in the specified format.
Important Note:
The distance calculation used in this sample code is simplistic and assumes flat Earth-like geometry for demonstration purposes. In a real-world application, more complex formulas involving trigonometry might be needed to accurately calculate distances between points based on geographical coordinates. This is a basic demonstration. Adjust the distance calculation as needed to reflect the actual aeronautical computations.
Make sure to run the program with various inputs to test its functionality!