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

write program in JAVA to read input text. There is a square 3x3 amtrix which pas

ID: 3877603 • Letter: W

Question

write program in JAVA to read input text. There is a square 3x3 amtrix which passes +1 row through all rows of matrix. The value at the center in each 3x3 matrix is replaced with highest value. Result is then sent to txt file.

Ex for input matrix

1 3 5 7 9 1 3 5 7 9

2 4 6 8 2 4 6 8 2 4

3 5 7 9 1 3 5 7 9 5

4 6 8 2 4 6 8 2 4 6

5 7 9 1 3 5 7 9 1 3

6 8 2 4 6 8 2 4 6 8

7 9 1 3 5 7 9 1 3 5

8 2 4 6 8 2 4 6 8 2

9 1 3 5 7 9 1 3 5 7

0 2 4 6 8 2 4 6 8 2

for ex;

first two rows is:

1 3 5 7 9 1 3 5 7 9

2 4 6 8 2 4 6 8 2 4

passing 3x3 matrix would change first two rows to:

4 6 8 9 9 9 8 8 8 9

5 7 9 9 9 9 8 9 9 9

Explanation / Answer

import java.util.Scanner;
import java.io.FileWriter;

class Main {
public static void main(String[] args) {
  
// DECLARING variables
int i,j,m,n, rows = 10;
  
System.out.println("Enter matrix line by line, separated by spaces");
  
Scanner sc = new Scanner(System.in);
int[][] a = new int[rows+2][rows+2];
int[][] b = new int[rows][rows];
  
// to automatically generate 0's for first column
for(j=0;j<rows+1;j++)
{
a[0][j] = 0;
}
  
// to automatically generate 0's for first row
for(i=0;i<rows+1;i++)
{
a[i][0] = 0;
}
  
// user input
for(i=1; i<rows+1; i++)
{
for(j=1; j<rows+1; j++)
{
a[i][j] = sc.nextInt();
}
// to automatically generate 0's for last column
a[i][j] = 0;
}
  
// to automatically generate 0's for last row
for(j=0;j<rows+1;j++)
{
a[i][j] = 0;
}
  
// main logic to get maximum of 3x3 matrix
for(i=0; i<rows; i++)
{
for(j=0; j<rows; j++)
{
int max = a[i][j];
for(m=i; m<3+i; m++)
{
for(n=j; n<3+j; n++)
{
if(a[m][n] > max)
{
max = a[m][n];
}
}
}
b[i][j] = max;
}
}
  
// writing to file
try{
FileWriter thisWrites=new FileWriter("out.txt");

  
for(i=0; i<rows; i++)
{
for(j=0; j<rows; j++)
{
thisWrites.write(b[i][j]+" ");
}
thisWrites.write(" ");
}
  
thisWrites.close();
}catch(Exception e){}
}
}

/*SAMLE OUTPUT to out.txt
4 6 8 9 9 9 8 8 9 9
5 7 9 9 9 9 8 9 9 9
6 8 9 9 9 8 8 9 9 9
7 9 9 9 9 8 9 9 9 9
8 9 9 9 8 8 9 9 9 8
9 9 9 9 8 9 9 9 9 8
9 9 9 8 8 9 9 9 8 8
9 9 9 8 9 9 9 9 8 8
9 9 6 8 9 9 9 8 8 8
9 9 6 8 9 9 9 8 8 8
*/