Question

In: Computer Science

write a java programming using 2D arrey. The Lo Shu Magic Square is a grid with...

write a java programming using 2D arrey.
The Lo Shu Magic Square is a grid with 3 rows and 3 columns. The Lo Shu Magic Square has the following properties:

The grid contains the numbers 1 through 9 (each number only once)

The sum of each row, each column and each diagonal are the same

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 function in a program.

Detailed Description:

In main create two arrays:

// Create a magic two-dimensional array.

      int[][] magicArray = { {4, 9, 2},

                             {3, 5, 7},

                             {8, 1, 6} };

      // Create a normal two-dimensional array.

      int[][] normalArray = { {1, 2, 3},

                              {4, 5, 6},

                              {7, 8, 8} };

For the normalArray call showArray and then showResult. Then do the same for magicArray

showArray accepts a two-dimensional array and prints it row by row

showResult call isMagicSquare with the two-dimensional array reference as the parameter. It prints a message based on the return value which is Boolean.

The isMagicSquare method accepts a two-dimensional int array as an argument, and returns true if the array meets all the requirements of a magic square. Otherwise it returns false. You can create separate methods to checkRange (all must be from 1-9), checkUnique (see if all values are unique), checkRowSum, checkColSum, checkDiagSum. If each of these 3 return true then it is a Magic Square.

Output should be

1 2 3

4 5 6

7 8 8

This is not a Lo Shu magic square.

4 9 2

3 5 7

8 1 6

This is a Lo Shu magic square.

Solutions

Expert Solution


public class CheckMagicSquare {

private static boolean isMagicSquare(int[][] magicArray) {
// calculate the sum of
// the prime diagonal
int sum = 0,sum2=0;
int N = magicArray.length;
for (int i = 0; i < N; i++)
sum = sum + magicArray[i][i];
// the secondary diagonal
for (int i = 0; i < N; i++)
sum2 = sum2 + magicArray[i][N-1-i];

if(sum!=sum2)
return false;

// For sums of Rows
for (int i = 0; i < N; i++) {

int rowSum = 0;
for (int j = 0; j < N; j++)
rowSum += magicArray[i][j];

// check if every row sum is
// equal to prime diagonal sum
if (rowSum != sum)
return false;
}

// For sums of Columns
for (int i = 0; i < N; i++) {
int colSum = 0;
for (int j = 0; j < N; j++)
colSum += magicArray[j][i];
// check if every column sum is
// equal to prime diagonal sum
if (sum != colSum)
return false;
}

return true;
}
  
public static void main(String[] args) {
int[][] magicArray = { {4, 9, 2},

{3, 5, 7},

{8, 1, 6} };

int[][] normalArray = { {1, 2, 3},

{4, 5, 6},

{7, 8, 8} };
if( isMagicSquare(normalArray)){
for (int i = 0; i < normalArray.length; i++) {
for (int j = 0; j < normalArray.length; j++) {
System.out.print(normalArray[i][j]+" ");
  
}
System.out.println("");
}
System.out.println("This is a Lo Shu magic square.");
}else{
for (int i = 0; i < normalArray.length; i++) {
for (int j = 0; j < normalArray.length; j++) {
System.out.print(normalArray[i][j]+" ");
  
}
System.out.println("");
}
System.out.println("This is not a Lo Shu magic square.");
}
if (isMagicSquare(magicArray)){
for (int i = 0; i < magicArray.length; i++) {
for (int j = 0; j < magicArray.length; j++) {
System.out.print(magicArray[i][j]+" ");
  
}
System.out.println("");
}
System.out.println("This is a Lo Shu magic square.");
}else{
for (int i = 0; i < magicArray.length; i++) {
for (int j = 0; j < magicArray.length; j++) {
System.out.print(magicArray[i][j]+" ");
  
}
System.out.println("");
}
System.out.println("This is a Lo Shu magic square.");
}
}
  
}

/* PLEASE UPVOTE */


Related Solutions

Python: Lo Shu Magic Square The Lo Shu Magic Square is a grid with 3 rows...
Python: Lo Shu Magic Square The Lo Shu Magic Square is a grid with 3 rows and 3 columns, shown in figures below. 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 B. In a program you can stimulate a magic square using a two-dimensional list. Write a function...
The Lo Shu Magic Square is a grid with 3 rows and 3 columns, shown in figure. The Lo Shu Magic Square has the following properties:
Python programmingThe Lo Shu Magic Square is a grid with 3 rows and 3 columns, shown in figure. 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 as shown in figure.In a program you can simulate a magic square using a two-dimensional list. Write a function that accepts a two-dimensional list as an argument and determines...
The Lo Shu Magic Square is a grid with 3 rows and 3 columns shown in...
The Lo Shu Magic Square is a grid with 3 rows and 3 columns shown in Figure 8-23. The Lo Shu Magic Square has the following properties: l The grid contains the numbers 1 through 9 exactly. l The sum of each row, each column, and each diagonal all add up to the same number. This is shown in Figure 8-24. In a program, you can simulate a magic square using a two-dimensional array. Design a program that initializes a...
Write a method that will accept a 2D character array. The 2D array represents a grid...
Write a method that will accept a 2D character array. The 2D array represents a grid (table) of characters in which a triple may occur. A triple is 3 matching characters. This method will search through the array to determine whether or not it contains a set of 3 matching characters. Specifically, it will check to see if the 3 matching characters appear somewhere in the array as three adjacent characters either horizontally (left to right) or vertically (top to...
java Write method that takes a 2D square array and two integer values that represent the...
java Write method that takes a 2D square array and two integer values that represent the indices of the two columns. Method swaps the first two columns. int[][] in = {{1,2,3}, {3,2,4}, {4,2,7}}; would output 213234247 public static void swap(int[][] in, int index1, int index2) {
. Write a program to print * in the following order using 2d array in java...
. Write a program to print * in the following order using 2d array in java                                              *             *             *             *             *                                              *             *             *             *                                              *             *             *                                              *             *                                                          *
Using any existing 2D or 3D graphics library ( such as Java 2D, Java 3D or...
Using any existing 2D or 3D graphics library ( such as Java 2D, Java 3D or OpenGL ) draw a scene featuring various elements. You are to choose from one of the following categories: Any man-made landscape ( using Java 2D, or Java 3D, or OpenGL) Promoting a cause (Using Java 3D or OpenGL ) 3. Any visual landscape element of your choice (Using OpenGL with Java or Python ) You are free to create whatever you choose but it...
Write a java program of a multiplication table of binary numbers using a 2D array of...
Write a java program of a multiplication table of binary numbers using a 2D array of integers.
Using any existing 2D or 3D graphics library ( Java 2D, Java 3D, draw a scene...
Using any existing 2D or 3D graphics library ( Java 2D, Java 3D, draw a scene within one of the following categories: Satire or humor Promote a cause You are free to create whatever you choose but it must conform to the following guidelines.   Show evidence of at least four colors. Have a textual composition on the finished product. Imagery or images Scene composition of at least six (6) elements One of the following 1) Shadows or Glows. May be...
using java LO: (Apply) Students will write loops that iterate until a condition is met. LO:...
using java LO: (Apply) Students will write loops that iterate until a condition is met. LO: (Analyze) Students will identify edge cases from a problem statement. In this game of volleyball, two teams compete to be the first team to score 21 points. However, to win the game, a team must also lead by two points. For example, if team A is tied with team B (20-20) and then scores, they will only be ahead by one point (21-20). Then,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT