Advertisement
Guest User

MagicSquare1.java SOLUTION

a guest
Jan 22nd, 2020
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.16 KB | None | 0 0
  1. //(c) A+ Computer Science
  2. //www.apluscompsci.com
  3. //Name -
  4.  
  5. import java.util.Scanner;
  6. import static java.lang.System.*;
  7.  
  8. public class MagicSquare1
  9. {
  10.    private int[][] mat;
  11.  
  12.     //size the matrix and load in the numbers into the matrix
  13.     //write all nested loop code here in the constructor
  14.    public MagicSquare1(int size, int[] numbers)
  15.    {
  16.       mat = new int[size][size];
  17.    
  18.       int k = 0;
  19.       for( int i = 0; i < size; i++)
  20.       {
  21.          for( int j = 0; j < size; j++)
  22.          {
  23.             mat[i][j] = numbers[k];
  24.             k++;
  25.          }
  26.       }
  27.    }
  28.  
  29.    public boolean isMagicSquare()
  30.    {
  31.       int x = sumRow(0);
  32.       for(int i = 1; i < mat.length; i++)
  33.       {
  34.          if(sumRow(i)!=x)
  35.             return false;
  36.       }
  37.       for(int i = 1; i < mat[0].length; i++)
  38.       {
  39.          if(sumCol(i)!=x)
  40.             return false;
  41.       }
  42.       if(sumDownDiag()!=x)
  43.          return false;
  44.       if(sumUpDiag()!=x)
  45.          return false;
  46.       return true;
  47.    }
  48.  
  49.    public int sumRow( int r )
  50.    {
  51.       int sum = 0;
  52.       for( int v : mat[r] )
  53.       {
  54.          sum+= v;
  55.       }
  56.       return sum;
  57.    }
  58.  
  59.    public int sumCol( int c )
  60.    {
  61.       int sum = 0;
  62.       for( int i = 0; i < mat.length; i++ )
  63.       {
  64.          sum+= mat[i][c];
  65.       }
  66.       return sum;
  67.    }
  68.  
  69.    public int sumDownDiag()
  70.    {
  71.       int sum = 0;
  72.       for( int i = 0; i < mat.length; i++ )
  73.       {
  74.          sum+= mat[i][i];
  75.       }
  76.       return sum;
  77.    }
  78.  
  79.    public int sumUpDiag()
  80.    {
  81.       int sum = 0;
  82.       for( int i = 0; i < mat.length; i++ )
  83.       {
  84.          sum+= mat[mat.length-i-1][i];
  85.       }
  86.       return sum;
  87.    }
  88.  
  89.    public String toString()
  90.    {
  91.       String output="";
  92.       for( int[] row : mat )
  93.       {
  94.          for( int val : row)
  95.          {
  96.             output += val + " ";
  97.          }
  98.          output += "\n";
  99.       }
  100.       return output.trim();
  101.    }
  102.    
  103.    public String magicOrNot()
  104.    {
  105.       if( isMagicSquare() )
  106.          return "MAGIC SQUARE";
  107.       else
  108.          return "NOT A MAGIC SQUARE";
  109.    }
  110.    
  111.    public static void main(String[] args){
  112.       MagicSquare1 square = new MagicSquare1(3, new int[]{8, 1, 6, 3, 5, 7, 4, 9, 2});
  113.       System.out.println( square + "\n" + square.magicOrNot() );
  114.      
  115.       square = new MagicSquare1(3, new int[]{6, 1, 8, 7, 5, 3, 2, 9, 4});
  116.       System.out.println( square + "\n" + square.magicOrNot() );
  117.      
  118.       square = new MagicSquare1(3, new int[]{8, 3, 1, 3, 5, 7, 9, 4, 2});
  119.       System.out.println( square + "\n" + square.magicOrNot() );
  120.      
  121.       square = new MagicSquare1(4, new int[]{7, 12, 1, 14, 2, 13, 8, 11, 16, 3, 10, 5, 9, 6, 15, 4});
  122.       System.out.println( square + "\n" + square.magicOrNot() );
  123.      
  124.       square = new MagicSquare1(5, new int[]{17, 24, 1, 8, 15, 23, 5, 7, 14, 16, 4, 6, 13, 20, 22, 10, 12, 19, 21, 3, 11, 18, 25, 2, 9});
  125.       System.out.println( square + "\n" + square.magicOrNot() );
  126.      
  127.       square = new MagicSquare1(5, new int[]{17, 24, 1, 8, 5, 23, 5, 7, 14, 6, 4, 6, 13, 20, 2, 10, 12, 19, 21, 3, 11, 18, 25, 2, 9});
  128.       System.out.println( square + "\n" + square.magicOrNot() );      
  129.    }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement