In: Computer Science
THIS IS JAVA
Magic squares.
An n × n matrix that is filled with the numbers 1, 2, 3, . . ., n^2 is a magic square if the sum of the elements in each row, in each column, and in the two diagonals is the same value.
Write a program that randomly generates 16 numbers, and it assigns them to the array after testing that the number was not already assigned. The program should test whether they form a magic square when put into a 4 × 4 array.
You need to test two features:
1. Build the array so that each of the numbers 1, 2, ..., 16 is present?
2.When the numbers are put into a square, are the sums of the rows, columns, and diagonals equal to each other? If so, display an appropriate message.
package test;
import java.util.ArrayList;
import java.util.Random;
public class magicMatrix {
// method to check if the matrix is magic
public static boolean magicCheck(int[][] mat) {
int sum = rowSum(mat,0);
for(int i=0;i<4;i++) {
// check if
mismatch in sum occurs in rows
if(sum !=
rowSum(mat,i)) {
return false;
}
}
for(int i=0;i<4;i++) {
// check if
mismatch in sum occurs in cols
if(sum !=
colSum(mat,i)) {
return false;
}
}
// check if mismatch in sum occurs
in diagonal in left direction
if(sum!= diagLSum(mat))
return
false;
// check if mismatch in sum occurs
in diagonal in right direction
if(sum!=diagRSum(mat))
return
false;
return true;
}
//calculates row sum
public static int rowSum(int[][] mat,int r) {
int sum=0;
for(int i=0;i<4;i++) {
sum+=mat[r][i];
}
return sum;
}
//calculates column sum
public static int colSum(int[][] mat,int c) {
int sum=0;
for(int i=0;i<4;i++) {
sum+=mat[i][c];
}
return sum;
}
public static int diagLSum(int[][] mat) {
int sum=0;
for(int i=0;i<4;i++) {
sum+=mat[i][i];
}
return sum;
}
//calculates diagonal sum in right diagonal
public static int diagRSum(int[][] mat) {
int sum=0;
for(int i=0;i<4;i++) {
sum+=
mat[i][3-i];
}
return sum;
}
//prints the matrix
public static void printMat(int[][] mat) {
for(int i=0;i<4;i++) {
for(int
j=0;j<4;j++) {
System.out.print(" "+mat[i][j]);
}
System.out.println("\n");
}
}
static Random rand;
public static void main(String[] args) {
// TODO Auto-generated method
stub
rand = new Random();
rand.setSeed(System.nanoTime());
//creates an array of
integers
ArrayList<Integer> array =
new ArrayList<Integer>();
//adds the randomly generated
numbers from 1 to 16 in the array
for(int i=0;i<16;) {
int
y=rand.nextInt(16)+1;
if(!array.contains(y)) {
array.add(y);
i++; // increments i if y is added to the
array
}
}
//declares the matrix
int[][] matrix= new
int[4][4];
//adds integers from array into
matrix
for(int i=0;i<4;i++) {
for(int
j=0;j<4;j++) {
matrix[i][j]=array.get(i*4+j);
}
}
//prints the matrix
System.out.println("The matrix
generated is -> ");
printMat(matrix);
//checks if the matrix is magic or
not and displays the appropriate message
if(magicCheck(matrix)) {
System.out.println("This matrix is a magic matrix");
}else {
System.out.println("This matrix is not a magic matrix");
}
}
}
==========================================================
above is result of one run..
Thanks
The matrix generated is -> 1 15 8 6 12 9 2 5 13 11 10 4 7 14 16 3 This matrix is not a magic matrix