Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Magic squares. an n x n matrix that is filled with the numbers 1,2,3...,n2 is a

ID: 3621988 • Letter: M

Question

Magic squares. an n x n matrix that is filled with the numbers 1,2,3...,n2 is a magic square if the sum of the elements in each row, in each column, and in the two diagonals is the same value.
Write a program that reads n2 values from the keyboard and tests wether they form a magic square when arranged as a square matrix. You need to test three features:
Did each user enter n2 numbers for some n?
Do each of the numbers 1,2,...,n2 occur exactly once in the user input?
When the numbers are put into a square, are the sums if the rows, columns, and diagonals equal to each other?

if the size of the input is a square, test wether all numbers between 1 and n2 are present. Then conpute the row, column, and diagonal sums. Implement a class Square with methods: public void add(int i) and public boolean isMagic()

not really sure how to do this problem your help is great.

Explanation / Answer

import java.io.*; import java.util.*; public class Square { private int nextCellRow; private int nextCellCol; private int[][] data; private int squareSize; public Square(int squareSize) { data = new int[squareSize][squareSize]; nextCellRow=0; nextCellCol=0; this.squareSize = squareSize; } /* * Enter a number in the next empty cell in the square */ public void add(int i) { data[nextCellRow][nextCellCol] = i; nextCellCol += 1; if (nextCellCol == squareSize) { nextCellCol = 0; nextCellRow += 1; } } public boolean isMagic() { List numbers = new ArrayList(); for (int i = 0; i
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote