Use C++ (Queues): Write a program that simulates the operation of a busy airport
ID: 3731765 • Letter: U
Question
Use C++ (Queues):
Write a program that simulates the operation of a busy airport that has only two runways
to handle all takeoffs and landings. You may assume that each takeoff or landing
takes 15 minutes to complete. One runway request is made during each five-minute time
interval, and the likelihood of a landing request is the same as for a takeoff request.
Priority is given to planes requesting a landing. If a request cannot be honored, it is
added to a takeoff or landing queue.
Your program should simulate 120 minutes of activity at the airport. Each request
for runway clearance should be time-stamped and added to the appropriate queue. The
output from your program should include the final queue contents, the number of takeoffs
completed, the number of landings completed, and the average number of minutes
spent in each queue
Explanation / Answer
Solution:
code:
// import required packages
import java.io.*;
import java.util.Queue;
import java.util.Collections;
import java.util.*;
import java.util.concurrent.LinkedBlockingQueue;
// class to simulate the airport runway
public class AirportSimulation
{
// method to check the arrival of flight
public static boolean isPlaneComing(int avgintrvl)
{
if(Math.random() < (1.0 / avgintrvl))
return true;
else
return false;
}
// main method to simulate the landing and takeoff
public static void main(String[] args)
{
// declare the required variables
int total_Tm =120;
int max_land_takeoff=15;
int totalTmInLanding = 0, totalTmInTakeoff = 0;
int Landingcount = 0, Takeoffcount = 0;
// create a queue for landing and takeoff
Queue<Integer> landingQueue = new LinkedBlockingQueue<Integer>();
Queue<Integer> takeoffQueue = new LinkedBlockingQueue<Integer>();
for(int lp = 0; lp < total_Tm; ++lp)
{
// check the arrival of flight for landing
if(isPlaneComing(max_land_takeoff))
{
landingQueue.add(lp);
}
// // check the arrival of flight for takeoff
if(isPlaneComing(max_land_takeoff))
{
takeoffQueue.add(lp);
}
// loop to compute number of landing and takeoff
while(true)
{
// Check landing queue is empty
if(!landingQueue.isEmpty()){
int nextPlane = landingQueue.peek();
landingQueue.remove();
Landingcount++;
totalTmInLanding += (lp - nextPlane);
int lj;
for(lj = lp; lj < max_land_takeoff + lp && lj < total_Tm; ++lj){
if(isPlaneComing(max_land_takeoff)){
landingQueue.add(lj);
}
if(isPlaneComing(max_land_takeoff)){
takeoffQueue.add(lj);
}
}
lp = lj;
if(lp >= total_Tm){
break;
}
}
else{
break;
}
}
// check takeoff queue is empty
if(!takeoffQueue.isEmpty()){
int nextPlane = takeoffQueue.peek();
takeoffQueue.remove();
Takeoffcount++;
totalTmInTakeoff += (lp - nextPlane);
int lj;
for(lj = lp; lj < max_land_takeoff + lp && lj < total_Tm; ++lj){
if(isPlaneComing(max_land_takeoff)){
landingQueue.add(lj);
}
if(isPlaneComing(max_land_takeoff)){
takeoffQueue.add(lj);
}
}
lp = lj;
}
}
/// display the result
System.out.println("The number of planes that took off in the simulated time is " + Takeoffcount);
System.out.println("the number of planes that landed in the simulated time is " + Landingcount);
System.out.println("the average time that a plane spent in the takeoff queue is " + totalTmInTakeoff / (double)Takeoffcount);
System.out.println("the average time that a plane spent in the landing queue is " + totalTmInLanding / double)Landingcount);
System.out.println("Contents of takeoff queue ");
for (int element : landingQueue)
{
System.out.println(element);
}
System.out.println("Contents of Landing queue " );
for (int element : takeoffQueue)
{
System.out.println(element);
}
}
}
I hope this helps if you find any problem. Please comment below. Don't forget to give a thumbs up if you liked it. :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.