*****Help, not good at java programiing!! Q1. The program contructs and prints a
ID: 3808114 • Letter: #
Question
*****Help, not good at java programiing!!
Q1.
The program contructs and prints a Magic Square. Your program should implement and use the following method.
/** Constructs a magic square of size n.
Parameters:
n magic square size. n must be an odd integer.
Returns:
When n is odd returns a magic square.
When n is even returns null.
**/
private static int[] [] contructMagicSquare(int n)
Test your program with the following magic square sizes: 3, 5, 7, and 8.
Q2. Implement the method given in the problem. Also implement the following method:
/**
Makes a 2D array (matrix) where the [i][j] element contains the
* average of neighbors of [i][j] element in the input 2D array (values).
* for example:
* matrix[i][j] = neighborAverage(values, i, j ); *
* @param values 2D input array
* @return neighbor average matrix
*/
static double[][] makeNeighborAverageMatrix(int[][] values)
The main method should call makeNeighborAverageMatrix and print the returned 2D array.
Explanation / Answer
PROGRAM CODE:
package simple;
public class MagicSquareAndAverageMatrix {
private static int[][] contructMagicSquare(int n)
{
int magicSquare[][] = new int[n][n];
int row = n-1;
int col = n/2;
magicSquare[row][col] = 1;for (int i = 2; i <= n*n; i++) {
if (magicSquare[(row + 1) % n][(col + 1) % n] == 0) {
row = (row + 1) % n;
col = (col + 1) % n;
}
else {
row = (row - 1 + n) % n;
// don't change col
}
magicSquare[row][col] = i;
}
return magicSquare;
}
static double[][] makeNeighborAverageMatrix(int[][] values)
{
double averageMatrix[][] = new double[values.length][values[0].length];
for(int i=0; i<values.length; i++)
{
for(int j=0; j<values[0].length; j++)
{
double counter = 0;
double value = 0;
if(i!=0)
{
value += values[i-1][j];
counter++;
}
if(i!=values[0].length-1)
{
value += values[i+1][j];
counter++;
}
if(j!=0)
{
value += values[i][j-1];
counter++;
}
if(j!=values[0].length-1)
{
value += values[i][j+1];
counter++;
}
averageMatrix[i][j] = value/counter;
}
}
return averageMatrix;
}
public static void printInt2DArray(int array[][])
{
for(int i=0; i<array.length; i++)
{
for(int j=0; j<array[0].length; j++)
{
System.out.print(array[i][j] + " ");
}
System.out.println();
}
System.out.println(" ");
}
public static void printDouble2DArray(double array[][])
{
for(int i=0; i<array.length; i++)
{
for(int j=0; j<array[0].length; j++)
{
System.out.printf("%.2f ", array[i][j] );
}
System.out.println();
}
System.out.println(" ");
}
public static void main(String[] args) {
System.out.println("Printing array with magic square 3: ");
printInt2DArray(contructMagicSquare(3));
System.out.println("Printing array with magic square 5: ");
printInt2DArray(contructMagicSquare(5));
System.out.println("Printing array with magic square 7: ");
printInt2DArray(contructMagicSquare(7));
System.out.println("Printing array with magic square 8: ");
printInt2DArray(contructMagicSquare(8));
int values[][] = new int[4][4];
for(int i=0; i<4; i++)
{
for(int j=0; j<4; j++)
{
values[i][j] = i*j;
}
}
System.out.println("Printing array with average matrix: ");
printDouble2DArray(makeNeighborAverageMatrix(values));
}
}
OUTPUT:
Printing array with magic square 3:
4 9 2
3 5 7
8 1 6
Printing array with magic square 5:
11 18 25 2 9
10 12 19 21 3
4 6 13 20 22
23 5 7 14 16
17 24 1 8 15
Printing array with magic square 7:
22 31 40 49 2 11 20
21 23 32 41 43 3 12
13 15 24 33 42 44 4
5 14 16 25 34 36 45
46 6 8 17 26 35 37
38 47 7 9 18 27 29
30 39 48 1 10 19 28
Printing array with magic square 8:
32 34 44 54 64 2 12 22
23 25 35 45 55 57 3 13
14 24 26 36 46 56 58 4
5 15 17 27 37 47 49 59
60 6 16 18 28 38 48 50
51 61 7 9 19 29 39 41
42 52 62 8 10 20 30 40
33 43 53 63 1 11 21 31
Printing array with average matrix:
0.00 0.33 0.67 1.50
0.33 1.00 2.00 2.67
0.67 2.00 4.00 5.33
1.50 2.67 5.33 6.00
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.