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

PLEASE HELP WRITE THE CODE IN JAVA LANGUAGUAGE . THANKS. In recreational mathema

ID: 3589430 • Letter: P

Question

PLEASE HELP WRITE THE CODE IN JAVA LANGUAGUAGE. THANKS.

In recreational mathematics, a magic square is an arrangement of numbers (usually integers) in a square grid, where the numbers in each row, and in each column, and the numbers that run diagonally in both directions, all add up to the same number. A magic square has the same number of rows as it has columns, and in conventional math notation, "n" stands for the number of rows (and columns) it has. Thus, a magic square always contains n2 numbers, and its size (the number of rows [and columns] it has) is described as being "of order n". A magic square that contains the integers from 1 to n2 is called a normal magic square.

M = (n(n^2 + 1)) / 2

The constant that is the sum of every row, column and diagonal is called the magic constant or magic sum, M. Every normal magic square has a unique constant determined solely by the value of n, which can be calculated using this formula:

In this assignment, you are to write a program that takes a 2-dimensional array of ints as input and determines if it is in fact a normal magic square.

You will need to implement each of the following methods:

public static int magicSum(int[][] array)

public static boolean rowsAreAllEqualToMagicSum(int[][] array)

public static boolean columnsAreAllEqualToMagicSum(int[][] array)

public static boolean diagonalsAreBothEqualToMagicSum(int[][] array)

public static boolean isMagicSquare(int[][] array)

public static boolean isNormalMagicSquare(int[][] array)

public static int getSumOfDownDiagonal(int[][] array)

public static int getSumOfUpDiagonal(int[][] array)

public static int getSumOfRow(int[][] array, int index)

public static int[] getColumn(int[][] array, int index)

public static int getSumOfColumn(int[] array)

You should test your program using a variety of 2-dimensional arrays.

Explanation / Answer

class IsMagicSquare
{
    public static int magicSum(int[][] array)
    {
       int n = array.length;
       return n * (n*n + 1) / 2;
    }
    public static boolean rowsAreAllEqualToMagicSum(int[][] array)
    {
       int magicSum = magicSum(array);
      
       for(int i = 0; i < array.length; i++)   //For each row
       {
          int sum = 0;   //Initially sum = 0.
          for(int j = 0; j < array.length; j++)   //For each column.
              sum += array[i][j];   //Calculate the sum of all columns.
          if(sum != magicSum)   //If the sum of that row is not magicSum
              return false;   //It fails test.
       }
       return true;   //If all tests pass.
    }
    public static boolean columnsAreAllEqualToMagicSum(int[][] array)
    {
       int magicSum = magicSum(array);
      
       for(int i = 0; i < array.length; i++)   //For each column
       {
          int sum = 0;   //Initially sum = 0.
          for(int j = 0; j < array.length; j++)   //For each row.
              sum += array[j][i];   //Calculate the sum of all rows.
          if(sum != magicSum)   //If the sum of that column is not magicSum
              return false;   //It fails test.
       }
       return true;   //If all tests pass.
    }
    public static boolean diagonalsAreBothEqualToMagicSum(int[][] array)
    {
       int magicSum = magicSum(array);
       int sum = 0;
       for(int i = 0; i < array.length; i++)   //For the leading diagonal.
           sum += array[i][i];   //Calculate the sum of the diagonal.
       if(sum != magicSum)   //If the diagonal sum is not magicSum.
           return false;   //It fails test.
       sum = 0;   //Again sum initialized to 0.
       for(int i = 0; i < array.length; i++)   //For the trialing diagonal.
           sum += array[array.length-i-1][i];   //Calculate the sum of the diagonal.
       if(sum != magicSum)   //If the diagonal sum is not magicSum.
           return false;   //It fails test.
       return true;   //If both tests pass.          
    }
    public static boolean isMagicSquare(int[][] array)
    {
       //If rowsSum, columnsSum, and diagonalsSum are all magicSums
       if(rowsAreAllEqualToMagicSum(array) && columnsAreAllEqualToMagicSum(array) && diagonalsAreBothEqualToMagicSum(array))
           return true;   //It is a magic square.
       return false;       //If not it is not a magic square.
    }
    public static boolean isNormalMagicSquare(int[][] array)
    {
       return false;   //What do you mean by this...???
    }
    public static int getSumOfDownDiagonal(int[][] array)
    {
       int sum = 0;
       for(int i = 0; i < array.length; i++)   //For the leading diagonal.
           sum += array[i][i];   //Calculate the sum of the diagonal.
       return sum;  
    }
    public static int getSumOfUpDiagonal(int[][] array)
    {
       int sum = 0;
       for(int i = 0; i < array.length; i++)   //For the trialing diagonal.
           sum += array[array.length-i-1][i];   //Calculate the sum of the diagonal.
       return sum;  
    }
    public static int getSumOfRow(int[][] array, int index)
    {
       int sum = 0;
       for(int i = 0; i < array.length; i++)   //For each column
           sum += array[index][i];   //Calculate the sum of that row.
       return sum;  
    }
    public static int[] getColumn(int[][] array, int index)
    {
       int[] temp = new int[array.length];
       for(int i = 0; i < array.length; i++)
           temp[i] = array[i][index];
       return temp;  
    }
    public static int getSumOfColumn(int[] array)
    {
       int sum = 0;
       for(int i = 0; i < array.length; i++)   //For each value in the array.
           sum += array[i];   //Calculate the sum of that column.
       return sum;  
    }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote