To implement the Airplane
class and the simulation for the Air Traffic Control tower, we will follow the requirements specified in your assignment. Below is a complete Python program that meets the specifications, including the Airplane
class definition and the logic to manage the airplanes.
import math
class Airplane:
def __init__(self, cs="AAA01", dist=1.0, dir=0, alt=0):
self.call_sign = cs.upper()
self.distance = dist
self.bearing = dir % 360
self.altitude = max(0, alt) # altitude cannot be negative
def move(self, dist, dir):
# Calculate new position
dir_rad = math.radians(dir)
delta_x = dist * math.sin(dir_rad)
delta_y = dist * math.cos(dir_rad)
# Convert polar coordinates to Cartesian for the new distance
new_x = delta_x + self.distance * math.sin(math.radians(self.bearing))
new_y = delta_y + self.distance * math.cos(math.radians(self.bearing))
# Update distance and bearing
self.distance = math.sqrt(new_x**2 + new_y**2)
self.bearing = int(math.degrees(math.atan2(delta_x, delta_y)) % 360)
def gainAlt(self):
self.altitude += 1000
def loseAlt(self):
self.altitude = max(0, self.altitude - 1000)
def getAlt(self):
return self.altitude
def toString(self):
return f"{self.call_sign} - {self.distance:.1f} miles away at bearing {self.bearing:03d}°, altitude {self.altitude} feet"
def distTo(self, other):
# Using the law of cosines
return math.sqrt(self.distance**2 + other.distance**2 - 2 * self.distance * other.distance *
math.cos(math.radians(other.bearing - self.bearing)))
def main():
# Creating Airplane 1 (default)
airplane1 = Airplane()
# Creating Airplane 2
airplane2 = Airplane("AAA02", 15.8, 128, 30000)
# Input for Airplane 3
user_input = input("Enter the details of the third airplane (call-sign, distance, bearing and altitude): ")
cs, dist, dir, alt = user_input.split()
dist, dir, alt = map(float, (dist, dir, alt))
airplane3 = Airplane(cs, dist, int(dir), int(alt))
# Initial Positions
print("Initial Positions:")
print(f'"Airplane 1": {airplane1.toString()}')
print(f'"Airplane 2": {airplane2.toString()}')
print(f'"Airplane 3": {airplane3.toString()}')
# Calculating initial distances
dist12 = airplane1.distTo(airplane2)
dist13 = airplane1.distTo(airplane3)
dist23 = airplane2.distTo(airplane3)
# Initial Distances
print("Initial Distances:")
print(f'The distance between Airplane 1 and Airplane 2 is {dist12:.2f} miles.')
print(f'The distance between Airplane 1 and Airplane 3 is {dist13:.2f} miles.')
print(f'The distance between Airplane 2 and Airplane 3 is {dist23:.2f} miles.')
# Initial Height Differences
diff12 = abs(airplane1.getAlt() - airplane2.getAlt())
diff13 = abs(airplane1.getAlt() - airplane3.getAlt())
diff23 = abs(airplane2.getAlt() - airplane3.getAlt())
print("Initial Height Differences:")
print(f'The difference in height between Airplane 1 and Airplane 2 is {diff12} feet.')
print(f'The difference in height between Airplane 1 and Airplane 3 is {diff13} feet.')
print(f'The difference in height between Airplane 2 and Airplane 3 is {diff23} feet.')
# Moving the airplanes
airplane1.move(dist23, 65) # Move Airplane 1
airplane2.move(8.0, 135) # Move Airplane 2
airplane3.move(5.0, 55) # Move Airplane 3
airplane1.gainAlt() # Increase altitude of Airplane 1
airplane2.loseAlt() # Decrease altitude of Airplane 2
airplane3.loseAlt() # Decrease altitude of Airplane 3
# New Positions
print("New Positions:")
print(f'"Airplane 1": {airplane1.toString()}')
print(f'"Airplane 2": {airplane2.toString()}')
print(f'"Airplane 3": {airplane3.toString()}')
# New Distances
new_dist12 = airplane1.distTo(airplane2)
new_dist13 = airplane1.distTo(airplane3)
new_dist23 = airplane2.distTo(airplane3)
print("New Distances:")
print(f'The distance between Airplane 1 and Airplane 2 is {new_dist12:.2f} miles.')
print(f'The distance between Airplane 1 and Airplane 3 is {new_dist13:.2f} miles.')
print(f'The distance between Airplane 2 and Airplane 3 is {new_dist23:.2f} miles.')
# New Height Differences
new_diff12 = abs(airplane1.getAlt() - airplane2.getAlt())
new_diff13 = abs(airplane1.getAlt() - airplane3.getAlt())
new_diff23 = abs(airplane2.getAlt() - airplane3.getAlt())
print("New Height Differences:")
print(f'The difference in height between Airplane 1 and Airplane 2 is {new_diff12} feet.')
print(f'The difference in height between Airplane 1 and Airplane 3 is {new_diff13} feet.')
print(f'The difference in height between Airplane 2 and Airplane 3 is {new_diff23} feet.')
if __name__ == "__main__":
main()
Explanation:
-
Airplane Class: The class has methods for moving airplanes, changing altitudes, calculating distances, and formatting output.
-
Main Function: The sequence involves creating airplanes, taking user input for a third airplane, calculating initial positions, distances, and height differences before and after moving the aircraft.
-
Input Handling: The user inputs the details for the third airplane, which are used to create an
Airplane
object. -
Distance Calculations: The distances between each pair of airplanes are calculated using trigonometric functions and the law of cosines.
-
Output Formatting: The output matches the requirements laid out in your assignment, ensuring that all details are printed as specified.
You can run this code in a suitable Python environment, and it will perform the simulation as required.