Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.81 KB | None | 0 0
  1. import java.util.ArrayList;
  2.  
  3. /**
  4.    A magic square is an n x n matrix which, if filled with numbers,
  5.    the sum of the elements in each row, each column,
  6.    and the two diagonal is the same value.
  7. */
  8. public class Square
  9. {
  10.    /**
  11.       Construct a Square object.
  12.       @param input the list of numbers
  13.    */
  14.    public Square(ArrayList input)
  15.    {
  16.       size = (int) Math.sqrt(input.size());
  17.       square = new int[size][size];
  18.  
  19.       for (int i = 0; i < size ; i++)
  20.          for (int j = 0; j < size ; j++)
  21.             square[i][j] = input.get(i * size + j);
  22.    }
  23.  
  24.    /**
  25.       Display the contents of the square.
  26.       @param the ith row
  27.       @param the jth column
  28.       @return a string represenation of the square
  29.    */
  30.    public String toString(int i, int j)
  31.    {
  32.       String s = "   " + square[i][j];
  33.       return s.substring(s.length() - 3, s.length());
  34.    }
  35.  
  36.    /**
  37.       Search for a number in the square.
  38.       @param n the number to search for
  39.       @return if the number is found, false otherwise
  40.    */
  41.    public boolean found(int n)
  42.    {
  43.       for (int i = 0; i < size ; i++)
  44.       {
  45.          for (int j = 0; j < size ; j++)
  46.          {
  47.             if (square[i][j] == n)
  48.                return true;
  49.          }
  50.       }
  51.       return false;
  52.    }
  53.  
  54.    /**
  55.       Add the numbers in the square.
  56.       @param i the row/column to add
  57.       @param row determines if a row or column should be added
  58.       @return sum the sum of the row/column
  59.    */
  60.    public int add(int i, boolean row)
  61.    {
  62.       int sum = 0;
  63.  
  64.       for (int j = 0; j < size; j++)
  65.       {
  66.          if (row)
  67.             sum = sum + square[i][j];
  68.          else
  69.             sum = sum + square[j][i];
  70.       }
  71.       return sum;
  72.    }
  73.  
  74.    /**
  75.       Find the sum of the diagonal.
  76.       @param mainDiagonal determine if it is the main diagonal
  77.       @return sum the sum of the diagonal
  78.    */
  79.    public int diagonalSum(boolean mainDiagonal)
  80.    {
  81.       int sum = 0;
  82.       for (int i = 0; i < size; i++)
  83.       {
  84.          int j;
  85.          if (mainDiagonal)
  86.             j = i;
  87.          else
  88.             j = size - 1 - i;
  89.          sum = sum + square[i][j];
  90.       }
  91.       return sum;
  92.    }
  93.  
  94.    /**
  95.       Determine if the square is a magic square.
  96.       @return true if square is a magic square, false otherwise
  97.    */
  98.    public boolean isMagic()
  99.    {
  100.       for (int n = 1; n <= size * size; n++)
  101.       {
  102.          if (!found(n))
  103.             return false;
  104.       }
  105.  
  106.       int sum = diagonalSum(true);
  107.  
  108.       if (sum != diagonalSum(false))
  109.          return false;
  110.       for (int i = 0; i < size; i++)
  111.       {
  112.          if (sum != add(i, true))
  113.             return false;
  114.          if (sum != add(i, false))
  115.             return false;
  116.       }
  117.       return true;
  118.    }
  119.  
  120.    private int[][] square;
  121.    private int size;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement