Write a java program to read an integer N (2 <= N <= 30) from an input filename
ID: 3721161 • Letter: W
Question
Write a java program to read an integer N (2 <= N <= 30) from an input filename “input5_01.txt” and then
a. Write a function to create an N x N matrix with random integers from the range [10, 99]. Output that matrix to the output filename “output5_01.txt”.
b. Find the maximum number of each row and each column of the matrix created in part a). Output those number to the same file “output5_01.txt”.
c. Find all primes in the matrix and output those to same file “output5_01.txt”.
Note: Do not print out duplicate primes.
Example: Input File “input5_01.txt”
5
Output File “output4_01_1.txt” + Part a) 13 56 95 11 16 12 23 25 77 83 19 99 21 86 46 34 66 29 67 42 29 20 50 71 83 + Part b) Max rows: 95 83 99 67 83 Max cols: 34 99 95 86 83 + Part c) 13 11 23 83 19 29 67 29 71
Explanation / Answer
package grade;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashSet;
import java.util.Set;
public class Matrix {
public static void writeMatrix(String matrix) {
FileWriter fileWriter=null;
PrintWriter writer=null;
try {
fileWriter = new FileWriter("output5_01.txt");
writer=new PrintWriter(fileWriter);
writer.print(matrix);
} catch (IOException e) {
System.out.println(e.getMessage());
}
finally {
if(writer!=null)
writer.close();
if(fileWriter!=null)
try {
fileWriter.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}
public static int checkMaxRow(int[][] matrix, int row) {
int max=Integer.MIN_VALUE;
for(int i=0;i<matrix.length;i++) {
int element=matrix[row][i];
if(element>max)
max=element;
}
return max;
}
public static int checkMaxCol(int[][] matrix, int col) {
int max=Integer.MIN_VALUE;
for(int i=0;i<matrix.length;i++) {
int element=matrix[i][col];
if(element>max)
max=element;
}
return max;
}
public static boolean isPrime(int n) {
if(n < 2) return false;
if(n == 2 || n == 3) return true;
if(n%2 == 0 || n%3 == 0) return false;
long sqrtN = (long)Math.sqrt(n)+1;
for(long i = 6L; i <= sqrtN; i += 6) {
if(n%(i-1) == 0 || n%(i+1) == 0) return false;
}
return true;
}
public static Set<Integer> findPrimes(int[][] matrix){
Set<Integer> set=new HashSet<>();
for(int i=0; i<matrix.length; i++) {
for(int j=0;j< matrix.length;j++) {
if(isPrime(matrix[i][j]))
set.add(matrix[i][j]);
}
}
return set;
}
public static void main(String[] args) {
String path="input5_01.txt";
BufferedReader reader=null;
try {
reader=new BufferedReader(new FileReader(path));
String Line=null;
Line=reader.readLine();
if(Line==null)
throw new Exception("Input file is empty");
int N=Integer.parseInt(Line);
int[][] matrix=new int[N][N];
String s="output4_01_1.txt + Part a) ";
for(int i=0;i<N;i++) {
for(int j=0;j<N;j++) {
matrix[i][j]= (int) (Math.random() * (89)) + 10;
System.out.print(matrix[i][j]+" ");
s+=matrix[i][j]+" ";
}
System.out.println();
}
String maxRows="";
String maxCols="";
for(int i=0;i<N;i++) {
maxRows+=checkMaxRow(matrix, i)+" ";
maxCols+=checkMaxCol(matrix, i)+" ";
}
s+=" Part b) Max Rows"+maxRows+" Max Cols "+maxCols;
s+="Part c) ";
Set<Integer> set=findPrimes(matrix);
for (Integer integer : set) {
s+=integer+" ";
}
writeMatrix(s);
}catch (NumberFormatException e) {
System.out.println("Input file does not contain a valid number");
}catch (Exception e) {
System.out.println(e.getMessage());
}
finally {
if(reader!=null)
try {
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.