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 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,...
Program: Java Write a Java program using good programming principles that will aggregate the values from...
Program: Java Write a Java program using good programming principles that will aggregate the values from several input files to calculate relevant percentages and write the values to an output file. You have been tasked with reading in values from multiple files that contains different pieces of information by semester. The Department of Education (DOE) would like the aggregate values of performance and demographic information by academic year. A school year begins at the fall semester and concludes at the...
Program: Java Write a Java program using good programming principles that will aggregate the values from...
Program: Java Write a Java program using good programming principles that will aggregate the values from several input files to calculate relevant percentages and write the values to an output file. You have been tasked with reading in values from multiple files that contains different pieces of information by semester.    The Department of Education (DOE) would like the aggregate values of performance and demographic information by academic year. A school year begins at the fall semester and concludes at the...
This for Java Programming Write a java statement to import the java utilities. Create a Scanner...
This for Java Programming Write a java statement to import the java utilities. Create a Scanner object to read input. int Age;     Write a java statement to read the Age input value 4 . Redo 1 to 3 using JOptionPane
java 2D array / recursion explain every method You are requested to write a Java program...
java 2D array / recursion explain every method You are requested to write a Java program of a simple Memory Management Unit. The program should allow the following: 1. The user can create a process asking for memory. The program will return a process ID if the requested memory can be allocated. It will also print the allocated Base and Limit. 2. The user can delete a process by specifying a process ID. The program should do that and free...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT