Update: I just placed the airport.txt into the same folder as flightfares but st
ID: 3919461 • Letter: U
Question
Update: I just placed the airport.txt into the same folder as flightfares but still getting:
Error: Could not find or load main class Dijkstra
Caused by: java.lang.ClassNotFoundException: Dijkstra
Help most appreciated!
urgent Java programming help please! Please let me know how I should try the code out in Eclipse such as paste everything as is into one section or what I should do (try putting into the same folder as the project and so on). Thank you in advance!
airport.txt
Here is a chunk that might help! I am not sure how to do package dijkstra;
package dijkstra;
import java.io.File;
import java.util.ArrayList;
import java.util.Scanner;
public class FlightFares {
int nodes; //no of items
private double fare_matrix[][]; //weighted matrix
private String codes[]; //store code names for airports
private double distance[]; //stores all fares from source to all remaining airports
public static void main(String args[]) {
FlightFares ob = new FlightFares(); //creaing object
String fileName = "airports.txt"; //filename
//reading file and generating matrix form of fares
try {
ob.readFile(fileName); //readfile function reads file and generates matrix
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/*for (String str : ob.codes) {
System.out.println(str + " ");
}
for (double str[] : ob.fare_matrix) {
for (double fare : str) {
System.out.print(fare + " ");
}
System.out.println();
}
*/
//calling calc() method with source name DFW
String source = "DFW";
ob.calc(ob.nodes, source);
System.out.println("The Shortest Path from Source " + source + " to all other vertices are : ");
for (int i = 0; i < ob.nodes; i++){
if (!ob.codes[i].equals(source))
System.out.println(
"source :" + source + " destination :" + ob.codes[i] + " MinCost is :" + ob.distance[i] + " ");
}
}
//function to calculate minimum fares
public void calc(int n, String str) {
int flag[] = new int[n];
int i, minpos = 0, k, c;
double minimum;
int s = getIndex(str);
for (i = 0; i < n; i++) {
flag[i] = 0;
this.distance[i] = this.fare_matrix[s][i];
}
c = 0;
while (c < n) {
minimum = 99;
for (k = 0; k < n; k++) {
if (this.distance[k] < minimum && flag[k] != 1) {
minimum = this.distance[k];
minpos = k;
}
}
flag[minpos] = 1;
c++;
for (k = 0; k < n; k++) {
if (this.distance[minpos] + this.fare_matrix[minpos][k] < this.distance[k] && flag[k] != 1)
this.distance[k] = this.distance[minpos] + this.fare_matrix[minpos][k];
}
}
}
//function to read data from file and generate weighted matrix
public void readFile(String FileName) throws Exception {
File F;
F = new File(FileName);
ArrayList list = new ArrayList<>();
Scanner S = new Scanner(F);
System.out.println(" loading... [" + FileName + "] and reading data... ");
while (S.hasNextLine()) {
String[] splited = S.nextLine().trim().split("\s+");
list.add(splited[0].toUpperCase());
}
S.close();
//initiating class properties
nodes = list.size();
fare_matrix = new double[nodes][nodes];
distance = new double[nodes];
codes = list.toArray(new String[list.size()]);
S = new Scanner(F);
while (S.hasNextLine()) {
String[] splited = S.nextLine().trim().split("\s+");
int row = getIndex(splited[0]), column = -1;
for (int i = 1; i < splited.length; i++) {
if (getIndex(splited[i]) != -1) {
column = getIndex(splited[i]);
} else {
fare_matrix[row][column] = Double.parseDouble(splited[i]);
}
}
}
for (int i = 0; i < nodes; i++)
for (int j = 0; j < nodes; j++) {
if (fare_matrix[i][j] == 0)
fare_matrix[i][j] = 999;
}
}
//method to get index value of this string from codes matrix
public int getIndex(String str) {
for (int i = 0; i < codes.length; i++) {
if (str.toUpperCase().equals(codes[i]))
return i;
}
return -1;
}
}
Project5 See folders you viewed previously Downloads Desktop Documents iCloud Drive AirDrop java CLASS TXT dijkstra.java FlightFares.class airports.txt Devices Remote Disc Eclipse Eclipse* Tags GreenExplanation / Answer
package dijkstra;
import java.io.File;
import java.util.ArrayList;
import java.util.Scanner;
public class FlightFares {
int nodes; //no of items
private double fare_matrix[][]; //weighted matrix
private String codes[]; //store code names for airports
private double distance[]; //stores all fares from source to all remaining airports
public static void main(String args[]) {
FlightFares ob = new FlightFares(); //creaing object
//As per the question the fileName must be dynamic for that you can pass the filename during runtime as runtime parameter & that will be args[0] if you don't
//need like that then just uncomment the below line & replace the args[0] with file attribute in try{} block it will work fine
//FileName must always contain complete filepath+fileName, here \ is used since "" is an escape character
// String fileName = "C:\vishnu\eclipse\Workspace\dijkstra\src\airport.txt"; //filename
//reading file and generating matrix form of fares
try {
ob.readFile(args[0]); //readfile function reads file and generates matrix
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//Note: Your program does not work per the question mentioned. Since you have problem with file loading I have answered for that part
//If your code should match the expected output use "while" loop to check whether customer wants to check other routes or not &
//get the source and destination from user and start to calculate the prices & piece of code is below
/*System.out.println("Do you like to find the route(Y/N): ");
Scanner S = new Scanner(System.in);
String depart;
String arrive;
String route=S.nextLine();
while(route.equalsIgnoreCase("Y"){
System.out.println("Enter Departure airport: ");
depart=S.nextLine();
System.out.println("Enter Arrival airport: ");
arrive=S.nextLine();
**implement your algorithm
System.out.println("Check another route(Y/N): ");
route=S.nextLine();
}
System.out.println("Your Route finding is Ended as per your choice");
*/
//calling calc() method with source name DFW
String source = "DFW";
ob.calc(ob.nodes, source);
System.out.println("The Shortest Path from Source " + source + " to all other vertices are : ");
for (int i = 0; i < ob.nodes; i++){
if (!ob.codes[i].equals(source))
System.out.println(
"source :" + source + " destination :" + ob.codes[i] + " MinCost is :" + ob.distance[i] + " ");
}
}
//function to calculate minimum fares
public void calc(int n, String str) {
int flag[] = new int[n];
int i, minpos = 0, k, c;
double minimum;
int s = getIndex(str);
for (i = 0; i < n; i++) {
flag[i] = 0;
this.distance[i] = this.fare_matrix[s][i];
}
c = 0;
while (c < n) {
minimum = 99;
for (k = 0; k < n; k++) {
if (this.distance[k] < minimum && flag[k] != 1) {
minimum = this.distance[k];
minpos = k;
}
}
flag[minpos] = 1;
c++;
for (k = 0; k < n; k++) {
if (this.distance[minpos] + this.fare_matrix[minpos][k] < this.distance[k] && flag[k] != 1)
this.distance[k] = this.distance[minpos] + this.fare_matrix[minpos][k];
}
}
}
//function to read data from file and generate weighted matrix
public void readFile(String FileName) throws Exception {
File F;
F = new File(FileName);
ArrayList list = new ArrayList<>();
Scanner S = new Scanner(F);
System.out.println(" loading... [" + FileName + "] and reading data... ");
while (S.hasNextLine()) {
String[] splited = S.nextLine().trim().split("\s+");
list.add(splited[0].toUpperCase());
}
S.close();
//initiating class properties
nodes = list.size();
fare_matrix = new double[nodes][nodes];
distance = new double[nodes];
codes = (String[]) list.toArray(new String[list.size()]);
S = new Scanner(F);
while (S.hasNextLine()) {
String[] splited = S.nextLine().trim().split("\s+");
int row = getIndex(splited[0]), column = -1;
for (int i = 1; i < splited.length; i++) {
if (getIndex(splited[i]) != -1) {
column = getIndex(splited[i]);
} else {
fare_matrix[row][column] = Double.parseDouble(splited[i]);
}
}
}
for (int i = 0; i < nodes; i++)
for (int j = 0; j < nodes; j++) {
if (fare_matrix[i][j] == 0)
fare_matrix[i][j] = 999;
}
}
//method to get index value of this string from codes matrix
public int getIndex(String str) {
for (int i = 0; i < codes.length; i++) {
if (str.toUpperCase().equals(codes[i]))
return i;
}
return -1;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.