Part 2 Programming 20 points Your assignment is to design a

Part 2: Programming: 20 points) Your assignment is to design a class called MagicSquares.A magic square is an n by n matrix that is filled with the numbers 1, 2, 3, 4 n square if the sum of the elements in each row, in each column, and in the two diagonals is the same value. Your class must have two instance variables: a 2-D array to hold the integers and an integer size which is the size of your square (for example n). Use the following UML diagram to design the class: Ma Square I int size int Magicsquares Magicsquares (int leftRightDiagonalsum int right Left Diagonal Sum int int coulmn Sum row sum int correct Numbers boolean validMagicsquare boolean Strin to Strin Program requirements and/or constraints: You will need to create two files Magicsquares,java file, and ignment8 java file which has the Ass main method. has two constructors. The first constructor Magicsquares reads the input from a file Magicsquares

Solution

1. MagicSquare.java:

package magic;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

public class MagicSquares {
   int square[][];
   int size;

   public MagicSquares() throws IOException {
       File file1 = new File(\"nums.txt\");
       FileInputStream in = new FileInputStream(file1);
       BufferedReader br = new BufferedReader(new InputStreamReader(in));
       String strLine;
       if ((strLine = br.readLine()) != null) {
           size = Integer.parseInt(strLine);
           square = new int[size][size];
       }
       for (int i = 0; i < size; i++) {
           for (int j = 0; j < size; j++) {
               strLine = br.readLine();
               int n = Integer.parseInt(strLine);
               if (correctNumbers(n)) {
                   square[i][j] = n;
               } else{
                   System.out.println(\"invalid number : \" + n);
                   System.exit(0);
               }
           }
       }
   }

   public MagicSquares(int[] arr) {
       size = (int) Math.sqrt(arr.length);
       square = new int[size][size];
       int k = 0;
       for (int i = 0; i < size; i++) {
           for (int j = 0; j < size; j++) {
               int n = arr[k];
               if (correctNumbers(n)) {
                   square[i][j] = n;
                   k++;
               } else{
                   System.out.println(\"invalid number : \" + n);
                   System.exit(0);
               }
           }
       }
   }

   int leftRightDiagonalSum() {
       int sum = 0;
       for (int i = 0; i < size; i++) {
           sum = sum + square[i][i];
       }
       return sum;
   }

   int rightLeftDiagonalSum() {
       int sum = 0;
       for (int i = size - 1; i >= 0; i--) {
           sum = sum + square[i][i];
       }
       return sum;
   }

   int columnSum(int no) {
       int sum = 0;
       for (int i = 0; i < size; i++) {
           sum = sum + square[i][no];
       }
       return sum;
   }

   int rowSum(int no) {
       int sum = 0;
       for (int i = 0; i < size; i++) {
           sum = sum + square[no][i];
       }
       return sum;
   }

   boolean correctNumbers(int num) {
       if (num >= 1 && num <= size * size)
           return true;
       else
           return false;
   }

   boolean validMagicSquare() {
       int ldSum = leftRightDiagonalSum(), rdSum = rightLeftDiagonalSum();
       if (ldSum == rdSum) {
           for (int i = 0; i < size; i++) {
               int cSum = columnSum(i);
               if (cSum != ldSum) {
                   return false;
               }
           }
           for (int i = 0; i < size; i++) {
               int rSum = rowSum(i);
               if (rSum != ldSum) {
                   return false;
               }
           }
           return true;
       } else
           return false;
   }

   @Override
   public String toString() {
       StringBuilder sb = new StringBuilder();
       for (int i = 0; i < size; i++) {
           for (int j = 0; j < size; j++) {
               sb.append(square[i][j] + \"\\t\");
               // System.out.print(square[i][j]);
           }
           sb.append(\"\ \");
       }
       return sb.toString();
   }

}

--------------------------------------------------------

2. Assignment8.java

package magic;

import java.io.IOException;
import java.util.Scanner;

public class Assignment8 {

   /**
   * @param args
   * @throws IOException
   */
   public static void main(String[] args) throws IOException {
       System.out.println(\"----Menu----\");
       System.out
               .println(\"1. Create a magic square from a file & check if it is a magic square\");
       System.out
               .println(\"2. Create a magic square by passing an array & check if it is a magic square\");
       System.out.println(\"3. Exit the program\");

       Scanner sc = new Scanner(System.in);
       int choice = sc.nextInt();

       if (choice == 1) {
           MagicSquares ms = new MagicSquares();
           if (ms.validMagicSquare()) {
               System.out.println(\"It\'s a magic square ! \");
               System.out.println(ms.toString());
           } else {
               System.out.println(\"It\'s not a magic square ! \");
           }
       } else if (choice == 2) {
           System.out.println(\"please enter the size of the square: \");
           Scanner sc2 = new Scanner(System.in);
           int n = sc2.nextInt();
           int sizeOfArray = n * n;
           System.out.println(\"please enter \" + sizeOfArray + \" integers: \");
           int arr[] = new int[sizeOfArray];
           for (int i = 0; i < sizeOfArray; i++) {
               sc2 = new Scanner(System.in);
               arr[i] = sc2.nextInt();
           }
           MagicSquares ms = new MagicSquares(arr);
           if (ms.validMagicSquare()) {
               System.out.println(\"It\'s a magic square ! \");
               System.out.println(ms.toString());
           } else {
               System.out.println(\"It\'s not a magic square ! \");
           }
       } else {
           return;
       }
   }

}

-----------------------------------------------

output:

----Menu----
1. Create a magic square from a file & check if it is a magic square
2. Create a magic square by passing an array & check if it is a magic square
3. Exit the program
1
It\'s a magic square !
11   18   25   2   9  
10   12   19   21   3  
4   6   13   20   22  
23   5   7   14   16  
17   24   1   8   15  

------------------------------------------

----Menu----
1. Create a magic square from a file & check if it is a magic square
2. Create a magic square by passing an array & check if it is a magic square
3. Exit the program
2
please enter the size of the square:
3
please enter 9 integers:
1
2
3
4
5
6
7
8
9
It\'s not a magic square !

 Part 2: Programming: 20 points) Your assignment is to design a class called MagicSquares.A magic square is an n by n matrix that is filled with the numbers 1,
 Part 2: Programming: 20 points) Your assignment is to design a class called MagicSquares.A magic square is an n by n matrix that is filled with the numbers 1,
 Part 2: Programming: 20 points) Your assignment is to design a class called MagicSquares.A magic square is an n by n matrix that is filled with the numbers 1,
 Part 2: Programming: 20 points) Your assignment is to design a class called MagicSquares.A magic square is an n by n matrix that is filled with the numbers 1,

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site