In: Computer Science
I am using NetBeans IDE Java to code and I am seeking comment to the code as well (for better understanding).
Write a program that reads in 16 values from the keyboard, displays them in a 4 x 4 array, and tests whether they form a magic square.
Example:
16 3 2 13
5 10 11 8
9 6 7 12
4 15 14 1
You need to test two features:
Hint: use a two-dimentional array.
public void turnLeft()
public void turnRight()
public void move()
public Point getLocation()
public String getDirection()
The turnLeft and turnRight methods change the direction but not the location. The move method moves the robot by one unit in the direction it is facing. The getDirection method returns a string "N", "E", "S", or "W".
package Matrix;
import java.util.Scanner;
// Defines class MagicSquareTest
public class MagicSquareTest
{
// Method to accept data to matrix
static void accpetData(int matrix[][])
{
// Scanner class object
created
Scanner sc = new
Scanner(System.in);
// Loops till number of rows
for(int r = 0; r <
matrix.length; r++)
{
// Loops till
number of columns
for(int c = 0; c
< matrix[r].length; c++)
{
// Accepts data for current cell
System.out.print("\n Enter the data for [" + r +
"] [" + c + "]: ");
matrix[r][c] = sc.nextInt();
}// End of for
loop for column
}// End of for loop for row
// Loops to display the
matrix
// Loops till number of rows
for(int r = 0; r <
matrix.length; r++)
{
// Loops till
number of columns
for(int c = 0; c
< matrix[r].length; c++)
{
// Accepts data for current cell
System.out.print(matrix[r][c] + " ");
}// End of for
loop for column
System.out.println();
}// End of for loop for row
// Close the scanner
sc.close();
}// End of method
// Method to return true if parameter matrix is magic
matrix
// Otherwise returns false
static boolean checkMagicMatrix(int matrix[][])
{
// calculate the sum of the prime
diagonal
int leftDiagonalSum = 0, rightDiagonalSum = 0;
// Loops till end of the matrix to calculate the sum
of left diagonal
for(int r = 0; r < matrix.length; r++)
leftDiagonalSum = leftDiagonalSum +
matrix[r][r];
// Loops till end of the matrix to calculate the sum
of right diagonal
for(int r = 0; r < matrix.length; r++)
rightDiagonalSum = rightDiagonalSum
+ matrix[r][matrix.length - 1 - r];
// Checks if left and right diagonal sum is not equal
then return false
if(leftDiagonalSum != rightDiagonalSum)
return false;
// Loops to calculate each row sum
// Loops till number of rows
for(int r = 0; r < matrix.length; r++)
{
// To store each row sum
int rowSum = 0;
// Loops till number of columns
for(int c = 0; c < matrix.length; c++)
rowSum += matrix[r][c];
// Checks if current row sum is not equals to left
diagonal sum
// then returns false
if (rowSum != leftDiagonalSum)
return false;
}// End of for loop
// Loops to calculate each column sum
// Loops till number of rows
for(int r = 0; r < matrix.length; r++)
{
// To store each column sum
int colSum = 0;
// Loops till number of columns
for(int c = 0; c < matrix.length; c++)
colSum += matrix[c][r];
// Checks if current column sum is not equals to left
diagonal sum
// then returns false
if(colSum != leftDiagonalSum)
return false;
}// End of for loop
// Otherwise returns true
return true;
}// End of method
// main method definition
public static void main(String []s)
{
// Declares a matrix of 4 x 4
int matrix[][] = new
int[4][4];
// Calls the method to accept and
display matrix
accpetData(matrix);
// Call the method to check matrix
is magic matrix or not
if(checkMagicMatrix(matrix))
System.out.print("\n Matrix is magic matrix.");
else
System.out.print("\n Matrix is not a magic matrix.");
}// End of main method
}// End of class
Sample Output:
Enter the data for [0] [0]: 16
Enter the data for [0] [1]: 3
Enter the data for [0] [2]: 2
Enter the data for [0] [3]: 13
Enter the data for [1] [0]: 5
Enter the data for [1] [1]: 10
Enter the data for [1] [2]: 11
Enter the data for [1] [3]: 8
Enter the data for [2] [0]: 9
Enter the data for [2] [1]: 6
Enter the data for [2] [2]: 7
Enter the data for [2] [3]: 12
Enter the data for [3] [0]: 4
Enter the data for [3] [1]: 15
Enter the data for [3] [2]: 14
Enter the data for [3] [3]: 1
16 3 2 13
5 10 11 8
9 6 7 12
4 15 14 1
Matrix is magic matrix.