Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hello, I had a chat session but we had difficulty with audio. Can someone please

ID: 3887516 • Letter: H

Question

Hello, I had a chat session but we had difficulty with audio. Can someone please comment out all the lines of code for this magic square program? Thank you.

mport java.io.*;
import java.util.*;

public class Magic
{
    static void generateSquare(int n, int ch)
    {
        int[][] magicSquare = new int[n][n];
        int i = n/2;
        int j = n-1;

        for (int num=1; num <= n*n; )
        {
            if (i==-1 && j==n)
            {
                j = n-2;
                i = 0;
            }
            else
            {
                if (j == n)
                   j = 0;
                if (i < 0)
                   i=n-1;
            }

            if (magicSquare[i][j] != 0)
            {
                j -= 2;
                i++;
                continue;
            }
            else
               magicSquare[i][j] = num++;

          j++;
          i--;
        }

        if (ch == 0) {
           for(i=0; i<n; i++)
           {
             
              for(j=0; j<n; j++)
                 System.out.print(magicSquare[i][j]+" ");
              System.out.println();
           }
        }
       
        if (ch == 1) {
           for(i=0; i<n; i++)
           {
              for (int k = 0; k<n; k++)
                  System.out.print("---");
              System.out.println();
              for(j=0; j<n; j++)
                 System.out.print(magicSquare[i][j]+"|");
              System.out.println();
           }
        }
      
    }

    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        int n = 0;
        do {
          System.out.println("Enter number of sides : ");
          n = sc.nextInt();
          if (n == 2)
             System.out.println("Not Possible");
          if (n == 6 || n == 10 || n==14)
             System.out.println("Don't press your luck");   
   
        } while (n == 2 || n== 6 || n==10 || n==14 || n < 1 || n > 17);
        System.out.println("with box-1 && without box-0 ");
        int choice = sc.nextInt();      
        generateSquare(n, choice);
     }
}

Explanation / Answer

Explanation of the code Description:

The code is about Magic square and A magic square is nothong but it is of order n is an arrangement of n power 2 numbers and also distinct integers in a square such that the n numbers in all rows and in all columns and both diagonals sum to the same constant. A magic square contains the integers from 1 to n power 2.

The comments to the code is added and it is given below:

Code:

import java.io.*; // importing io package
import java.util.*; // importing util package.
public class Magic //public class MAgig is created
{
static void generateSquare(int n, int ch) // generatesquare Method declared
{
int[][] magicSquare = new int[n][n]; //array declared
int i = n/2; // variable declared i
int j = n-1; // variable declared j
for (int num=1; num <= n*n; ) //for loop for looping all the elements
{
if (i==-1 && j==n) //if else block for checking conditions
{
j = n-2;
i = 0;
}
else
{
if (j == n)
j = 0;
if (i < 0)
i=n-1;
}
if (magicSquare[i][j] != 0) // another if else block for checking conditions.
{
j -= 2;
i++;
continue;
}
else
magicSquare[i][j] = num++;
j++;
i--;
}
if (ch == 0) {
for(i=0; i<n; i++) //for loop for printing magic square elements
{

for(j=0; j<n; j++)
System.out.print(magicSquare[i][j]+" "); // printing magic square elements
System.out.println();
}
}

if (ch == 1) {
for(i=0; i<n; i++)
{
for (int k = 0; k<n; k++) //for loop for printing magic square elements
System.out.print("---");
System.out.println();
for(j=0; j<n; j++)
System.out.print(magicSquare[i][j]+"|"); // printing magic square elements
System.out.println();
}
}
  
}
public static void main(String[] args) //main method
{
Scanner sc = new Scanner(System.in); //scanner method for taking input
int n = 0;
do {
System.out.println("Enter number of sides : "); //taking input for no of sides
n = sc.nextInt();
if (n == 2) //if block condition checking for n==2
System.out.println("Not Possible"); //printing the message if condition fails
if (n == 6 || n == 10 || n==14)
System.out.println("Don't press your luck");   

} while (n == 2 || n== 6 || n==10 || n==14 || n < 1 || n > 17);
System.out.println("with box-1 && without box-0 ");
int choice = sc.nextInt();
generateSquare(n, choice); // calling the generate square method
}
}

Hope This Helps, if you have any doubts Please comment i will get back to you, thank you and please thumbs up

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote