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

In Java. You will write a program that will sort the columns in a 2D array. The

ID: 3666214 • Letter: I

Question

In Java.

You will write a program that will sort the columns in a 2D array. The number of rows, number of columns and the maximum possible value will be obtained via command line parameters (in the listed order). Remember, you will have to convert the rows, cols and maximum value from String to int. Implement the following methods: public void createData( String filename, int numRows, int numCols, int maxValue) This method should create a file with numRows rows of integer values. Each row will contain numCols values on it separated by a space (i.e. The input file will match the dimensions of the 2D array.). public void readData( String filename, int [][]matrix) Read the integer values from the file into the 2D array. Assumes that the array has already been instantiated. public void sortColumn( int [][] matrix, int col ) Sort the column col of the matrix using Selection Sort. public void sortMatrix ( int [][] matrix ) Sort each column in the 2D array by calling sortColumn for each column in the array. public void sortMatrix ( int [][] matrix ) Print the contents of the 2D array with each row on a separate line and each column separated by space(s). Create a main method that will use the above methods to generate the file of random values, read the data from the file into the 2D array, output the unsorted array to the console, sort the array columns and the output the sorted array to the console.

Explanation / Answer

import java.io.*;

import java.io.File;

public class ArrayImplement

{

public void createData(String filename, int numRows,int numCols, int maxValue)

{

File f = new File(filename);

Scanner s = new Scanner(System.in);

InputStream is;

is=System.in;

System.out.println("enter data ");

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

{

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

{

num=Integer.parseInt(s.readLine());

is.write(num);

}

}

} // end of the method

public void readData(String filename, int [][] matrix)

int x=0;

int y=0;

string line;

while((line =in.readLine())!=null)

{

string [] values= line.split(" ");

for(string str :values)

{

int str_int = Integer.parseInt(str);

matrix[x][y] = str_int;

}

y =y+1;

}

x=x+1;

}

} // end of the method

public void sortColumn(int [n][m]matrix, int col)

{

int rows=n;

int col=m;

for(int i=0;i<rows-1;i++)

{

int index=i;

for(int j=i+1; j<rows; j++)

{

if(matrix[j][col] <matrix[index])

index=j;

int temp= matrix[index][xol];

matrix[index][col]= matrix[i];

matrix[i]= temp;

}

} // end of the method

public void printMatrix(int [n][m] matrix)

{

int rows=n;int col=m;

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

{

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

{

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

System.out.println(" ");

}

System.out.print(" ");

}

} // end of the method

}

public void sortMatrix(int [n][m] matrix)

{

int rows=n;

int col=m;

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

{

sortColumn([n][i]matrix, i);

}

} // end of the method

public static void main(String [] args)

{

string filename= "exampleArray";

System.out.println("enter number of rows , columns and maximumValue respectively");

Scanner s= new Scanner(System.in);

int r1= s.readLine();

int c1= s.readLine();

int max= s.readLine();

int matrix[50][50];

createData(filename, r1,c1);

System.out.println("reading data in matrix");

readData(filename, matrix[r1][c1]);

System.out.println("sorting matrix");

sortMatrix(matrix[r1][c1]);

System.out.println("the sorted matrix is:");

printMatrix(matrix[r1][c1]);

} // end of the main method

} // end of the class

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