Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Modifying The Code: Language: JAVA Source Code: public class MagicSquare { public void initializeArray(int[][] a) { for (int row = 0; row public int[][] generateMagicSquare(int

Modifying The Code:

Language: JAVA

Source Code:

public class MagicSquare {

public void initializeArray(int[][] a) { for (int row = 0; row

public int[][] generateMagicSquare(int size) { int[][] magicSquare = new int[size][_____________]; initializeArray(_______________________);

int element = __________; int row = _________; int col = ______________;

magicSquare[_________][___________] = ______________;

while (element

if (___________) // out of bounds above row = ______________; if (col > ______________) // out of bounds to the right row = ______________; col = ______________; } _______________________ = element; } return ______________________; }

public void run() throws Exception { BufferedReader kbd = new BufferedReader(new InputStreamReader(System.in)); int size = 0; System.out.println("Magic Square Generator"); System.out.print("Enter the desired size of the magic square: "); size = Integer.parseInt(kbd.readLine()); if (_____________________) // size is an even number System.out.println("The size must be an odd number."); else if (size

/** * Displays each element of a two dimensional on a space with * uniform width. * Given: Two-dimensional array * For row =0 to last valid row of the given two-dimensional array * For column = 0 to last valid column of a row * Print the element at the current row and column on a fixed width space * Print the enter character */ public void display(int[][] array) { for (int r = 0; r

}

Requirements:

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

*

/** * Program that generates and displays a 3 by 3, 5 by 5, 7 by 7,9 by 9, 11 by 11 * or 13 by 13 magic square. Although the program may be written such that a magic square with any odd number size is generated, the output screen may not be able * to contain the magic square. * General Algorithm ( Implemented in the run method. ) * 1. Accept the desired size of the magic square * 2. If the size entered is an even number, give a message saying the size * must be an odd number and repeat from step 1. * 3. If the size is less than 3 or the size is greater than 13, give a message saying the size must be an odd number from 3 to 13 and repeat from step 1 * 4. Pass the size to the method that generates a magic square so that a magic square with the desired size is generated and pass the magic * square to the method that displays the magic square.

* Initializes a two-dimensional array by putting a to every * cell of the array. * For row o to the last row of the two-dimensional array * For columna to last column of each row * let array[row][column] = 0 **/ * public void initializeArray(int[][] a) { for (int row = 0; row ( a.length; row++) { for (int col = 0; col *

* Processes: * 1. Instantiate the two dimensional array called magicSquare * 2. Initialize magicSquare such that every cell is filled with a by * giving magicSquare to the method for initializing a two-dimensional array. * 3. Let the number to be placed in the magicSquare be element and * let element be 1. * 4. Let row be a (start from row a) * 5. Let col be the middle column of the row (size/2) * 6. put element in the magicSquare at the current row and col * 7. while element is less than size * size * Let element element + 1 * Let row = row -1 (i.e. Move one cell up diagonally) * Let col = col + 1 * If rou size-1, go the the cell below the corner cell * row = row+2; col-1 * If row 50, go to the last valid row * row = size -1 * If col > size, go to the first column 0 * If magicSquare[row][col] is not a (there is already an element), go the cell below row = row +2 * col = col-1 * Put element in the cell indexed row and col * col * col * * **/ public int[][] generateMagicSquare(int size) { int[][] magicSquare = new int[size][ initializeArray ( ); ]; int element int row = int col magicSquare[ ][ while (element col ) // out of bounds to the right if (magicSquare[row][col] != 0) { row = col } element; } return } /** * 1. Accept the desired size of the magic square * 2. If the size entered is an even number, give a message saying the size * must be an odd number and repeat from step 1. * 3. If the size is less than 3 or the size is greater than 13, give a message * saying the size must be an odd number from 3 to 13 and repeat from step 1 * 4. Pass the size to the method that generates a magic square so that a magic square with the desired size is generated and pass the magic square to the method that displays the magic square. */ * public void run() throws Exception { BufferedReader kbd = new BufferedReader(new InputStreamReader (System.in)); int size = 0; System.out.println("Magic Square Generator"); System.out.print("Enter the desired size of the magic square: "); size Integer.parseInt(kbd.readLine()); if ( ) // size is an even number System.out.println("The size must be an odd number."); else if (size

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

7. Define cultural space.

Answered: 1 week ago

Question

8. Describe how cultural spaces are formed.

Answered: 1 week ago