Ineed help with java programing, not my strong suit!!! Please and thanks. The La
ID: 3808276 • Letter: I
Question
Ineed help with java programing, not my strong suit!!! Please and thanks. The Lab4 program reads from a file. NetworkA.txt is an example. The file contents are:
5 0 4 10 0 1 20 1 2 30 2 0 20 2 3 5 3 2 5 3 4 10 4 3 10
*Complete the methods containing a TODO comment in the Lab4 program. Test the program. You may find in helpful to implement and test one method at a time. import java.util.*; public class Lab4 { public static void main(String[] args) throws FileNotFoundException { // Select networkAdjMtx definition file JFileChooser fc = new JFileChooser(); fc.setDialogTitle("Select the Network Definition File:"); int ret = fc.showOpenDialog(null); File netDefFile = fc.getSelectedFile(); if(ret != JFileChooser.APPROVE_OPTION) { // Programs that use the javax.swing classes use multiple // threads. System.exit() will terminate the other threads. System.exit(0); } // Assigns the File object to the Scanner object. Notice System.in // in not used here. This is another Scanner constructor, It will // open the file for reading. Scanner netDefFileInp = new Scanner(netDefFile); // Load the network definition into an adjacency matrix. The adjacency // matrix is described in loadNetworkAdjacencyMatrix. int[][] network = loadNetworkAdjacencyMatrix(netDefFileInp); // Display the adjacency matrix displayNetworkAdjacencyMatrix(network); // Here's the Scanner object that we usually use. Scanner inp = new Scanner(System.in); // 1. Read a path using the loadPath method (you will implement this // method ). // 2. Determine if the path is valid using the isValidPatch method (you // will implement this method) // 3. If the path is valid then calculate the path cost using the // patCost method (you will implement this method) System.out.println("Enter the path as a sequence of node numbers"); System.out.println(" ie 0 4 2. Enter quit to end."); System.out.print("Enter a path: "); while (inp.hasNextLine()) { String line = inp.nextLine(); if("quit".equalsIgnoreCase(line)) { break; } int[] path = loadPath(new Scanner(line)); System.out.print(Arrays.toString(path)); if (isValidPath(network, path)) { System.out.printf(" is a valid path. The path cost is %d.%n", pathCost(network, path)); } else { System.out.println(" is not a valid path."); } System.out.print("Enter a path: "); } System.exit(0); }
Explanation / Answer
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
import javax.swing.JFileChooser;
/**
*
* @author Sam
*/
public class Lab4 {
public static void main(String[] args) throws FileNotFoundException {
// Select networkAdjMtx definition file
JFileChooser fc = new JFileChooser();
fc.setDialogTitle("Select the Network Definition File:");
int ret = fc.showOpenDialog(null);
File netDefFile = fc.getSelectedFile();
if(ret != JFileChooser.APPROVE_OPTION) {
// Programs that use the javax.swing classes use multiple
// threads. System.exit() will terminate the other threads.
System.exit(0);
}
// Assigns the File object to the Scanner object. Notice System.in
// in not used here. This is another Scanner constructor, It will
// open the file for reading.
Scanner netDefFileInp = new Scanner(netDefFile);
// Load the network definition into an adjacency matrix. The adjacency
// matrix is described in loadNetworkAdjacencyMatrix.
int[][] network = loadNetworkAdjacencyMatrix(netDefFileInp);
// Display the adjacency matrix
displayNetworkAdjacencyMatrix(network);
// Here's the Scanner object that we usually use.
Scanner inp = new Scanner(System.in);
// 1. Read a path using the loadPath method (you will implement this
// method ).
// 2. Determine if the path is valid using the isValidPatch method (you
// will implement this method)
// 3. If the path is valid then calculate the path cost using the
// patCost method (you will implement this method)
System.out.println("Enter the path as a sequence of node numbers");
System.out.println(" ie 0 4 2. Enter quit to end.");
System.out.print("Enter a path: ");
while (inp.hasNextLine()) {
String line = inp.nextLine();
if("quit".equalsIgnoreCase(line)) {
break;
}
int[] path = loadPath(new Scanner(line));
System.out.print(Arrays.toString(path));
if (isValidPath(network, path)) {
System.out.printf(" is a valid path. The path cost is %d.%n",
pathCost(network, path));
} else {
System.out.println(" is not a valid path.");
}
System.out.print("Enter a path: ");
}
System.exit(0);
}
private static int[][] loadNetworkAdjacencyMatrix(Scanner netDefFileInp) { //load the matrix
int size = netDefFileInp.nextInt(); //read size
int adjMat[][] = new int[size][size]; //create matrix
int i,j;
while (netDefFileInp.hasNextInt()){
i = netDefFileInp.nextInt(); //first node
j = netDefFileInp.nextInt(); //second node
adjMat[i][j] = netDefFileInp.nextInt(); // read weight
}
return adjMat;
}
private static void displayNetworkAdjacencyMatrix(int[][] network) { //normal printing of matrix
System.out.println("Adjacency Matrix: ");
for (int[] network1 : network) {
for (int n: network1)
System.out.print(n + " ");
System.out.println();
}
}
private static int[] loadPath(Scanner scanner) {
int size = scanner.nextInt(); //first number indicates the size of path
int path[] = new int[size];
int i = 0;
while (scanner.hasNextInt())
path[i++] = scanner.nextInt(); //scan path
return path;
}
private static boolean isValidPath(int[][] network, int[] path) {
if (path.length < 2) //cannot find path path contains less than two nodes
return false;
int i;
int j = path[0];
int k = 1;
while (k < path.length){
i = j;
j = path[k++];
if (network[i][j] == 0) //if path from i to j is zero, indicates no path is present
return false;
}
return true;
}
private static int pathCost(int[][] network, int[] path) {
int i,j=path[0],k=1;
int cost = 0;
while (k < path.length){
i = j;
j = path[k++];
cost += network[i][j]; // calculate cost
}
return cost;
}
}
I realized the path file example was missing. Still, I tried my best to implement that. In case if you have any doubt, please comment below.