I’m not so good at java programming, please I don’t understand coding at all it’
ID: 3796692 • Letter: I
Question
I’m not so good at java programming, please I don’t understand coding at all it’s a class I have to take***
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.
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.
Assignment Guidelines:
1. Run your programs with enough test cases to prove that the
program works.
Hand in the output for each test run.
2. For each source file include a comment that contains your name,
assignment number and problem number.
3. Hand in a source code listing (
print out of your Java source
files) and the output produced by your program. Use Courier New
font.
4. Proper indentation.
5. Use Java Language Coding Guidelines (Appendix L) for variable
names, method names, class names and constant names
Explanation / Answer
Gagan Goel
Q1 Matrix Square
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
class MagicSquare {
static int[][] contructMagicSquare(int n) {
int A[][] = new int[n][n]; // Creating the Magic Matrix
int i, j, k, t;
/* Initializing every cell of the matrix with 0 */
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
A[i][j] = 0;
}
}
/* When the size of the matrix is Odd */
if (n % 2 != 0) {
i = 0;
j = n / 2;
k = 1;
while (k <= n * n) {
A[i][j] = k++;
i--; // Making one step upward
j++; // Moving one step to the right
if (i < 0 && j > n - 1) // Condition for the top-right corner
// element
{
i = i + 2;
j--;
}
if (i < 0) // Wrapping around the row if it goes out of boundary
i = n - 1;
if (j > n - 1) // Wrapping around the column if it goes out of
// boundary
j = 0;
if (A[i][j] > 0) // Condition when the cell is already filled
{
i = i + 2;
j--;
}
}
return A;
} else {
return A = null;
}
}
}
public class MagicSquareMain {
public static void main(String[] args) {
// TODO Auto-generated method stub
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the size of the matrix : ");
int n = 0;
try {
n = Integer.parseInt(br.readLine());
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// MagicSquare.contructMagicSquare(n);
int B[][] = new int[n][n]; // Creating the Magic Matrix
B = MagicSquare.contructMagicSquare(n);
if(B!=null){
// Printing the Magic matrix
System.out.println("The Magic Matrix of size " + n + "x" + n + " is:");
System.out.println();
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print(B[i][j] + " ");
}
System.out.println();
}
}else{
System.out.println("Even number Magic Matrix is : " + null);
}
}
}
Output :
Enter the size of the matrix : 3
The Magic Matrix of size 3x3 is:
8 1 6
3 5 7
4 9 2
Enter the size of the matrix : 5
The Magic Matrix of size 5x5 is:
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
Enter the size of the matrix : 7
The Magic Matrix of size 7x7 is:
30 39 48 1 10 19 28
38 47 7 9 18 27 29
46 6 8 17 26 35 37
5 14 16 25 34 36 45
13 15 24 33 42 44 4
21 23 32 41 43 3 12
22 31 40 49 2 11 20
Enter the size of the matrix : 8
Even number Magic Matrix is : null
Q2 Average
class Average {
public static double[][] makeNeighborAverageMatrix(int[][] values) {
int[][] temps = values;
for (int i = 0; i < temps.length; i++) {
int sum = 0;
for (int g = 0; g < temps[i].length; g++) {
sum += temps[i][g];
System.out.print(temps[i][g] + " ");
}
System.out.println(sum / temps[i].length);
System.out.println();
}
return null;
}
}
public class Average2dArray {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[][] arr = { { 27, 28, 26, 29, 30 }, { 26, 25, 25, 37, 40 } };
Average.makeNeighborAverageMatrix(arr);
}
}
OUtPUT :
27 28 26 29 30 28
26 25 25 37 40 30
Note : Your Second Question is not clear so much .Explain Again ?
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.