In this project, you are asked to simulate airplane landing and takeoff at an ai
ID: 3529137 • Letter: I
Question
In this project, you are asked to simulate airplane landing and takeoff at an airport. We will consider a small airport with only one runway. In each unit of time, one plane can land or one plane can takeoff, but not both. Planes arrive ready to takeoff or land at random times, so at any given moment of time, the runway may be idle or a plane may be landing or taking off, and there may be several planes waiting either to land or takeoff.
In simulating the airport, it will be useful to create a class Plane whose objects represent individual planes. This class will definitely need an initialization method and methods to represent takeoff and landing. You will also need to use a class Runway to hold information about the state and operation of the runway. This class will maintain members representing queues of planes waiting to land and takeoff.
In our simulation, we shall be especially concerned with the amounts of time that planes need to wait in queues before taking off or landing. Therefore, the measurement of time will be of utmost importance to our program. We shall divide the time period of our simulation into units in such a way that just one plane can use the runway, either to land or takeoff, in any given unit of time. The precise details of how we handle the landing and takeoff queues will be dealt with when you program the Runway class. Similarly, the precise methods of describing the operation of a Plane are not needed in the main program.
A key step in our simulation is to decide, at each time unit, how many new planes become ready to land and take off. Although there are many ways in which these decisions can be made, one of the most interesting and useful is to make a random decision. When the program is run repeatedly with random decisions, the results will differ from run to run, and with sufficient experimentation, the simulation may display a range of behavior not unlike that of the actual system being studied.
The Runway class needs to maintain two queues of planes, which we shall call landing and takeoff, to hold waiting planes. It is better to keep a plane waiting in the ground than in the air, so a small airport allows a plane to take off only if there are no planes waiting to land. Hence, our Runway method activity, which controls access to the Runway, will first service the head of the Queue of planes waiting to land, and only if the landing Queue is empty will it allow a Plane to take off. The aim of the simulation is to gather data about likely airport use. You are to use the class Runway itself to keep statistics such as the number of planes processed, the average time spent waiting, and the number of planes (if any) refused service. These details should be reflected in the various data members of the following Runway class definition.
The Plane class needs to maintain data about particular Plane objects. This data must include a flight number, a time of arrival at the airport system, and a Plane status as either arriving or departing.
The user must supply the number of time intervals the simulation is to run, the expected number of planes arriving, the expected number of planes departing per time interval, and the maximum allowed size for runway queues. The program should perform a random simulation of the airport, showing the status of the runaway at each time interval, and prints out a summary of airport operation at the conclusion.
Explanation / Answer
import java.util.Queue;
import java.util.Scanner;
import java.util.concurrent.LinkedBlockingQueue;
public class AirportSimulation {
public static boolean isPlaneComingIntoQueue(int avgInterval){
if(Math.random() < (1.0 / avgInterval))
return true;
else
return false;
}
public static boolean isPlaneCrashed(int in, int out, int interval){
if(out - in > interval){
return true;
}
else{
return false;
}
}
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.print("The amount of time needed for one plane to land: ");
int landTime = in.nextInt();
System.out.print("the amount of time needed for one plane to take off: ");
int takeoffTime = in.nextInt();
System.out.print("the average amount of time between arrival of planes to the landing queue: ");
int avgArrivalInterval = in.nextInt();
System.out.print("the average amount of time between arrival of planes to the takeoff queue: ");
int avgDepartureInterval = in.nextInt();
System.out.print("the maximum amount of time that a plane can stay in the landing queue without running out of fuel and crashing: ");
int crashLimit = in.nextInt();
System.out.print("the total length of time to be simulated: ");
int totalTime = in.nextInt();
int totalTimeSpentInLandingQueue = 0, totalTimeSpentInTakeoffQueue = 0;
int numPlanesLanded = 0, numPlanesTookoff = 0;
int numPlanesCrashed = 0;
Queue<Integer> landingQueue = new LinkedBlockingQueue<Integer>();
Queue<Integer> takeoffQueue = new LinkedBlockingQueue<Integer>();
for(int i = 0; i < totalTime; ++i){
if(isPlaneComingIntoQueue(avgArrivalInterval)){
landingQueue.add(i);
}
if(isPlaneComingIntoQueue(avgDepartureInterval)){
takeoffQueue.add(i);
}
while(true){
while(!landingQueue.isEmpty() && isPlaneCrashed(landingQueue.peek(), i, crashLimit)){
landingQueue.remove();
numPlanesCrashed++;
}
if(!landingQueue.isEmpty()){
int nextPlane = landingQueue.peek();
landingQueue.remove();
numPlanesLanded++;
totalTimeSpentInLandingQueue += (i - nextPlane);
int j;
for(j = i; j < landTime + i && j < totalTime; ++j){
if(isPlaneComingIntoQueue(avgArrivalInterval)){
landingQueue.add(j);
}
if(isPlaneComingIntoQueue(avgDepartureInterval)){
takeoffQueue.add(j);
}
}
i = j;
if(i >= totalTime){
break;
}
}
else{
break;
}
}
if(!takeoffQueue.isEmpty()){
int nextPlane = takeoffQueue.peek();
takeoffQueue.remove();
numPlanesTookoff++;
totalTimeSpentInTakeoffQueue += (i - nextPlane);
int j;
for(j = i; j < takeoffTime + i && j < totalTime; ++j){
if(isPlaneComingIntoQueue(avgArrivalInterval)){
landingQueue.add(j);
}
if(isPlaneComingIntoQueue(avgDepartureInterval)){
takeoffQueue.add(j);
}
}
i = j;
}
}
while(!landingQueue.isEmpty() && isPlaneCrashed(landingQueue.peek(), totalTime, crashLimit)){
landingQueue.remove();
numPlanesCrashed++;
}
System.out.println("The number of planes that took off in the simulated time is " + numPlanesTookoff);
System.out.println("the number of planes that landed in the simulated time is " + numPlanesLanded);
System.out.println("the number of planes that crashed because they ran out of fuel before they could land is " + numPlanesCrashed);
System.out.println("the average time that a plane spent in the takeoff queue is " + totalTimeSpentInTakeoffQueue / (double)numPlanesTookoff);
System.out.println("the average time that a plane spent in the landing queue is " + totalTimeSpentInLandingQueue / (double)numPlanesLanded);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.