Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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,

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 corner, center, lower right corner and upper right corner, center, and lower left corner) sum to 15. Generally, the numbers within a magic square range between 1 and n2, where n is the size of the side of the square. 4 9 2 3 5 7 8 1 6 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: Lets make a Magic Square! How big should it be? 2 That would violate the laws of mathematics! Lets make a Magic Square! How big should it be? -4 That would violate the laws of mathematics! Lets make a Magic Square! How big should it be? 12 Thats huge! Please enter a number less than 9. Lets 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: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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 n2. For example: Where do you want to put a new value? Row: 0 Column: 3 What value should go there? 2 The square currently looks like this: 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 Where do you want to put a new value? Row: 0 Column: 3 What value should go there? 4 The square currently looks like this: 0 0 0 4 0 0 0 0 0 0 0 0 0 0 0 0 Where do you want to put a new value? Row: 0 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: 0 0 0 4 0 0 0 0 0 0 0 0 0 0 0 0 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 Victory! This is what I have so far. I need help finishing and debugging my program public static void main(String[] args) { int size = getSize(); printSquare(); playSquare(); } public int getSize() { Scanner input = new Scanner(System.in); int size = -1; boolean inputtaken = false; while (!inputtaken) { System.out.printf("Lets make a Magic Square! How big should it be?"); size = input.nextInt(); if (size < 3) { System.out.println("That would violate the laws of mathematics!"); } else if (size > 9) { System.out.println("Thats huge! Please enter a number less than 9."); } else { System.out.println("Great"); inputtaken = true; } } return size; } public void printSquare(int size) { System.out.println("The square currently looks like this:"); int i, j; for (i = 0; i < size; i++) { for (j = 0; j < size; j++) { Object[][][] array = null; System.out.printf("%3d ", array[i][j]); } System.out.println(); } } public void playSquare(int size, int[][] array) { Scanner input = new Scanner(System.in); boolean done = false; while (!done) { int result = printSquare(); if (checkSquare(array)) { done = true; } else { System.out.println("Where do you want to put a new value?"); System.out.printf("Row:"); int row = input.nextInt(); System.out.printf("COl:"); int col = input.nextInt(); System.out.println("What value should go there?"); int val = input.nextInt(); if (val >= 1 && val <= size * size) { array[row][col] = val; } else { System.out.println("You can only use numbers between " + 1 + " and " + size * size + " for this square."); } System.out.println("victory"); } public static boolean checkSquare(int[][] array, int size) { for (int row = 0; row < size; row++) { int sumOfRow = 0; int sumOfColoumns = 0; int nSquare = size * size; int M = (size * size * (size * size + 1) / 2) / size; int sumOfPrimaryDiagonal = 0; int sumOfSecondaryDiagonal = 0; boolean[] flag = new boolean[size * size]; for (int col = 0; col < size; col++) { if (array[row][col] < 1 || array[row][col] > nSquare) { return false; } if (flag[array[row][col] - 1] == true) { return false; } flag[array[row][col] - 1] = true; sumOfRow += array[row][col]; sumOfColoumns += array[col][row]; } sumOfPrimaryDiagonal += array[row][row]; sumOfSecondaryDiagonal += array[row][size - row - 1]; if (sumOfRow != M || sumOfColoumns != M) { return false; } if (sumOfPrimaryDiagonal != M || sumOfSecondaryDiagonal != M) { return false; } } return true; } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

DATABASE Administrator Make A Difference

Authors: Mohciine Elmourabit

1st Edition

B0CGM7XG75, 978-1722657802

More Books

Students also viewed these Databases questions

Question

15-5 How will MIS help my career?

Answered: 1 week ago