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

In this assignment, you are going to write a Java program to read a graph data f

ID: 3887106 • Letter: I

Question

In this assignment, you are going to write a Java program to read a graph data file which will be used in subsequent programming assignments. The first row of the file indicates the number of nodes and the following rows denote the connections for each node. The following examples are two data files.

Write a Java program called “Color” • The program should read two parameters from the command line o The first parameter is a data filename o The second parameter is an integer value – k o Allocate a 2D array for the data o Write a method to print out the graph data o Write a note to describe how I can “javac” your program

In this assignment, you are going to write a Java program to read a graph data file which will be used in subsequent programming assignments. The first row of the file indicates the number of nodes and the following rows denote the connections for each node. The following examples are two data files 00 1001 00 10 11 110111 00 10 11 01 1 101 10 0100110000 10 1000 1000 010 1000100 0010 100010 1001000001 1000000110 0100000011 00 10010001 000 101 1000 0000 101100 YOUR TASKS Write a Java program called "Color" The program should read two parameters from the command line o o o o o The first parameter is a data filename The second parameter is an integer value - k Allocate a 2D array for the data Write a method to print out the graph data Write a note to describe how I can "javac" your program >>java Color data1.txt 4

Explanation / Answer

Hi,
You can use the below code for the above requirement, i have added comments to help you understand,

/* package whatever; // don't place package name! */

import java.util.*;

import java.lang.*;

import java.io.*;

class Ideone

{

public static void main (String[] args) throws java.lang.Exception

{

String filename=args[0]; // first command line arguement

int n=args[1]; //second arguement

int[][] m = new int[n][n];

try (BufferedReader br = new BufferedReader(new FileReader(filename))) {

String sCurrentLine;

while ((sCurrentLine = br.readLine()) != null) {

for(int i=0;i<n;i++)

{

for(int j=0;j<n;j++)

{

m[i][j]=Integer.parseInt(sCurrentLine);

}

}

}

for(int i=0;i<n;i++)

{

for(int j=0;j<n;j++)

{

System.out.println(m[i][j]);

}

}

} catch (IOException e) {

e.printStackTrace();

}

System.out.println(multi);

}

}
args[] in main specifies the command line arguements, so running javac with arguements you can pass them from command line
Thumsb up if this was helpful, otherwise let me know in comments.