Question
Using JAVA and a queue ADT implement the enqueue method, dequeue method, getFront method, isEmpty method, and a toString method. Write a program that uses
Using JAVA and a queue ADT implement the enqueue method, dequeue method, getFront method, isEmpty method, and a toString method. Write a program that uses the queue ADT to simulate an airport that follows these simple rules: There is 2 runways and only one airplane at a time can take off meaning they will take turns taking off. The airplanes alternate between runways - unless one runway is empty.
I've put code i've already started below.
----------------------------------------------------------------------------
public class Airplane {
String airline;
int flightNumber;
public Airplane(String airline, int flightNumber) {
this.airline = airline;
this.flightNumber = flightNumber;
}
public String getAirline() {
return airline;
}
public void setAirline(String airline) {
this.airline = airline;
}
public int getFlightNumber() {
return flightNumber;
}
public void setFlightNumber(int flightNumber) {
this.flightNumber = flightNumber;
}
@Override
public String toString() {
return "Airplane [airline=" + airline + ", flightNumber=" + flightNumber + "]";
}
}
---------------------------------------------------------------------------
/**
a * A simple implementation of the Queue ADT using a nested Node class and a doubly linked list
* @param
*/
public class LinkedQueue
Node front;
Node back;
public LinkedQueue() {
front = null;
back = null;
}
public void enqueue(T obj) {
if (isEmpty()) {
Node node = new Node(obj, null, null);
front = node;
back = node;
} else {
Node node = new Node(obj, null, back);
back.setNext(node);
back = node;
}
}
public T dequeue() {
// Need to implement dequeue
return null;
}
public T getFront() {
// Need to implement getFront
return null;
}
public boolean isEmpty() {
// Need to implement isEmpty
return false;
}
@Override
public String toString() {
// Need to implement toString
return "";
}
// Nested class Node
private class Node {
T data;
Node next;
Node prev;
// Constructor for inner class Node
Node(T data, Node next, Node prev) {
this.data = data;
this.next = next;
this.prev = prev;
}
// Setters & Getters for inner class Node
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
public Node getPrev() {
return prev;
}
public void setPrev(Node prev) {
this.prev = prev;
}
}
}
---------------------------------------------------------------------
public class AirportSimulation {
static LinkedQueue
static LinkedQueue
static Airplane plane1 = new Airplane("AA", 4992);
static Airplane plane2 = new Airplane("DL", 1345);
static Airplane plane3 = new Airplane("AA", 4531);
static Airplane plane4 = new Airplane("DL", 9873);
static Airplane plane5 = new Airplane("AA", 3414);
static Airplane plane6 = new Airplane("DL", 2366);
public static void main(String[] args) {
// Commnent/uncomment as needed for testing
// Flights only in runway1
//testCase1();
// Flights only in runway1
//testCase2();
// Flights in both runways test 1
//testCase3();
// Flights in both runways test 2
//testCase4();
// Need to change code so that each runway alternates if planes are waiting on both runways
runwayStatus();
while (!runway1.isEmpty()) {
System.out.println("Dequeueing from runway 1...");
runway1.dequeue();
runwayStatus();
}
while (!runway2.isEmpty()) {
System.out.println("Dequeueing from runway 2...");
runway2.dequeue();
runwayStatus();
}
// Need to change code so that runway 1 has the ability that it can send two flights at the same time.
}
public static void runwayStatus() {
System.out.println(" ------------------------------------------------");
System.out.println("---- Current runway status ---------------------");
System.out.println("Runway 1: " + runway1);
System.out.println("Runway 2: " + runway2);
System.out.println("------------------------------------------------");
}
public static void testCase1 () {
runway1.enqueue(plane1);
runway1.enqueue(plane2);
runway1.enqueue(plane3);
runway1.enqueue(plane4);
runway1.enqueue(plane5);
runway1.enqueue(plane6);
}
public static void testCase2 () {
runway2.enqueue(plane1);
runway2.enqueue(plane2);
runway2.enqueue(plane3);
runway2.enqueue(plane4);
runway2.enqueue(plane5);
runway2.enqueue(plane6);
}
public static void testCase3 () {
runway1.enqueue(plane1);
runway1.enqueue(plane2);
runway1.enqueue(plane3);
runway2.enqueue(plane4);
runway2.enqueue(plane5);
runway2.enqueue(plane6);
}
public static void testCase4 () {
runway1.enqueue(plane1);
runway1.enqueue(plane2);
runway1.enqueue(plane3);
runway1.enqueue(plane4);
runway1.enqueue(plane5);
runway2.enqueue(plane6);
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started