Question
I need to create a 'magic square' java program. A magic square is an NxN matrix of numbers such that the values in each row,
I need to create a 'magic square' java program.
A magic square is an NxN matrix of numbers such that the values in each row, column and diagonal add up to the same number. Write a class called MagicSquare that determines whether a given square is a magic square.
MagicSquare will have the following static methods. The class will have two public methods:
displaySquare will accept a 2D array of integers and display the contents of the 2D square/array as shown above
. checkSquare will accept a 2D array of integers and return true if the 2D square is magic and false if it is not.
You will also write four private helper methods:
checkRow will take an integer identifying a row and will return the sum of the values in that row
checkColumn will take an integer identifying a column and will return the sum of the values in that column
checkDiag1 will return the sum of the values in the diagonal from the top-left to the bottom righ
t checkDiag2 will return the sum of the values in the diagonal from the bottom-left to the top-right
The txt file is:
Magic Square data 2 7 6 9 5 1 4 3 8 4 14 15 1 9 7 6 12 5 11 10 8 16 2 3 13 Non-Magic Square 2 3 4 8 1 7 10 5 2 13 14 15 1 9 7 6 12 5 11 10 8 16 2 3 4
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started