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

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

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

}

Problem Statement: Create a program for generating an n by n Magic Square where n is an odd integer by completing the code presented below. class name: MagicSquare A Magic Square is an n by n array of unique integers where the sum of the elements in rows, columns and diagonals are equal. Furthermore, the integers in the array are the integers from 1 to n*n. Examples of Magic Squares: 3 by 3 Magic Square (The integers from 1 to 9 are distributed in a 3 by 3 array such that the sum of every row, column and diagonal is 15) 5 by 5 Magic Square (The integers from 1 to 25 are distributed in a 5 by 5 array such that the sum of every row, column and diagonal is 65) 8 8 1 5 9 6 7 24 5 3 4 17 23 4 10 11 14 20 2 1 7 13 19 25 6 15 16 22 3 9 12 21 18 2 *

/** * 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

Intranet And Web Databases For Dummies

Authors: Paul Litwin

1st Edition

0764502212, 9780764502217

More Books

Students also viewed these Databases questions

Question

What attracts you about this role?

Answered: 1 week ago

Question

How many states in India?

Answered: 1 week ago

Question

HOW IS MARKETING CHANGING WITH ARTIFITIAL INTELIGENCE

Answered: 1 week ago

Question

Different types of Grading?

Answered: 1 week ago

Question

Why is the System Build Process an iterative process?

Answered: 1 week ago