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

An n × n matrix that is filled with the numbers 1, 2, 3, … , n2 is a magic squar

ID: 3635061 • Letter: A

Question

An n × 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.
For example,

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

You are to write programs that reads in n2 values from the keyboard and tests whether they form a magic square when arranged as a square matrix.

Program Organization Requirements:

Functional Class: Square.java.

-Declare an ArrayList attribute, numbers, to store user input data.

-Declare a two-dimensional int array attribute, square, to put user input into a
square.

-Create a method
public void add(int i) to add a number to the ArrayList numbers.

-Create a method
public booleaqn isMagic() to determine if the square is a magic square. It returns true if a magic square, false otherwise.

-This method should tests
1. Did the user enter n2 numbers for some n? (Eg., 4, 9, 16, 25, …)
2. Do each of the numbers 1, 2, … , n2 occur exactly once in the user input?
3. When the numbers are put into a square, are the sum of the rows, columns, and diagonals equal to each other?

If the size of the input is a square, test whether all numbers between 1 and n2 are present. Then compute the row, column, and diagonal sums to test for a magic square.

-You can add more attributes and/or methods as you need.
-You cannot use Scanner, JOptionPane and System.out,print(in) in this functional class


Test Class: TestMagicSquare.java(This class would have the main method in it)

-Prompt user for a sequence of integers.
-Use Scanner or JOptionPane class.
-Validate user input for int values.
-Use the add(i) method of the Square to add each number to the ArrayList.
-Check if the user input makes a square. If not, stop the program.
-If the numbers make a square, test for uniqueness. If there are any duplicate numbers, stop the program.
-Then call isMagic() method if it is a magic square.

~~~~Sample Output Runs~~~~~~

Enter an integer(x to exit): 2
Enter an integer(x to exit): 3
Enter an integer(x to exit): 4
Enter an integer(x to exit): r
***INVALID DATA ENTRY***
Enter an integer(x to exit):5
Enter an integer(x to exit):d
***INVALID DATA ENTRY***
Enter an integer(x to exit):6
Enter an integer(x to exit):x

Step 1. Numbers do not make a square: Program Stopped
Press any key continue.....


Enter an integer(x to exit): 1
Enter an integer(x to exit): 2
Enter an integer(x to exit): 3
Enter an integer(x to exit): 4
Enter an integer(x to exit): 5
Enter an integer(x to exit): 6
Enter an integer(x to exit): 7
Enter an integer(x to exit): 8
Enter an integer(x to exit): 8
Enter an integer(x to exit): x

Step 1. Numbers make a square ***
Step 2. Numbers are not unique: Program Stopped
Press any key to continue.....




Enter an integer(x to exit): 1
Enter an integer(x to exit): 2
Enter an integer(x to exit): 3
Enter an integer(x to exit): 4
Enter an integer(x to exit): 5
Enter an integer(x to exit): 6
Enter an integer(x to exit): 7
Enter an integer(x to exit): 8
Enter an integer(x to exit): 9
Enter an integer(x to exit): x

Step 1. Numbers make a square ***
Step 2. Numbers are unique ***
Step 3. But it is NOT a magic square. ***
Press any key to continue....



Enter an integer(x to exit): 16
Enter an integer(x to exit): 3
Enter an integer(x to exit): 2
Enter an integer(x to exit): 13
Enter an integer(x to exit): 5
Enter an integer(x to exit): 10
Enter an integer(x to exit): 11
Enter an integer(x to exit): 8
Enter an integer(x to exit): 9
Enter an integer(x to exit): 6
Enter an integer(x to exit): 7
Enter an integer(x to exit): 12
Enter an integer(x to exit): 4
Enter an integer(x to exit): 15
Enter an integer(x to exit): 14
Enter an integer(x to exit): 1
Enter an integer(x to exit): x

Step 1. Number make a square ***
Step 2. Number are unique ***
Step 3. Yes, it is a MAGIC SQUARE! ***
Press any key to continue....

Explanation / Answer

http://www.cplusplus.com/forum/beginner/1621/

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