Question: Below in bold is my current java code. I need help with the outputs that will be provided below my code. if there is a

Below in bold is my current java code. I need help with the outputs that will be provided below my code. if there is a repeated number in the users input then it should print "The input cannot be a magic square! There must be one of each value from 1 to (number in context of magic square.). if there is not a repeated number and the numbers just do not work, then it should print out a different message that will be seen below my code. Thank You!

Scanner scnr = new Scanner(System.in);

// Get the size of the magic square from the user

System.out.println("Enter in the magic square size you would like:");

int size = scnr.nextInt();

// Calculate the magic number from the magic square size

int magicNumber = (size * (size * size + 1)) / 2;

System.out.println("The Magic number for size "+ size + " is " + magicNumber + ".");

// Get the values to go into the magic square from the user

int[] values = new int[size * size];

System.out.println("Enter in the values:");

for (int i = 0; i < values.length; i++) {

values[i] = scnr.nextInt();

}

// Display the entered values as a magic square

System.out.println(" You entered: ");

for (int i = 0; i < values.length; i++) {

System.out.print(values[i] + " ");

if ((i + 1) % size == 0) {

System.out.println();

}

}

System.out.println("Analyzing..."

// Analyze the magic square

boolean isMagicSquare = true;

int diagonalSum1 = 0;

int diagonalSum2 = 0;

for (int i = 0; i < size; i++) {

int rowSum = 0;

int colSum = 0;

for (int j = 0; j < size; j++) {

int value = values[i * size + j];

rowSum += value;

colSum += values[j * size + i];

if (i == j) {

diagonalSum1 += value;

}

if (i + j == size - 1) {

diagonalSum2 += value;

}

}

if (rowSum != magicNumber || colSum != magicNumber) {

isMagicSquare = false;

break;

}

}

if (diagonalSum1 != magicNumber || diagonalSum2 != magicNumber) {

isMagicSquare = false;

}

if (isMagicSquare) {

System.out.println(" This is a magic square!");

} else {

System.out.println(" The input cannot be a magic square!");

}

// Check if the inputted list of numbers can even be a valid magic square

if (!compareArrays(values, size)) {

System.out.println("The inputted list of numbers cannot be a valid magic square matrix");

return;

}

// Check if the sum of all rows, columns, and diagonals are equal to the magic

// number

int[][] magicSquare = new int[size][size];

for (int i = 0; i < size; i++) {

for (int j = 0; j < size; j++) {

magicSquare[i][j] = values[i * size + j];

}

}

if (!checkRows(magicSquare, magicNumber) || !checkColumns(magicSquare, magicNumber)

|| !checkDiagonals(magicSquare, magicNumber)) {

//Printing the wrong rows by calling wrongRows method

wrongRows(magicSquare,magicNumber);

//Printing the wrong columns by calling wrongColumns method

wrongCols(magicSquare,magicNumber);

System.out.println("The inputted list of numbers is not a magic square matrix");

return;

}

}

/**

* Compares the given array of values to the size of the magic square to

* determine

* if it can be a valid magic square. Returns true if it is valid, false

* otherwise.

*/

private static boolean compareArrays(int[] values, int size) {

// Check that the array has the correct length

if (values.length != size * size) {

return false;

}

// Check that the array contains each integer from 1 to (size^2 - 1) exactly

// once

int[] sortedValues = values.clone();

Arrays.sort(sortedValues);

for (int i = 0; i < sortedValues.length; i++) {

if (sortedValues[i] != i + 1) {

return false;

}

}

return true;

}

/**

* Checks if the sum of each row in the given magic square is equal to the given

* magic number. Returns true if all rows are valid, false otherwise.

*/

private static boolean checkRows(int[][] magicSquare, int magicNumber) {

for (int i = 0; i < magicSquare.length; i++) {

int sum = 0;

int j;

for (j = 0; j < magicSquare[i].length; j++) {

sum += magicSquare[i][j];

}

if (sum != magicNumber) {

return false;

}

}

return true;

}

/**

* Checks if the sum of each column in the given magic square is equal to the

* given

* magic number. Returns true if all columns are valid, false otherwise.

*/

