JAVA PROGRAMMING 1. LoShuMagicSquare The Lo Shu Magic Square is a grid with 3 ro
ID: 3866103 • Letter: J
Question
JAVA PROGRAMMING
1. LoShuMagicSquare The Lo Shu Magic Square is a grid with 3 rows and 3 columns shown in Figure 1. The Lo Shu Magic Square has the following properties: • The grid contains the numbers 1 through 9 exactly. • The sum of each row, each column, and each diagonal all add up to the same number. This is shown in Figure 2. In a program you can simulate a magic square using a two-dimensional array. Write a method that accepts a two-dimensional array as an argument, and determines whether the array is a Lo Shu Magic Square. Test the method in a program.
15 4 2 15 3 5 715 8 615 15 15 15 15 15 15 15 Figure 1 Figure 2Explanation / Answer
Note:
Please check the code now..Thank you.
___________________________
LoShuMagicSquare.java
import java.util.Arrays;
import java.util.Scanner;
public class LoShuMagicSquare {
public static void main(String[] args) {
//Scanner Object is used to get the inputs from the user.
Scanner keyboard = new Scanner(System.in);
while (true) {
int[][] square = new int[3][3];
//here This Nested loop will take the user given values and fill the two-dimensional array
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
System.out.print("Input value for row " + (row + 1) + " column " + (col + 1) + "::");
square[row][col] = keyboard.nextInt();
}
}
//Calling the method to check the matrix is a magic square or not.
String str = magicSum(square);
System.out.println(str);
System.out.println("Continue (Y/N) ::");
char ch = keyboard.next(".").charAt(0);
if (ch == 'Y' || ch == 'y')
continue;
else {
System.out.println(":: Program Exit ::");
}
break;
}
}
// Method code which contains logic which checks whether the matrix is a magic square or not.
private static String magicSum(int[][] square) {
int order = square.length;
int[] sumRow = new int[order];
int[] sumCol = new int[order];
int[] sumDiag = new int[2];
Arrays.fill(sumRow, 0);
Arrays.fill(sumCol, 0);
Arrays.fill(sumDiag, 0);
//calculating the Sum of Each row
for (int row = 0; row < order; row++) {
for (int col = 0; col < order; col++) {
sumRow[row] += square[row][col];
}
System.out.println("sum row " + row + "::" + sumRow[row]);
}
//calculating the Sum of Each column
for (int col = 0; col < order; col++) {
for (int row = 0; row < order; row++) {
sumCol[col] += square[row][col];
}
System.out.println("sum columns " + col + "::" + sumCol[col]);
}
//calculating the Sum of Diagonal 1
for (int row = 0; row < order; row++) {
sumDiag[0] += square[row][row];
}
System.out.println("sum diagonal 0 " + "::" + sumDiag[0]);
//calculating the Sum of Diagonal 2
for (int row = 0; row < order; row++) {
sumDiag[1] += square[row][order - 1 - row];
}
System.out.println("sum diagonal 1 " + "::" + sumDiag[1]);
boolean bool = true;
int sum = sumRow[0];
for (int i = 1; i < order; i++) {
bool = bool && (sum == sumRow[i]);
}
for (int i = 0; i < order; i++) {
bool = bool && (sum == sumCol[i]);
}
for (int i = 0; i < 2; i++) {
bool = bool && (sum == sumDiag[i]);
}
if (bool) {
return "This is a Lo Shu Magic Square whose sum is::" + sum;
} else {
return " :: This is not a Lo Shu Magic Square :: ";
}
}
}
______________________
Output:
Input value for row 1 column 1::4
Input value for row 1 column 2::9
Input value for row 1 column 3::2
Input value for row 2 column 1::3
Input value for row 2 column 2::5
Input value for row 2 column 3::7
Input value for row 3 column 1::8
Input value for row 3 column 2::1
Input value for row 3 column 3::6
sum row 0::15
sum row 1::15
sum row 2::15
sum columns 0::15
sum columns 1::15
sum columns 2::15
sum diagonal 0 ::15
sum diagonal 1 ::15
This is a Lo Shu Magic Square whose sum is::15
Continue (Y/N) ::
Y
Input value for row 1 column 1::3
Input value for row 1 column 2::5
Input value for row 1 column 3::7
Input value for row 2 column 1::2
Input value for row 2 column 2::9
Input value for row 2 column 3::4
Input value for row 3 column 1::8
Input value for row 3 column 2::6
Input value for row 3 column 3::1
sum row 0::15
sum row 1::15
sum row 2::15
sum columns 0::13
sum columns 1::20
sum columns 2::12
sum diagonal 0 ::13
sum diagonal 1 ::24
:: This is not a Lo Shu Magic Square ::
Continue (Y/N) ::
N
:: Program Exit ::
_______________Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.