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

Help with this code please? I have the code on Eclipse as a chunk of code as is,

ID: 3918404 • Letter: H

Question

Help with this code please? I have the code on Eclipse as a chunk of code as is, no seperation or anything, and wanted to see how to make this code work. Any advice would be great! Thanks in advance. I also have the text file saved if needed. There may be something about a space? line String[] splitString = airportDetail.split(" "); It must have 2 spaces in split function... may be check answer interface is removing extra space

Aiports.txt:

Dijkstra.java

import java.io.File;

import java.io.FileNotFoundException;

import java.util.HashMap;

import java.util.Iterator;

import java.util.LinkedList;

import java.util.NoSuchElementException;

import java.util.PriorityQueue;

import java.util.Scanner;

import java.util.Set;

public class Dijkstra

{

static HashMap mapvertex = new HashMap();

StringBuilder route = new StringBuilder();

public void dijkstra(Vertex departure, String arrival) {

Set airportNames = mapvertex.keySet();

Iterator airportIterator = airportNames.iterator();

while (airportIterator.hasNext()) {

Vertex vertex = mapvertex.get(airportIterator.next());

vertex.setKnown(false);

vertex.setCost(0);

vertex.setIsInfinity(true);

vertex.setPrevVertex(null);

}

departure.setIsInfinity(false);

Vertex vertex = departure;

vertex.setIsInfinity(false);

boolean arrivalFound = false;

PriorityQueue travvertx = new PriorityQueue<>();

for (;;) {

try {

vertex = travvertx.remove();

} catch (NoSuchElementException nsee) {

if (vertex == departure) {

} else {

break;

}

}

if (vertex == null) {

break;

}

LinkedList adjacentVertices = vertex.getlstadjvertx();

Iterator iterator = adjacentVertices.iterator();

while (iterator.hasNext()) {

AdjacentVertex adjVertex = iterator.next();

int cost = vertex.getCost() + adjVertex.getCost();

if (adjVertex.getVertex().getKnown() == false) {

if (adjVertex.getVertex().getIsInfinity()) {

adjVertex.getVertex().setCost(cost);

adjVertex.getVertex().setPrevVertex(vertex);

adjVertex.getVertex().setIsInfinity(false);

} else {

if (adjVertex.getVertex().getCost() > cost) {

adjVertex.getVertex().setCost(cost);

adjVertex.getVertex().setPrevVertex(vertex);

}

}

if (!travvertx.contains(adjVertex.getVertex())) {

travvertx.add(adjVertex.getVertex());

}

}

}

vertex.setKnown(true);

}

}

public int printPath(Vertex departure, Vertex arrival) {

if (arrival == departure) {

route.append(arrival.getName() + " -> ");

return -1;

} else {

int numberOfConnections = 1 + printPath(departure, arrival.getPrevVertex());

route.append(arrival.getName() + " -> ");

return numberOfConnections;

}

}

public static void main(String[] args) throws FileNotFoundException {

File airports = new File("Airports.txt");

Scanner sc1 = new Scanner(airports);

while (sc1.hasNext()) {

String airportDetail = sc1.nextLine();

String[] splitString = airportDetail.split(" ");

Vertex airport = new Vertex(splitString[0]);

mapvertex.put(splitString[0], airport);

}

sc1 = new Scanner(airports);

while (sc1.hasNext()) {

String airportDetail = sc1.nextLine();

String[] splitString = airportDetail.split(" ");

Vertex airport = mapvertex.get(splitString[0]);

LinkedList listadjvertx = new LinkedList<>();

for (int j = 1; j < splitString.length; j++) {

String[] subStrings = splitString[j].split(" ");

int cost = Integer.parseInt(subStrings[1]);

listadjvertx.add(new AdjacentVertex(mapvertex.get(subStrings[0]), cost));

}

airport.setadj(listadjvertx);

}

while (true) {

Dijkstra dij = new Dijkstra();

Scanner sc = new Scanner(System.in);

System.out.print("Enter Departure Airport: ");

String departure = sc.next().toUpperCase();

System.out.print("Enter Arrival Airport: ");

String arrival = sc.next().toUpperCase();

Vertex depvertx = mapvertex.get(departure);

Vertex arrvertx = mapvertex.get(arrival);

System.out.println("");

dij.dijkstra(depvertx, arrival);

System.out.println("By Price:");

System.out.println("");

int numberOfConnections = dij.printPath(depvertx, arrvertx);

System.out.println("Price : " + arrvertx.getCost());

System.out.println("Connection(s): " + numberOfConnections);

dij.route.toString();

System.out.println("Route : " + dij.route.substring(0, dij.route.length() - 4));

System.out.println("");

System.out.println("Check Another Route? (Y/N)");

if (sc.next().toUpperCase().equals("N")) {

break;

}

}

}

}

Vertex.java

import java.util.LinkedList;

