Please program in Java A magic square is a grid of unique nu

Please program in Java

A magic square is a grid of unique numbers (i.e. no number can appear more than once) in which the sum of all the rows, columns, and diagonals are the same. Here is an example in which all of the rows, columns and diagonals (upper left comer, center, lower right corner and upper right comer, center, and lower left corner) sum to 15. Generally, the numbers within a magic square range between 1 and n^2. where n is the size of the side of the square. Your goal for this project is to write a program that helps a user to create a magic square. Your program should meet the following requirements: Initialization: A Magic square is a grid of numbers with n numbers along each side. For example, in the magic square above, n = 3. The value for n can technically be any positive number except 2 (because then there would be no diagonals), but due to hardware limitations it is probably a good idea to put an upper bound on n. For this program we will allow n to be at most 8. When your program begins its execution, it should ask the user how big the magic square should be. For example: Let\'s make a Magic Square! How big should it be? 2 That would violate the laws of mathematics! Let\'s make a Magic Square! How big should it be? -4 That would violate the laws of mathematics! Let\'s make a Magic Square! How big should it be? 12 That\'s huge! Please enter a number less than 9. Let\'s make a Magic Square! How big should it be? 4 Great! Display: After the size has been chosen and in between each user input, your program should display the current state of the square. Use zeros to indicate empty places. For example, after the initialization above, your program should display: The square currently looks like this: Value entry: Allow the user to enter or change a value in the puzzle by providing the row and column of the value they want to change, along with the new value. Keep in mind that a user might want to overwrite an existing value. Also, remember that all numbers must be between 1 and n^2. For example: Where do you want to put a new value? Row: theta Column: 3 What value should go there? 2 The square currently looks like this: Where do you want to put a new value? Row: theta Column: 3 What value should go there? 4 The square currently looks like this: Where do you want to put a new value? Row: theta Column: 3 What value should go there? 191 You can only use numbers between 1 and 16 for this square. The square currently looks like this: Solution Recognition: When the magic square is complete, congratulate the user. For example: The square currently looks like this: 7 12 1 14 2 13 8 11 16 3 10 5 9 6 15 4

Solution

import java.util.Scanner;

public class Test{
   public static void display(int arr[][]){
       System.out.println(\"The square currently looks like this:\");
       for(int i = 0; i < arr.length; i++){
           for(int j = 0; j < arr[i].length; j++){
               System.out.print(arr[i][j] + \" \");
               if(arr[i][j] < 10) System.out.print(\" \");
           }
           System.out.println();
       }
       System.out.println();
   }
  
   static boolean completed(int arr[][]){
       for(int i = 0; i < arr.length; i++){
           for(int j = 0; j < arr[i].length; j++){
               if(arr[i][j] == 0) return false;
           }
       }
       return true;
   }
  
   static boolean correct(int arr[][]){
       int sum = 0, temp;
       for(int i = 0; i < arr[0].length; i++){
           sum += arr[0][i];
       }
       for(int i = 0; i < arr.length; i++){
           temp = 0;
           for(int j = 0; j < arr[i].length; j++){
               temp += arr[i][j];
           }
           if(temp != sum) return false;
       }
       for(int i = 0; i < arr[0].length; i++){
           temp = 0;
           for(int j = 0; j < arr.length; j++){
               temp += arr[j][i];
           }
           if(temp != sum) return false;
       }
       temp = 0;
       for(int i = 0; i < arr.length; i++){
           temp += arr[i][i];
       }
       if(temp != sum) return false;
       temp = 0;
       for(int i = 0; i < arr.length; i++){
           temp += arr[i][arr.length - i - 1];
       }
       if(temp != sum) return false;
       return true;
   }
  
   public static void main(String args[]){
       Scanner in = new Scanner(System.in);
       int size;
       do{
           System.out.print(\"Let\'s make a Magic Square! How big should it be? \");
           size = in.nextInt();
           if(size >= 9){
               System.out.println(\"That\'s huge! Please enter a number less than 9.\ \");
           }
           else if(size <= 0 || size == 2) System.out.println(\"That would violate the laws of mathematics!\ \");
       }while(size <= 0 || size > 8 || size == 2);
       System.out.println(\"Great!\ \");
       int arr[][] = new int[size][size];
      
       int row, col, num;
       while(true){
           display(arr);
           System.out.println(\"Where do you want to put a new value? \");
           System.out.print(\"Row: \");
           row = in.nextInt();
           System.out.print(\"Column: \");
           col = in.nextInt();
           System.out.print(\"What value should go there? \");
           num = in.nextInt();
           arr[row][col] = num;
           if(num <= 0 || num > size * size){
               System.out.println(\"You can only use numbers between 1 and \" + size * size + \" for this square\");
               continue;
           }
           System.out.println();
           if(completed(arr)){
               if(correct(arr)){
                   System.out.println(\"Congratultions!\");
                   display(arr);
                   break;
               }
               else{
                   System.out.println(\"Incorrect solution!\");
               }
           }
           System.out.println();
       }
   }  
}

Please program in Java A magic square is a grid of unique numbers (i.e. no number can appear more than once) in which the sum of all the rows, columns, and diag
Please program in Java A magic square is a grid of unique numbers (i.e. no number can appear more than once) in which the sum of all the rows, columns, and diag
Please program in Java A magic square is a grid of unique numbers (i.e. no number can appear more than once) in which the sum of all the rows, columns, and diag

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site