Help, i\'m suppose to use my previous lab (lab4) into lab 5, but stuck on this a
ID: 3833150 • Letter: H
Question
Help, i'm suppose to use my previous lab (lab4) into lab 5, but stuck on this assignment here's the lab 4 code:
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) {
int n = netDefFileInp.nextInt();
int a_Mx[][] = new int[n][n];
int i,j;
while (netDefFileInp.hasNextInt()){
i = netDefFileInp.nextInt();
j = netDefFileInp.nextInt();
a_Mx[i][j] = netDefFileInp.nextInt(); }
return a_Mx;
}
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 n = scanner.nextInt();
int wy[] = new int[n];
int i = 0;
while (scanner.hasNextInt())
wy[i++] = scanner.nextInt();
return wy;
}
private static boolean isValidPath(int[][] network, int[] path) {
if (path.length < 2)
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)
return false;
}
return true;
}
private static int pathCost(int[][] network, int[] path) {
int i,j=path[0],k=1;
int wtc = 0;
while (k < path.length){
i = j;
j = path[k++];
wtc += network[i][j]; }
return wtc;
}
}
After Lab4 is completed, copy the Lab4.java file to Lab5.java. You may want to create a new project for Lab5.
Verify that Lab5.java compiles and produces the same output as Lab4.java.
Create a new public class named Lab5Utils.
Remove the following method definitions from the Lab5 class and put them in the Lab5Utils class. You may use cut and paste to move the method definitions or try the Refactor Move… command. Refactor is on the NetBeans menu bar. If the methods have private access then change the access to public. The methods to move are:
- displayNetworkAdjacencyMatrix
-isValidPath
-loadNetworkAdjacencyMatrix
-loadPath
-pathCost
Verify that Lab5.java compiles and produces the same output as Lab4.java
Explanation / Answer
Source Code:
Lab5.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
import javax.swing.JFileChooser;
public class Lab5 {
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 = Lab5Utils.loadNetworkAdjacencyMatrix(netDefFileInp);
// Display the adjacency matrix
Lab5Utils.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 (Lab5Utils.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[] loadPath(Scanner scanner) {
int n = scanner.nextInt();
int wy[] = new int[n];
int i = 0;
while (scanner.hasNextInt()) {
wy[i++] = scanner.nextInt();
}
return wy;
}
private static int pathCost(int[][] network, int[] path) {
int i, j = path[0], k = 1;
int wtc = 0;
while (k < path.length) {
i = j;
j = path[k++];
wtc += network[i][j];
}
return wtc;
}
}
Lab5Utils.java
public class Lab5Utils {
public 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();
}
}
public static boolean isValidPath(int[][] network, int[] path) {
if (path.length < 2) {
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) {
return false;
}
}
return true;
}
public static int[][] loadNetworkAdjacencyMatrix(Scanner netDefFileInp) {
int n = netDefFileInp.nextInt();
int a_Mx[][] = new int[n][n];
int i, j;
while (netDefFileInp.hasNextInt()) {
i = netDefFileInp.nextInt();
j = netDefFileInp.nextInt();
a_Mx[i][j] = netDefFileInp.nextInt();
}
return a_Mx;
}
public static int[] loadPath(Scanner scanner) {
int n = scanner.nextInt();
int wy[] = new int[n];
int i = 0;
while (scanner.hasNextInt()) {
wy[i++] = scanner.nextInt();
}
return wy;
}
public static int pathCost(int[][] network, int[] path) {
int i, j = path[0], k = 1;
int wtc = 0;
while (k < path.length) {
i = j;
j = path[k++];
wtc += network[i][j];
}
return wtc;
}
}
Steps:
So, here you did not want to do much modifications.
The first is, copy the whole code in a file named Lab5.java so I first saved it and the class name change to Lab5.
The next thing is, create Lab5Utils.java file.
Then simply cut the mentioned methods from class Lab5 and paste it in class Lab5Utils.
All these methods are 'private static'. Change them from 'private' to 'public' and keep 'static' as it is.
Then in the class Lab5, there are these methods called.
So previously they were called from main() because they were of the same class. Now, as they are from other class, and in the new class, all the methods are public, you can access them in Lab5.
All the methods are defined 'static' so they can be called using class name as Lab5.method_name.
So these are the steps and I have separated the code.
The code is compiled and executed successfully.
Do comment if there is any query. Thank you. :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.