public class Vertex implements Comparable {

private String name;

private LinkedList listadjvertxs;

private int cost;

private boolean known;

private boolean isInfinity;

private Vertex prevVertex;

public Vertex(String name) {

this.name = name;

this.cost = 0;

this.known = false;

this.isInfinity = true;

this.prevVertex = null;

}

public LinkedList getlstadjvertx() {

return this.listadjvertxs;

}

public void setadj(LinkedList listofadjvertx) {

this.listadjvertxs = listofadjvertx;

}

public String getName() {

return this.name;

}

public int getCost() {

return this.cost;

}

public void setCost(int cost) {

this.cost = cost;

}

public boolean getKnown() {

return this.known;

}

public void setKnown(boolean known) {

this.known = known;

}

public boolean getIsInfinity() {

return this.isInfinity;

}

public void setIsInfinity(boolean isInfinity) {

this.isInfinity = isInfinity;

}

public Vertex getPrevVertex() {

return this.prevVertex;

}

public void setPrevVertex(Vertex prevVertex) {

this.prevVertex = prevVertex;

}

public int compareTo(Vertex vertex) {

return this.cost - vertex.getCost();

}

public String toString() {

return this.name;

}

}

AdjacentVertex.java

public class AdjacentVertex {

private Vertex vertex;

private int cost;

public AdjacentVertex(Vertex vertex, int cost) {

this.vertex = vertex;

this.cost = cost;

}

public Vertex getVertex() {

return this.vertex;

}

public void setVertex(Vertex vertex) {

this.vertex = vertex;

}

public int getCost() {

return this.cost;

}

public void setCost(int cost) {

this.cost = cost;

}

}

19 RubliS.sLAss Dijkstra. LibrarwJava/JavaVirtual Machinesdk-10.0.1.jdk/Content ?D Dijkstra (1) [Java Application] Dijkstra at localhost:50091 21 Thread [mainl (Suspended (exception Error) Dijkstra.rma.n(String[]) line: 149 al Machines dk-10.0.1Jdk/Content ?? Dijkstra 11] [Java Application] ? a Dijkstra at localhost: 50093 27e Rublic.void.dijkstra(Verkex. deperture.String arrival).I 28 29 SatsString..aicaortNanes....apvertexaySctC Thread [main(Suspended (exception Error)) LibrarwJava/JavaVirtual Machinesdk-10.0.1.jdk/Content Dijkstra at localhost:50097 Dijkstra.main(StringI) line: 149 Dijkstra (1) [Java Application) Thread [mainl (Suspended (exception Error) Dijkstra.main(String) line: 149 alMachines dk-10.0.1Jdk/Content Dijkstra (1) [Java Application) Dijkstra at localhost:50519 Thread [main] (Suspended 4exception Error)) LibraryJaa/JavaVirtual Machinesdk-10.0.1.jdk/Content Dijkstra at localhost:50521 ? 41 vertex setisInfinity true): Dijkstra.main(String[]) line: 149 43 vertePrVactexn ?D Dijkstra (1) [Java Application] 45 ? f. Thread [main] (Suspended exception Error)) Dijkstra.main String)) line: 149 47 departure.setisInfinity false 49 Yectex.verkex....departur 51 vectestsntinityfalse:. al Machines/dk-10.0.1 '?dk/Content ? Dijkstra 11] [Java Application] Dijkstra at local nost: 50523 ? uf> Thread (main] (Suspended 4exception Error)) ? Dijkstra.main(Stringt) ine: 149

Explanation / Answer

Please find the code below without errors:

Dijkstra.java

*******************

import java.io.File;

import java.io.FileNotFoundException;

import java.util.HashMap;

import java.util.Iterator;

import java.util.LinkedList;

import java.util.NoSuchElementException;

import java.util.PriorityQueue;

import java.util.Scanner;

import java.util.Set;

public class Dijkstra

