(java programming:methods): program you are to write will create & fill an 9x9,
ID: 3626066 • Letter: #
Question
(java programming:methods): program you are to write will create & fill an 9x9, 2 dmensional array with randomly gererated letters a to d. program will analyze & re-port if it finds at least 3 consecutive letters in any row, column, diagonal(upper left to lower right), or sub-diagonal(upper right to lower left). nothing is reported if no consecutive letters are found. only one analysis per row or column is needed. once a match has been found you are to move to the next row, column, etc.your program should have at least one method to: generate, display, and complete each analysis.MUST: generate & display a 9x9, 2 dimension array of characters filled with a,b,c,d.
correctly analyzes the matrix row
correctly analyzes the matrix column
correctly analyzes the matrix diagonal
correctly analyzes the matrix sub-diagonal
meaningful identifers, internal and javadoc comments
Explanation / Answer
please rate - thanks
import java.util.*;
public class mat
{
public static void main(String[] args)
{int n=9,i;
char[][] a = new char [n][n];
a=fillarray(n); //fill array
display(a,n); //display array
for(i=0;i<n;i++)
if(row(a,n,i)) //check rows
System.out.println("row "+i+" has 3 in a row");
else
System.out.println("row "+i+" does not have 3 in a row");
for(i=0;i<n;i++)
if(col(a,n,i)) //check columns
System.out.println("column "+i+" has 3 in a row");
else
System.out.println("column "+i+" does not have 3 in a row");
if(diag1(a,n)) //check main diagonal
System.out.println("main diagonal has 3 in a row");
else
System.out.println("main diagonal does not have 3 in a row");
if(diag2(a,n)) //check sub diagonall
System.out.println("sub-diagonal has 3 in a row");
else
System.out.println("sub-diagonal does not have 3 in a row");
}
public static boolean col(char a[][],int n,int c)
{int i;
for(i=1;i<n-1;i++)
if(a[i][c]==a[i-1][c]&&a[i-1][c]==a[i+1][c]) //check adjacent columns
return true;
return false;
}
public static boolean row(char a[][],int n,int r)
{int i;
for(i=1;i<n-1;i++) //check adjacent rows
if(a[r][i]==a[r][i-1]&&a[r][i-1]==a[r][i+1])
return true;
return false;
}
public static boolean diag1(char a[][],int n)
{int i;
for(i=1;i<n-1;i++) //check adjacents on main diagonal
if(a[i][i]==a[i-1][i-1]&&a[i-1][i-1]==a[i+1][i+1])
return true;
return false;
}
public static boolean diag2(char a[][],int n)
{int i;
for(i=1;i<n-1;i++) //check adjacents on sub diagonal
if(a[i][n-i-1]==a[i-1][n-i]&&a[i-1][n-i]==a[i+1][n-2-i])
return true;
return false;
}
public static char[][] fillarray(int n)
{int i,j;
Random r=new Random();
char[][] a = new char [n][n];
for(i=0;i<n;i++)
for(j=0;j<n;j++)
a[i][j]=(char)(r.nextInt('d'-'a'+1)+'A'); //generate a-d }
return a;
}
public static void display(char a[][],int n)
{ int i,j;
System.out.println("The Matrix:");
System.out.print(" "); //space
for(i=0;i<n;i++) //label columns
System.out.print(i+" ");
System.out.println();
for(i=0;i<n;i++)
{System.out.print(i+" "); //print row *
for(j=0;j<n;j++)
System.out.print(a[i][j]+" ");
System.out.println(" ");
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.