private static boolean checkColumns(int[][] magicSquare, int magicNumber) {

for (int i = 0; i < magicSquare.length; i++) {

int sum = 0;

int j;

for ( j= 0; j < magicSquare[i].length; j++) {

sum += magicSquare[j][i];

}

if (sum != magicNumber) {

return false;

}

}

return true;

}

/**

* Checks if the sum of each diagonal in the given magic square is equal to the

* given

* magic number. Returns true if all diagonals are valid, false otherwise.

*/

private static boolean checkDiagonals(int[][] magicSquare, int magicNumber) {

int sum = 0;

for (int i = 0; i < magicSquare.length; i++) {

sum += magicSquare[i][i];

}

if (sum != magicNumber) {

return false;

}

sum = 0;

for (int i = 0; i < magicSquare.length; i++) {

sum += magicSquare[i][magicSquare.length - i - 1];

}

if (sum != magicNumber) {

return false;

}

return true;

}

//If is not a magic sqaure matrix then proform this method to print the wrong rows and it elements

private static void wrongRows(int[][] magicSquare, int magicNumber) {

int i=0;

for ( ; i < magicSquare.length; i++) {

int sum = 0;

int j=0;

for ( ; j < magicSquare[i].length; j++) {

sum += magicSquare[i][j];

}

if (sum != magicNumber) {

System.out.println("Row +"+ (i)+" doesn't work");

System.out.println("These are the values in row "+i+" : ");

for(int k=0;k

{

System.out.println(magicSquare[i][k]);

}

}

}

return;

}

//If is not a magic sqaure matrix then proform this method to print the wrong columns and its elements

private static void wrongCols(int[][] magicSquare, int magicNumber) {

for (int i = 0; i < magicSquare.length; i++) {

int sum = 0;

int j;

for ( j= 0; j < magicSquare[i].length; j++) {

sum += magicSquare[j][i];

}

if (sum != magicNumber) {

System.out.println("Column "+ i+" doesn't work");

System.out.println("These are the values in column "+i+" : ");

for(int k=0;k

{

System.out.println(magicSquare[k][i]);

}

}

}

return;

}

}

Output 4:

Enter in the magic square size you would like:

5

The magic number for size 5 is 65.

Enter in the values:

17 2 23 8 15 24 7 14 16 1 5 21 19 3 17 6 12 18 13 16 13 22 4 20 5

You entered:

17 2 23 8 15

24 7 14 16 1

5 21 19 3 17

6 12 18 13 16

13 22 4 20 5

Analyzing...

The input cannot be a magic square! There must be one of each value from 1 to 25.

Row 1 does not work! These are the values in row 1: 24 7 14 16 1

Row 4 does not work! These are the values in row 4: 13 22 4 20 5

Column 1 does not work! These are the values in column 1: 2 7 21 12 22

Column 2 does not work! These are the values in column 2: 23 14 19 18 4

Column 3 does not work! These are the values in column 3: 8 16 3 13 20

Column 4 does not work! These are the values in column 4: 15 1 17 16 5

Diagonal 1 does not work! These are the values in diagonal 1: 17 7 19 13 5

Diagonal 2 does not work! These are the values in diagonal 2: 15 16 19 12 13

This is not a magic square!

Output 5:

Enter in the magic square size you would like:

4

The magic number for size 4 is 34.

Enter in the values:

16 1 7 10 8 11 6 9 5 14 2 13 12 3 15 4

You entered:

16 1 7 10

8 11 6 9

5 14 2 13

12 3 15 4

Analyzing...

Column 0 does not work! These are the values in column 0: 16 8 5 12

Column 1 does not work! These are the values in column 1: 1 11 14 3

Column 2 does not work! These are the values in column 2: 7 6 2 15

Column 3 does not work! These are the values in column 3: 10 9 13 4

Diagonal 1 does not work! These are the values in diagonal 1: 16 11 2 4

Diagonal 2 does not work! These are the values in diagonal 2: 10 6 14 12

This is not a magic square!

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!