Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help with this homework please use class (Public and Private) Singly Link

ID: 3727681 • Letter: I

Question

I need help with this homework please use class (Public and Private)

Singly Linked Lists

1. Standard 3 character airport code 2. Distance from New York City's LaGuardia Airport to the specified airport, in miles The head of the list should be represented with an element whose value is 'LGA' and whose distance is 0. Airport codes will be provided in order of closest to furthest away. Create a singly linked list to house each of the airports as a node in the same order in which it was provided (nearest to furthest) Each node should contain both the airport code and the entered distance.

Explanation / Answer

1)AirportNode.java Class

/**

* Class Node will represent single entry in linked list.

* */

public class AirportNode {

private String airportCode;

private int distance;

private AirportNode nextAirport;

/**

* default constructor

*/

public AirportNode() {

}

/**

* Constructor to set Airport code and distance

*

* @param airportCode

* @param distance

*/

public AirportNode(String airportCode, int distance) {

this.airportCode = airportCode;

this.distance = distance;

}

/**

*

* @return airport code

*/

public String getAirportCode() {

return airportCode;

}

/**

* set airport code

*

* @param airportCode

*/

public void setAirportCode(String airportCode) {

this.airportCode = airportCode;

}

/**

*

* @return airport distance

*/

public int getDistance() {

return distance;

}

/**

* set airport distance

*

* @param distance

*/

public void setDistance(int distance) {

this.distance = distance;

}

/**

*

* @return next airport

*/

public AirportNode getNextAirport() {

return nextAirport;

}

/**

* set next airport

*

* @param nextAirport

*/

public void setNextAirport(AirportNode nextAirport) {

this.nextAirport = nextAirport;

}

}

---------------------------------------------------------------------------------------------------------------------

2)SingalyLinkedList.java

public class SingalyLinkedList {

protected AirportNode head;

protected AirportNode tail;

/* Constructor */

public SingalyLinkedList() {

head = null;

tail = null;

}

/**

* Function to add new airport

**/

public void add(String airportCode, int distance) {

AirportNode airport = new AirportNode(airportCode, distance);

if (head == null) {

head = airport;

tail = head;

} else {

tail.setNextAirport(airport);

tail = airport;

}

}

/**

* Search for a airport details

*

* @param airportCode

*/

public void searchAirportDetail(String airportCode) {

boolean airportFound = false;

if (head != null) {

AirportNode airportNode = head;

while (airportNode != null) {

if (airportNode.getAirportCode().equals(airportCode)) {

airportFound = true;

}

if (airportFound) {

System.out.println("Airport code : "

+ airportNode.getAirportCode() + ", Distance : "

+ airportNode.getDistance());

}

airportNode = airportNode.getNextAirport();

}

}

if (!airportFound) {

System.out.print("No Airport detail found in list");

}

}

}

-----------------------------------------------------------------------------------------------------------------

3) Test.java

public class Test {

public static void main(String str[]) {

SingalyLinkedList airportList = new SingalyLinkedList();

System.out.println("Adding Airport : LGA, Distance 0");

airportList.add("LGA", 0);

System.out.println("Adding Airport : PNQ, Distance 500");

airportList.add("PNQ", 500);

System.out.println("Adding Airport : BEL, Distance 1000");

airportList.add("BEL", 1000);

System.out.println("Adding Airport : DEL, Distance 15000");

airportList.add("BEL", 1500);

System.out.println(">>Search for : PNQ");

airportList.searchAirportDetail("PNQ");

}

}

-------------------------------------------------

NOTE: Put these three class in a single package and run Test.class.

Sample output:-

Adding Airport : LGA, Distance 0
Adding Airport : PNQ, Distance 500
Adding Airport : BEL, Distance 1000
Adding Airport : DEL, Distance 15000
>>Search for : PNQ
Airport code : PNQ, Distance : 500
Airport code : BEL, Distance : 1000
Airport code : BEL, Distance : 1500