At the beginning of the program, briefly describe to the user what a Magic Squar
ID: 3571420 • Letter: A
Question
At the beginning of the program, briefly describe to the user what a Magic Square Matrix is (described further on), and then allow them to enter “start” to begin the search. The goal of this program is to output a matrix with all rows, columns and diagonals having the same sum – this is called the Magic Square Matrix. The maximum digit in the matrix should not exceed 9 and all numbers in the matrix are UNIQUE (meaning that each number does not appear more than once in the matrix). You will keep generating matrices and outputting whether it is a Magic Square Matrix or not. Once you output a Magic Square Matrix, the program will exit. You will be using a two-dimensional Array for this program. Example: User Enters: start After displaying many 3x3 matrices, a Magic Square is finally found: Because: -3 rows and 3 columns and 3 is not even. -Rows: 2+7+6=15, 9+5+1=15, 4+3+8=15 -Columns: 2+9+4=15, 7+5+3=15, 6+1+8=15 -Diagonals: 2+5+8=15,6+5+4=15 -All the numbers in the matrix are from :13^2 or (9); therefore, it doesn’t exceed 9 You must have the following methods created and used within your program: 1. sumRows(int [][]array) The method will calculate the sum of the rows in your generated matrix. Return the sum of rows if they are all equal else return -1 if they are not equal. Hint: sum of all elements/number of rows= sum of rows. If all row sums are equal. 2. sumColumns(int [][]array)The method will calculate the sum of the columns in your generated matrix. Return the sum of columns if they are all equal else return -1 if they are not equal. 3. sumDiagonals(int [][]array) The method will calculate the sum of the diagonals in your generated matrix and will return the sum of the diagonals if they are all equal, else return -1 if they are not equal. Hint: Diagonals indexes are: - Diagonal 1: [0][0],[1][1], [2][2] - Diagonal 2: [size-1][0], [size-2][1] [0][2] 4. isUnique(int [][] array, int num) Checks if num exists in the array. If num exists in the array, return false. If it does not exist in the array, return true. 5. displayMagicSquare(int [][] array) Prints the two-dimensional Array. 6. fillMatrix(int [][] array) Fills the two dimensional Array with Unique random numbers from 1 9 And returns the array. 7. isMagicSquare(int [][]array) returns true only if: - The array has sum of its rows equal to its columns and equal to its diagonals -Rows, columns, and diagonals’ sums are above 0.
Explanation / Answer
package magicsquare;
/**
* FileName:Assignment8.java
*/
import java.util.Arrays;
/**
* @author
*
* Driver program for checking magic square
*/
public class Assignment8 {
public static void main(String [] args){
MagicSquares ms = new MagicSquares();
System.out.println(ms.validMagicSquare());
System.out.println(ms);
}
}
package magicsquare;
/**
* FileName : MagicSquares.java
*/
import java.io.*;
import java.util.*;
/**
*@author ""
*
*/
public class MagicSquares {
private int[][] square;
private int size;
/**
* This default constructor which initialize the square and size
* Reading input from file named as num.txt
*
* */
public MagicSquares() {
try {
BufferedReader br = new BufferedReader(new FileReader("num.txt"));
//read array dimesion from first line
this.size=Integer.parseInt(br.readLine());
square= new int[this.size][this.size];
//read next line and split by space and convert io integer beacuse split method return list of string
String[] numArray = br.readLine().split(" ");
int count=0;
for(int i=0;i<this.size;i++){
for(int j=0;j<this.size;j++){
this.square[i][j]=Integer.parseInt(numArray[count++]);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* This Parametrized constructor which initialize the square and size
*
* */
public MagicSquares(int[] arr) {
//take the squareroot(lenth of arr) eg. if lenh of array is 25 then size=squareroot(25)=5
this.size=(int)Math.sqrt(arr.length);
int count=0;
for(int i=0;i<this.size;i++){
for(int j=0;j<this.size;j++){
this.square[i][j]=arr[count++];
}
}
}
/**
* calculating sum diagonally from left to right
*/
private int leftRightDiagonalSum() {
int sum =0;
for(int i=0;i<this.size;i++){
for(int j=0;j<this.size;j++){
if(i==j){
sum+=this.square[i][j];
}
}
}
return sum;
}
/**
* calculating sum diagonally from right to left
*/
private int rightLeftDiagonalSum() {
int sum =0;
for(int j=this.size;j>=0;j--){
for(int i=0;i<this.size;i++){
if(i+j==this.size-1){
sum+=this.square[j][i];
}
}
}
return sum;
}
private int rowSum() {
int []sum=new int[this.size];
for(int i=0;i<this.size;i++){
for(int j=0;j<this.size;j++){
sum[i]+=this.square[i][j];
}
}
for(int i=0;i<this.size;i++){
if(sum[i]!=sum[0]){
return -1;
}
}
return sum[0];
}
private int columnSum() {
int []sum=new int[this.size];
for(int i=0;i<this.size;i++){
for(int j=0;j<this.size;j++){
sum[i]+=this.square[j][i];
}
}
for(int i=0;i<this.size;i++){
if(sum[i]!=sum[0]){
return -1;
}
}
return sum[0];
}
private boolean correctNumbers() {
boolean flag=false;
for(int i=0;i<this.size;i++){
for(int j=0;j<this.size;j++){
if(square[i][j]>this.size*this.size)
return false;
}
}
Set<Integer> unique = new HashSet<Integer>();
for (int i = 0; i < this.size; i++) {
for (int j = 0; j < this.size; j++) {
unique.add(square[i][j]);
}
}
if (unique.size()<this.size*this.size){
return false;
}
return true;
}
public boolean validMagicSquare() {
boolean flag=true;
List<Integer> chekEquality = new ArrayList<>();
chekEquality.add(leftRightDiagonalSum());
chekEquality.add(rightLeftDiagonalSum());
chekEquality.add(rowSum());
chekEquality.add(columnSum());
int value = chekEquality.get(0);
for(int x:chekEquality){
if(x!=value){
return false;
}
}
if(correctNumbers()&& flag==true) {
flag = true;
}
else {
flag=false;
}
return flag;
}
public String toString() {
String s="";
for (int i = 0; i < this.size; i++) {
for (int j = 0; j < this.size; j++) {
s+=square[i][j]+" ";
}
s+=" ";
}
return s;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.