Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have a java assignment that I really need help with. My instructor asked me to

ID: 3765187 • Letter: I

Question

I have a java assignment that I really need help with. My instructor asked me to write a java source code for a Lo Shu Magic Square with 3 rows and 3 columns with the numbers from 1-9. Believe me he is not the best teacher one could have. Every time I attempt to solve the problem I always get errors. I know that this exercise could be extremely easy for some of you experts out there. Can somebody please help me with the codes for the assignement below? That would be greatly appreciated.

The Lo Shu Magic Square is a grid (array) with 3 rows and 3 columns. The Lo Shu Magic square has the following properties:

·         The grid contains the number 1 through 9 only . Each number is used once and only once.

·         The sum of each row, each column and the two diagonals add up to the identical value.

Write a program with a Boolean function which evaluates to True if the array parameter is a Lo Shu Magic Square and returns False if the array is not.

Test the program with the following data for several arrays:

Case 1: 1 2 3 4 5 6 7 8 9

Case 2: 3 1 4 5 9 2 6 7 8

Case 3: 1 7 9 2 5 0 6 3 4

Case 4: 3 1 4 5 9 2 6 4 8

Case 5: 4 9 2 3 5 7 8 1 6

Explanation / Answer

package current;

import java.util.ArrayList;

public class LoShuMagicSquare {

   public static void main(String[] args) {
      
       //Case 1: 1 2 3 4 5 6 7 8 9
       int gridVal[][] = { { 1, 2, 3} , {4, 5, 6} , {7, 8, 9}};
       System.out.println("Case 1: 1 2 3 4 5 6 7 8 9   Valid: " +LoShuMagicSquare.evaluate(gridVal));

//       Case 2: 3 1 4 5 9 2 6 7 8
       int gridVal2[][] = { { 3, 1, 4 } , {5,9,2} , {6,7, 8}};
       System.out.println("Case 2: 3 1 4 5 9 2 6 7 8   Valid: " +LoShuMagicSquare.evaluate(gridVal2));
//      
//       Case 3: 1 7 9 2 5 0 6 3 4
       int gridVal3[][] = { { 1,7,9 } , {2,5,0} , {6,3,4}};
       System.out.println("Case 3: 1 7 9 2 5 0 6 3 4 Valid: " +LoShuMagicSquare.evaluate(gridVal3));
//      
//       Case 4: 3 1 4 5 9 2 6 4 8
  
//       Case 5: 4 9 2 3 5 7 8 1 6
       int gridVal5[][] = { { 4,9,2} , {3,5,7} , {8,1,6}};
       System.out.println("Case 5: 4 9 2 3 5 7 8 1 6 Valid: " +LoShuMagicSquare.evaluate(gridVal5));
   }
  
   // Lo Shu Magic Square with 3 rows and 3 columns with the numbers from 1-9.
   public static boolean evaluate(int gridVal[][]) {
       // The grid contains the number 1 through 9 only . Each number is used once and only once.

      // The sum of each row, each column and the two diagonals add up to the identical value.

       if(gridVal != null) {
           ArrayList<Integer> entries = new ArrayList<Integer>();
           int sumRows[] = new int[] {0,0,0};
           int diagonal[] = new int[] {0,0};
           for(int i = 0; i < 3; i++) {
               for(int j = 0; j < 3; j++) {
                   //// The grid contains the number 1 through 9 only
                   if(gridVal[i][j] < 1 || gridVal[i][j] > 9)
                       return false;
                   //Each number is used once and only once.
                   if(entries.contains(gridVal[i][j]))
                       return false;
                   sumRows[i] += gridVal[i][j];
                   //1,5,9 elements
                   if(i == j) {
                       diagonal[0] += gridVal[i][j];
                   }
               }
           }
          
           //2nd diagnoal
           diagonal[1] = gridVal[0][2] + gridVal[1][1] + gridVal[2][0];
          

              // The sum of each row, each column and the two diagonals add up to the identical value.
           if(sumRows[0] == sumRows[1] && sumRows[0] == sumRows[2] && sumRows[0] == diagonal[0] && sumRows[0] == diagonal[1])
               return true;
       }
       return false;
   }
}

-------------output---------------

Case 1: 1 2 3 4 5 6 7 8 9   Valid: false
Case 2: 3 1 4 5 9 2 6 7 8   Valid: false
Case 3: 1 7 9 2 5 0 6 3 4 Valid: false
Case 5: 4 9 2 3 5 7 8 1 6 Valid: true