Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

//Convert the following java program to C++ 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

//Convert the following java program to C++

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 landingQueue = new LinkedBlockingQueue(); Queue takeoffQueue = new LinkedBlockingQueue(); 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); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

DB2 Universal Database V7.1 Application Development Certification Guide

Authors: Steve Sanyal, David Martineau, Kevin Gashyna, Michael Kyprianou

1st Edition

0130913677, 978-0130913678

More Books

Students also viewed these Databases questions

Question

18. If you have power, then people will dislike and fear you.

Answered: 1 week ago