All pastes #1697752 Raw Edit

Square

public java v1 · immutable
#1697752 ·published 2009-12-02 04:19 UTC
rendered paste body
import java.util.Scanner;import java.util.ArrayList;/** * A class used to see if the numbers input can form a magic square. */public class Square{    /**     * A constructor for the class Square.     * @param aNumber The square's side length.     */    public Square(int aNumber)    {        n = aNumber;        n2 = n * n;        a = new int[n][n];        int rowNum = 0;        int colNum = 0;    }    /**     * A method which adds a number to the square.     * @param aNumber The number to add to the square.     */    public void add(int aNumber)    {        if ( colNum < n)         {            a[rowNum][colNum] =  aNumber;               // adding the number to the array            colNum++;        }                else                                                                      // reset column number and go to next row        {            rowNum += 1;            colNum = 0;            a[rowNum][colNum] = aNumber;                           colNum++;                }}        /**     * A method to test whether or not the square is a magic square or not. `     */    public boolean isMagic()    {                int check = (n * (n2 + 1)) / 2; // the magic number sum must be equal to.               for(int i = 0; i < n; i++)              // check rows            {                int sum = 0;                for(int j = 0; j < n; j++)                    sum += a[i][j];                if( sum != check) return false;            }                 for(int j = 0; j < n; j++)           // check columns            {                int sum = 0;                for(int i = 0; i < n; i++)                    sum += a[i][j];                if( sum != check) return false;            }                        int leftToRightSum = 0;           for(int i =0; i < n; i++)      //check diagonal that goes left to right            {                leftToRightSum += a[i][i];            }            if (leftToRightSum != check) return false;                        int rightToLeftSum = 0;            int c = n - 1;            for(int i = n - 1; i >= 0; i--)                     // check diagonal going right to left            {                rightToLeftSum += a[i][c];                c--;            }            if ( rightToLeftSum != check) return false;                  else return true;    }        /**     * A method to turn the entered values into a square.     */    public String toString()    {        String r = "";        for(int i = 0; i < n; i++)        {           for(int j = 0; j < n; j++)           {               r += a[i][j] + "\t";    //tab for allignment           }                      r += "\n";       }              return r;    }                   private int n = 0;   private int[][] a;   private int rowNum = 0;   private int colNum = 0;   private int n2 = 0;        public static void main(String[] args)    {        System.out.println("Rules: Enter a number n that is larger than 2 \n and enter all the values from 1 to n^2 ONLY ONCE in any order.");        ArrayList<Integer> inputs = new ArrayList<Integer>();                Scanner in = new Scanner(System.in);                System.out.print("Enter n: ");        int n = in.nextInt();        while( n <= 2)        {        System.out.print("N must be larger than 2.");        n = in.nextInt();        }                Square square = new Square(n);                System.out.println("Enter your numbers. Every " + (n + 1) + "th term will automatically create a new row.");        for (int k = 1; k <= n * n; k++)        {            int input = in.nextInt();            if(inputs.contains(input) == true)            {                System.out.println("Error: each number can be entered only once.");                throw new RuntimeException("Make sure you follow the directions!");            }                        else if ( input < 1 || input > (n * n))                {                    System.out.println("Error: only numbers 1 - " + (n * n) + " are allowed.");                    throw new RuntimeException("Make sure you follow the directions!");                }            else            {                inputs.add(input);                square.add(input);            }        }        System.out.println("This is your square: \n" + square.toString());        if (square.isMagic() == false)            System.out.println("This square is not magic.");                    else System.out.println("This square is magic.");    }}