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

You are going to write a program that allows a user to input a couple parameters

ID: 3885384 • Letter: Y

Question

You are going to write a program that allows a user to input a couple parameters and print a magic square appropriate for those parameters.

Program Description

Your program should ask the user to enter a number of sides (N) for the magic square. Your program should handle: 1, 3, 4, 5, 7, 8, 9, 11, 12, 13, 15, 16, 17

For 2, you should print "Not possible"

For 6, 10, 14, you should print "Don't press your luck"

For all other values, you should produce a valid Magic Square.

Your program should also ask the user for a preferred printing format, with or without box outlines. For a 3x3,

without outlines looks like:         with outlines looks like:

8 1 6                 --- --- ---

3 5 7              | 8 | 1 | 6 |

4 9 2                 --- --- ---

| 3 | 5 | 7 |

   --- --- ---  

| 4 | 9 | 2 |

   --- --- ---

Finally, for any allowed N > 2 your program should ask for an "example id" a-d. Every time a user enters 'a' for a particular N, they should see the same square; likewise for b-c. However, the square they see when they enter 'a' should not be identical to the one they see for 'b', 'c' or 'd'; likewise for 'b' and 'c', 'c' and 'd', and 'b' and 'd'. (in other words, they're all different)

After requesting these 3 options, your program should print an appropriate magic square.

Unit Tests

You must submit JUnit tests that verify all of the major components of your solution. You should have no fewer than 3 and probably no more than 10.

Explanation / Answer

import 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);
     }
}

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