Use Java to write a program that prompts the user for a positive odd integer , generates a normal magic square of order size of that integer, and prints it with numbers right-justified and aligned in evenly-spaced columns. (Magic square definition: https://en.wikipedia.org/wiki/Magic_square. Can assume that the numbers in the square are 3 digits or less (i.e. size ≤ 31). Have the construction of the 2D integer array magic square take place in a public static method getMagicSquare that takes in an integer n as a parameter and returns the 2D integer array magic square as specified. Your main method should perform input and output (I/O) and call this method to perform the computation. Methods for generating a magic square : Assign 1 to our initial current position: the bottom row, middle column. Place each successive value (up to size * size) in the first of the following positions that is unoccupied: (1) one space down and to the right, or (2) one space up. Positions wrap around the sides, top and bottom, as shown in the following example transcripts (user input underlined):
Use Java to write a program that prompts the user for a positive odd integer , generates a normal magic square of order size of that integer, and prints it with numbers right-justified and aligned in evenly-spaced columns. (Magic square definition: https://en.wikipedia.org/wiki/Magic_square. Can assume that the numbers in the square are 3 digits or less (i.e. size ≤ 31). Have the construction of the 2D integer array magic square take place in a public static method getMagicSquare that takes in an integer n as a parameter and returns the 2D integer array magic square as specified. Your main method should perform input and output (I/O) and call this method to perform the computation.
Methods for generating a magic square : Assign 1 to our initial current position: the bottom row, middle column. Place each successive value (up to size * size) in the first of the following positions that is unoccupied: (1) one space down and to the right, or (2) one space up. Positions wrap around the sides, top and bottom, as shown in the following example transcripts (user input underlined):
Step by step
Solved in 3 steps with 2 images