This lab will use the concept of a doubly-linked list to simulate part of an air
ID: 3860139 • Letter: T
Question
This lab will use the concept of a doubly-linked list to simulate part of an air traffic control system. The program will manage the arrival and departure of flights into and out of your airspace.
The job of air traffic controller involves taking responsibility for all of the aircraft in a specific area of the airspace. The airspace for this problem will be a piece of the traffic corridor between the cities of Buffalo and Boston, Ma. Other controllers will have responsibility for the area to the east, west, north and south of you but your area will be under your exclusive control.
The program will accept certain commands from you and will display certain information to you in response. The commands and their responses will be as follows:
Command: “Entering” – This command will signal that an aircraft has entered your airspace. You will be able to specify
The ID int of an aircraft
The altitude int thousands of feet
The speed int miles per hour
The name String (ex: “U566”)
Response: one aircraft will be added to your problem
Command: “Leaving” – you will give the aircraft name
Response: it will be removed from your problem
Command: “Show” – you will give the aircraft name
Response: all information for that aircraft will be displayed
Command: “Display” –
Response: all aircraft in your problem will be displayed in a list
Implementation:
You will create a doubly-linked list in Java. You will implement an “aircraft” class which will take the place of the node class which was discussed. Objects of this aircraft class will be linked together in a list, as they are entered into the problem. When the “Display” command is entered, all the aircraft currently on the list will be displayed to the user.
Remember:
Use object reference variables for links between nodes
Implement two classes
linked list class which will be used by main to manipulate objects of the aircraft class
an aircraft class whose objects will contain aircraft information.
The main program should be the place where all of the “conversation” with the user takes place. This means that;
The linked list object will be able to return a single aircraft object in response to the “Show” command and will return an array of aircraft objects in response to the “Display” command.
The aircraft class should have a method that returns a String, suitable for displaying all of its information (used in the “Show” command) and another method that returns its name (used in the “Display” command). This will prevent the main program from having to know the details of the data inside the aircraft class.
There will be NO keyboard or display accessing in the aircraft class nor in the linked list class
Outline of classes
Main
Create linked list object
Loop while cmd not “quit”
Enter
Get AC info from user
Add an AC to list
Leave
Get AC name from user
Delete AC from list
Display
Get AC count from list
Loop from 1 to count
Show each AC
Display all existing AC in order of altitude
Show
Get AC name from user
Search list for name
Show AC found
When quitting, tell list to delete all remaining AC (if any).
Linked List
Get AC count
Add an AC
Delete an AC
Show AC by number
Search for AC by name and return the number
Delete all AC
Aircraft
Get name
Set name
Set type
Set altitude
Set speed
Testing:
In order to save a lot of typing, you may use single character commands (Ex: ‘e’ for “entering”, ‘l’ for “leaving” etc.).
You should be able to
“entering” the first aircraft
“entering” the second aircraft
“entering” the third aircraft
“display” all three, in altitude-order
“show” the first
“show” the third
“leaving” the second
“show” the second (present an appropriate error message)
“display” both remaining
“leaving” the third
“display” the remaining one
“leaving’ the first
“display” the empty list
Explanation / Answer
Answer:
Program code to copy:
//AirCraftNode.java
public class AirCraftNode
{
// call variables
private String name;
private int ID;
private int altitude;
private int speed;
protected AirCraftNode next, prev;
// constructor -Default
public AirCraftNode()
{
this.name = "";
this.ID = 0;
this.altitude = 0;
this.speed = 0;
this.next = null;
this.prev = null;
}
// Parameterized constructor
public AirCraftNode(String name, int ID, int altitude, int speed)
{
this.name = name;
this.ID = ID;
this.altitude = altitude;
this.speed = speed;
this.next = null;
this.prev = null;
}
// Getter and setter methods to access the class variables
public String getName()
{
return this.name;
}
public void setName(String name)
{
this.name = name;
}
public int getID()
{
return this.ID;
}
public void setID(int ID)
{
this.ID = ID;
}
public int getAltitude()
{
return this.altitude;
}
public void setAltitude(int altitude)
{
this.altitude = altitude;
}
public int getSpeed()
{
return this.speed;
}
public void setSpeed(int speed)
{
this.speed = speed;
}
public void setNext(AirCraftNode next)
{
this.next = next;
}
public void setPrev(AirCraftNode prev)
{
this.prev = prev;
}
// toString() to returns the string in an appropriate format
public String toString()
{
String aircraft = " Aircraft name: " + getName()
+ " Aircraft type: " + getID() + " Aircraft Speed: "
+ getSpeed() + " Aircraft Altitude: " + getAltitude()
+ " ";
return aircraft;
}
}
// AirCraftDoublyLinkedList.java
// DoublyLinkedList class which is applied on the AirCraft
public class AirCraftDoublyLinkedList
{
// define the AirCraftNode with a head node
protected AirCraftNode head = null;
/* Default constructor and putting Aircraft value into HashMap */
public AirCraftDoublyLinkedList()
{
}
// addAirCraft() method will add a node to the linked list
public void addAirCraft(String name, int ID, int altitude, int speed)
{
// create a new node of AirCraftNode by using the
// parameterized constructor
AirCraftNode newNode = new AirCraftNode(name, ID, altitude, speed);
// if head is null(no values in the list)
if (head == null)
{
// assign the newNode to the head(list)
head = newNode;
}
// otherwise add the nodes the front
else
{
newNode.next = head;
head.prev = newNode;
head = newNode;
}
}
// searchAirCraft() searches the aircraft name in the list and
// returns the ID of the aircraft
public int searchAirCraft(String name)
{
// set the temp with head
AirCraftNode temp = head;
// loop until the temp is null
while (temp != null)
{
// check for the matching of the name with the list names
if (temp.getName().equalsIgnoreCase(name))
{
// if found return id
return temp.getID();
}
// move the next node
temp = temp.next;
}
// if not found return -1
return -1;
}
// showAircraft() method will search for the id and prints the
// respective details of the aircraft
public void showAircraft(int number)
{
AirCraftNode temp = head;
while (temp != null)
{
if (temp.getID() == number)
{
System.out.println(temp);
break;
}
temp = temp.next;
}
}
// deleteAircraft() accepts the name and searches for the name
// and if found deletes the node from the list
public boolean deleteAircraft(String name)
{
// set the temp to head
AirCraftNode temp = head;
// set the previous node to head
AirCraftNode prevNode = head;
// check for the match with the head node
if (head.getName().equalsIgnoreCase(name))
{
head = null;
return true;
}
// if not loop through each node
while (temp.next != null)
{
// search for the name in the list
// if found, set the links to the
if (temp.getName().equalsIgnoreCase(name))
{
prevNode.next = temp.next;
temp.next.prev = prevNode;
return true;
}
// move to the next node
prevNode = temp;
temp = temp.next;
}
// if the temp is the last node then try to match with the
// name
if (temp.next == null)
{
if (temp.getName().equalsIgnoreCase(name))
{
prevNode.next = null;
return true;
}
}
// if not found return false
return false;
}
// getAircraftCount() gets the count of nodes in the
// linkedlist
public int getAircraftCount()
{
int count = 0;
AirCraftNode temp = head;
while (temp != null)
{
count += 1;
temp = temp.next;
}
return count;
}
// deletes all the nodes of the list
public void deleteAllAircraft()
{
head = null;
}
// returns the array form of the linked list
public AirCraftNode[] getArrayForm()
{
// define the array of AirCraftNode
AirCraftNode acn[] = new AirCraftNode[getAircraftCount()];
// set the head to temp
AirCraftNode temp = head;
// define an index variable
int i = 0;
// loop through each node
while (temp != null)
{
// assign the node into the array at index i
acn[i] = temp;
i++;
// move to the next node
temp = temp.next;
}
// return the array form
return acn;
}
}
// AirCraftDLDriver.java
import java.util.Scanner;
// class to test the doublylinked list class
public class AirCraftDLDriver
{
// main method
public static void main(String[] args)
{
// define the Scanner object
Scanner scan = new Scanner(System.in);
// define the AirCraftDoublyLinkedList object
AirCraftDoublyLinkedList aircraftList = new AirCraftDoublyLinkedList();
// define and declare the required variables
char ch = ' ';
int ID = 0, speed = 0, altitude = 0;
String name;
int choice;
int count = 0;
// print the heading
System.out.println("#################### Air Traffic Control System ####################### ");
do
{
// display the menu
System.out.println(" Air Traffic Control System ");
System.out.println("1. Entering");
System.out.println("2. Leaving");
System.out.println("3. Show");
System.out.println("4. Display");
// prompt and read the choice
System.out.print("Enter your choice: ");
choice = scan.nextInt();
// use switch to switch to the particular case
switch (choice)
{
// to read input from the user and then set the values
// to the list
case 1:
System.out.println("Please enter the Aircraft details as prompted.");
System.out.print("Enter ID of the Aircraft: ");
ID = scan.nextInt();
System.out.print("Enter Altitude of Aircraft, in thousands of feet: ");
altitude = scan.nextInt();
System.out.print("Enter Speed of Aircraft: ");
speed = scan.nextInt();
System.out.print("Enter Name of Aircraft: ");
scan.nextLine();
name = scan.nextLine();
// add to the list
aircraftList.addAirCraft(name, ID, altitude, speed);
break;
// to deal with deleting the value
case 2:
// prompt and read the name of the aircraft
System.out.println("Please Enter Name of Aircraft");
scan.nextLine();
name = scan.nextLine();
// search for the name and delete the value
// if the method deleteAircraft returns true means
// node is deleted
if (aircraftList.deleteAircraft(name))
{
System.out.println("Deleted the Aircraft successfully!");
}
// otherwise display the un-success message
else
{
System.out.println("Could not delete/find the Aircraft!");
}
break;
// to deal with printing the respective aircraft details
case 3:
// prompt and read the name of the aircraft
System.out.println("Please Enter Name of Aircraft");
scan.nextLine();
name = scan.nextLine();
// get the id by searching for the name
ID = aircraftList.searchAirCraft(name);
// display the respective aircraft
aircraftList.showAircraft(ID);
break;
// to deal with printing all the aircrafts with respect
// to altitudes
case 4:
// get count
count = aircraftList.getAircraftCount();
// get the array form
AirCraftNode nodes[] = aircraftList.getArrayForm();
// send the array to sort display the values in decreasing
// order of the altitues
sortByAltitude(nodes, count);
for (int i = 0; i < count; i++)
{
System.out.println(nodes[i]);
}
break;
// default option selected
default:
System.out.println("Wrong Entry ");
break;
}
// prompt the user whether the user wants to continue or not
System.out.print(" Do you want to continue (Type y or n): ");
ch = scan.next().charAt(0);
System.out.println();
} while (ch == 'Y' || ch == 'y');
}
// static method to sort the values
public static void sortByAltitude(AirCraftNode aircrafts[], int size)
{
AirCraftNode temp = null;
System.out.println(size);
System.out.println(aircrafts[0].getName());
System.out.println(aircrafts[size - 1].getName());
for (int i = 0; i < size - 1; i++)
{
for (int j = 0; j < size - 1; j++)
{
if (aircrafts[j].getAltitude() < aircrafts[j + 1]
.getAltitude())
{
temp = aircrafts[j];
aircrafts[j] = aircrafts[j + 1];
aircrafts[j + 1] = temp;
}
}
}
}
}
Sample Output:
#################### Air Traffic Control System #######################
Air Traffic Control System
1. Entering
2. Leaving
3. Show
4. Display
Enter your choice: 1
Please enter the Aircraft details as prompted.
Enter ID of the Aircraft: 1254
Enter Altitude of Aircraft, in thousands of feet: 1300
Enter Speed of Aircraft: 120
Enter Name of Aircraft: Bleriot Bleriot
Do you want to continue (Type y or n): y
Air Traffic Control System
1. Entering
2. Leaving
3. Show
4. Display
Enter your choice: 1
Please enter the Aircraft details as prompted.
Enter ID of the Aircraft: 1350
Enter Altitude of Aircraft, in thousands of feet: 140
Enter Speed of Aircraft: 120
Enter Name of Aircraft: Curtiss Wasp
Do you want to continue (Type y or n): y
Air Traffic Control System
1. Entering
2. Leaving
3. Show
4. Display
Enter your choice: 1
Please enter the Aircraft details as prompted.
Enter ID of the Aircraft: 1200
Enter Altitude of Aircraft, in thousands of feet: 1200
Enter Speed of Aircraft: 130
Enter Name of Aircraft: Curtiss R
Do you want to continue (Type y or n): y
Air Traffic Control System
1. Entering
2. Leaving
3. Show
4. Display
Enter your choice: 1
Please enter the Aircraft details as prompted.
Enter ID of the Aircraft: 3600
Enter Altitude of Aircraft, in thousands of feet: 3600
Enter Speed of Aircraft: 120
Enter Name of Aircraft: Fairey Delta 2
Do you want to continue (Type y or n): y
Air Traffic Control System
1. Entering
2. Leaving
3. Show
4. Display
Enter your choice: 4
4
Fairey Delta 2
Bleriot Bleriot
Aircraft name: Fairey Delta 2
Aircraft type: 3600
Aircraft Speed: 120
Aircraft Altitude: 3600
Aircraft name: Bleriot Bleriot
Aircraft type: 1254
Aircraft Speed: 120
Aircraft Altitude: 1300
Aircraft name: Curtiss R
Aircraft type: 1200
Aircraft Speed: 130
Aircraft Altitude: 1200
Aircraft name: Curtiss Wasp
Aircraft type: 1350
Aircraft Speed: 120
Aircraft Altitude: 140
Do you want to continue (Type y or n): y
Air Traffic Control System
1. Entering
2. Leaving
3. Show
4. Display
Enter your choice: 3
Please Enter Name of Aircraft
Curtiss R
Aircraft name: Curtiss R
Aircraft type: 1200
Aircraft Speed: 130
Aircraft Altitude: 1200
Do you want to continue (Type y or n): y
Air Traffic Control System
1. Entering
2. Leaving
3. Show
4. Display
Enter your choice: 2
Please Enter Name of Aircraft
Curtiss R
Deleted the Aircraft successfully!
Do you want to continue (Type y or n): y
Air Traffic Control System
1. Entering
2. Leaving
3. Show
4. Display
Enter your choice: 4
3
Fairey Delta 2
Bleriot Bleriot
Aircraft name: Fairey Delta 2
Aircraft type: 3600
Aircraft Speed: 120
Aircraft Altitude: 3600
Aircraft name: Bleriot Bleriot
Aircraft type: 1254
Aircraft Speed: 120
Aircraft Altitude: 1300
Aircraft name: Curtiss Wasp
Aircraft type: 1350
Aircraft Speed: 120
Aircraft Altitude: 140
Do you want to continue (Type y or n): n
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.