{

static HashMap<String, Vertex> mapvertex = new HashMap<>();

StringBuilder route = new StringBuilder();

public void dijkstra(Vertex departure, String arrival) {

Set<String> airportNames = mapvertex.keySet();

Iterator<String> airportIterator = airportNames.iterator();

while (airportIterator.hasNext()) {

Vertex vertex = mapvertex.get(airportIterator.next());

vertex.setKnown(false);

vertex.setCost(0);

vertex.setIsInfinity(true);

vertex.setPrevVertex(null);

}

departure.setIsInfinity(false);

Vertex vertex = departure;

vertex.setIsInfinity(false);

boolean arrivalFound = false;

PriorityQueue<Vertex> travvertx = new PriorityQueue<>();

for (;;) {

try {

vertex = travvertx.remove();

} catch (NoSuchElementException nsee) {

if (vertex == departure) {

} else {

break;

}

}

if (vertex == null) {

break;

}

LinkedList<Vertex> adjacentVertices = vertex.getlstadjvertx();

Iterator iterator = adjacentVertices.iterator();

while (iterator.hasNext()) {

AdjacentVertex adjVertex = (AdjacentVertex) iterator.next();

int cost = vertex.getCost() + adjVertex.getCost();

if (adjVertex.getVertex().getKnown() == false) {

if (adjVertex.getVertex().getIsInfinity()) {

adjVertex.getVertex().setCost(cost);

adjVertex.getVertex().setPrevVertex(vertex);

adjVertex.getVertex().setIsInfinity(false);

} else {

if (adjVertex.getVertex().getCost() > cost) {

adjVertex.getVertex().setCost(cost);

adjVertex.getVertex().setPrevVertex(vertex);

}

}

if (!travvertx.contains(adjVertex.getVertex())) {

travvertx.add(adjVertex.getVertex());

}

}

}

vertex.setKnown(true);

}

}

public int printPath(Vertex departure, Vertex arrival) {

if (arrival == departure) {

route.append(arrival.getName() + " -> ");

return -1;

} else {

int numberOfConnections = 1 + printPath(departure, arrival.getPrevVertex());

route.append(arrival.getName() + " -> ");

return numberOfConnections;

}

}

public static void main(String[] args) throws FileNotFoundException {

File airports = new File("input2.txt");

Scanner sc1 = new Scanner(airports);

while (sc1.hasNext()) {

String airportDetail = sc1.nextLine();

String[] splitString = airportDetail.split(" ");

Vertex airport = new Vertex(splitString[0]);

mapvertex.put(splitString[0], airport);

}

sc1 = new Scanner(airports);

while (sc1.hasNext()) {

String airportDetail = sc1.nextLine();

String[] splitString = airportDetail.split(" ");

Vertex airport = (Vertex) mapvertex.get(splitString[0]);

LinkedList listadjvertx = new LinkedList<>();

for (int j = 1; j < splitString.length; j++) {

String[] subStrings = splitString[j].split(" ");

int cost = Integer.parseInt(subStrings[1]);

listadjvertx.add(new AdjacentVertex((Vertex) mapvertex.get(subStrings[0]), cost));

}

airport.setadj(listadjvertx);

}

while (true) {

Dijkstra dij = new Dijkstra();

Scanner sc = new Scanner(System.in);

System.out.print("Enter Departure Airport: ");

String departure = sc.next().toUpperCase();

System.out.print("Enter Arrival Airport: ");

String arrival = sc.next().toUpperCase();

Vertex depvertx = (Vertex) mapvertex.get(departure);

Vertex arrvertx = (Vertex) mapvertex.get(arrival);

System.out.println("");

dij.dijkstra(depvertx, arrival);

System.out.println("By Price:");

System.out.println("");

int numberOfConnections = dij.printPath(depvertx, arrvertx);

System.out.println("Price : " + arrvertx.getCost());

System.out.println("Connection(s): " + numberOfConnections);

dij.route.toString();

System.out.println("Route : " + dij.route.substring(0, dij.route.length() - 4));

System.out.println("");

System.out.println("Check Another Route? (Y/N)");

if (sc.next().toUpperCase().equals("N")) {

break;

}

}

}

}

Vertex.java

********************

import java.util.LinkedList;

public class Vertex implements Comparable {

private String name;

   private LinkedList listadjvertxs;

   private int cost;

   private boolean known;

   private boolean isInfinity;

   private Vertex prevVertex;

   public Vertex(String name) {

       this.name = name;

       this.cost = 0;

       this.known = false;

       this.isInfinity = true;

       this.prevVertex = null;

   }

   public LinkedList getlstadjvertx() {

       return this.listadjvertxs;

   }

   public void setadj(LinkedList listofadjvertx) {

       this.listadjvertxs = listofadjvertx;

   }

   public String getName() {

       return this.name;

   }

   public int getCost() {

       return this.cost;

   }

   public void setCost(int cost) {

       this.cost = cost;

   }

   public boolean getKnown() {

       return this.known;

   }

   public void setKnown(boolean known) {

       this.known = known;

   }

   public boolean getIsInfinity() {

       return this.isInfinity;

   }

   public void setIsInfinity(boolean isInfinity) {

       this.isInfinity = isInfinity;

   }

   public Vertex getPrevVertex() {

       return this.prevVertex;

   }

   public void setPrevVertex(Vertex prevVertex) {

       this.prevVertex = prevVertex;

   }

   public int compareTo(Vertex vertex) {

       return this.cost - vertex.getCost();

   }

   public String toString() {

       return this.name;

   }

   @Override

   public int compareTo(Object o) {

       // TODO Auto-generated method stub

       return 0;

   }

}

AdjacentVertex.java

****************************

public class AdjacentVertex {

private Vertex vertex;

private int cost;

public AdjacentVertex(Vertex vertex, int cost) {

this.vertex = vertex;

this.cost = cost;

}

public Vertex getVertex() {

return this.vertex;

}

public void setVertex(Vertex vertex) {

this.vertex = vertex;

}

public int getCost() {

return this.cost;

}

public void setCost(int cost) {

this.cost = cost;

}

}

NOTE: I have removed all the errors from your code, but I cannot guarantee that your code will work as expected until you provide me the airport.txt file. Once you provide me the text file, I can try to check if your code works as expected.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote