Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

------------------------------------------------------------------------- CODE IN JAVASCRIPT ONLY USE PARAMATERS SPECIFIED ONLY JAVA UTIL SCANNER AND OTHER IMPORTS CALLED UPON DO NOT USE READLINE OR ANY UNNECESSARY FUNCTIONS

------------------------------------------------------------------------- CODE IN JAVASCRIPT ONLY USE PARAMATERS SPECIFIED ONLY JAVA UTIL SCANNER AND OTHER IMPORTS CALLED UPON DO NOT USE READLINE OR ANY UNNECESSARY FUNCTIONS ------------------------------------------------------------------------ Magic Squares (CODE IN JAVASCRIPT USING ONLY JAVA UTIL SCANNER AND ARRAYLIST IF NEEDED)

You will be working to create a program that will check whether an inputted list of numbers is a magic square, a square array of numbers that sums to the same value in each direction.

Terminology

Magic Square: A square 2D array of integers that sums up to the same value (magic number) in all directions (horizontal, vertical, diagonal). A magic square of size 3 has a magic number of 15. There are no duplicate numbers within the square.

Two Dimensional (2D) Array: An array that contains arrays. Often used to represent a two-dimensional concept, like a table or a tic-tac-toe board. Elements within a 2D array can be accessed like so: myArray[i][j] where i is retrieving a specific inner array from the outer array (row) and j is a specific element within that inner array (column). More Information:

Work to create a solution to determine whether an inputted list of numbers is a magic square.

Your program should take the following inputs:

The size of the magic square

The values to go into the magic square

Your program should perform the following computations:

Calculate the magic number from the magic square size

Check if the inputted list of numbers can even be a valid magic square by giving it to the compareArrays method and examining the boolean return value.

The compareArrays method checks that there is only a single instance of each integer from 1 to (size2 - 1)

Check if the sum of all rows, columns, and diagonals are equal to the magic number

Additional Requirements:

Use only scnr.nextInt() for this lab as we only need to take in ints as inputs.

You must use a 2D array.

Your program will need to print out the various inputs and results in proper formatting, as shown in the sample outputs below.

Sample Outputs

Output 1:

Enter in the magic square size you would like:

3

The magic number for size 3 is 15.

Enter in the values:

4 9 2 3 5 7 8 1 6

You entered:

4 9 2

3 5 7

8 1 6

Analyzing...

This is a magic square!

Output 2:

Enter in the magic square size you would like:

4

The magic number for size 4 is 34.

Enter in the values:

1 15 14 4 12 6 7 9 8 10 11 5 13 3 2 16

You entered:

1 15 14 4

12 6 7 9

8 10 11 5

13 3 2 16

Analyzing...

This is a magic square!

Output 3:

Enter in the magic square size you would like:

7

The magic number for size 7 is 175.

Enter in the values:

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

You entered:

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

Analyzing...

This is a magic square!

Output 4:

Enter in the magic square size you would like:

5

The magic number for size 5 is 65.

Enter in the values:

17 2 23 8 15 24 7 14 16 1 5 21 19 3 17 6 12 18 13 16 13 22 4 20 5

You entered:

17 2 23 8 15

24 7 14 16 1

5 21 19 3 17

6 12 18 13 16

13 22 4 20 5

Analyzing...

The input cannot be a magic square! There must be one of each value from 1 to 25.

Row 1 does not work! These are the values in row 1: 24 7 14 16 1

Row 4 does not work! These are the values in row 4: 13 22 4 20 5

Column 1 does not work! These are the values in column 1: 2 7 21 12 22

Column 2 does not work! These are the values in column 2: 23 14 19 18 4

Column 3 does not work! These are the values in column 3: 8 16 3 13 20

Column 4 does not work! These are the values in column 4: 15 1 17 16 5

Diagonal 1 does not work! These are the values in diagonal 1: 17 7 19 13 5

Diagonal 2 does not work! These are the values in diagonal 2: 15 16 19 12 13

This is not a magic square!

Output 5:

Enter in the magic square size you would like:

4

The magic number for size 4 is 34.

Enter in the values:

16 1 7 10 8 11 6 9 5 14 2 13 12 3 15 4

You entered:

16 1 7 10

8 11 6 9

5 14 2 13

12 3 15 4

Analyzing...

Column 0 does not work! These are the values in column 0: 16 8 5 12

Column 1 does not work! These are the values in column 1: 1 11 14 3

Column 2 does not work! These are the values in column 2: 7 6 2 15

Column 3 does not work! These are the values in column 3: 10 9 13 4

Diagonal 1 does not work! These are the values in diagonal 1: 16 11 2 4

Diagonal 2 does not work! These are the values in diagonal 2: 10 6 14 12

This is not a magic square!

Output 6:

Enter in the magic square size you would like:

4

The magic number for size 4 is 34.

Enter in the values:

1 16 6 11 14 7 9 4 8 5 12 13 3 10 15 2

You entered:

1 16 6 11

14 7 9 4

8 5 12 13

3 10 15 2

Analyzing...

Row 2 does not work! These are the values in row 2: 8 5 12 13

Row 3 does not work! These are the values in row 3: 3 10 15 2

Column 0 does not work! These are the values in column 0: 1 14 8 3

Column 1 does not work! These are the values in column 1: 16 7 5 10

Column 2 does not work! These are the values in column 2: 6 9 12 15

Column 3 does not work! These are the values in column 3: 11 4 13 2

Diagonal 1 does not work! These are the values in diagonal 1: 1 7 12 2

Diagonal 2 does not work! These are the values in diagonal 2: 11 9 5 3

This is not a magic square!

Output 7:

Enter in the magic square size you would like:

3

The magic number for size 3 is 15.

Enter in the values:

1 2 3 4 5 6 7 8 9

You entered:

1 2 3

4 5 6

7 8 9

Analyzing...

Row 0 does not work! These are the values in row 0: 1 2 3

Row 2 does not work! These are the values in row 1: 7 8 9

Column 0 does not work! These are the values in column 0: 1 4 7

Column 2 does not work! These are the values in column 2: 3 6 9

This is not a magic square!

Output 8:

Enter in the magic square size you would like:

4

The magic number for size 4 is 34.

Enter in the values:

34 0 0 0 0 34 0 0 0 0 0 34 0 0 0 0 34

You entered:

34 0 0 0

0 34 0 0

0 0 0 34

0 0 0 0

Analyzing...

The input cannot be a magic square! There must be one of each value from 1 to 16.

Row 3 does not work! These are the values in row 3: 0 0 0 0

Column 2 does not work! These are the values in column 2: 0 0 0 0

Diagonal 1 does not work! These are the values in diagonal 1: 34 34 0 0

Diagonal 2 does not work! These are the values in diagonal 2: 0 0 0 0

This is not a magic square!

Output 9:

Enter in the magic square size you would like:

2

The magic number for size 3 is 15.

Enter in the values:

1 2 2 1

You entered:

1 2

2 1

Analyzing...

The input cannot be a magic square! There must be one of each value from 1 to 4.

Row 0 does not work! These are the values in row 0: 1 2

Row 1 does not work! These are the values in row 1: 2 1

Column 0 does not work! These are the values in column 0: 1 2

Column 1 does not work! These are the values in column 1: 2 1

Diagonal 1 does not work! These are the values in diagonal 1: 1 1

Diagonal 2 does not work! These are the values in diagonal 2: 2 2

This is not a magic square!

Output 10:

Enter in the magic square size you would like:

-1

Please enter in a number in greater than 0

-3

Please enter in a number in greater than 0

5

The magic number for size 5 is 65.

Enter in the values:

9 3 22 16 15 2 21 20 14 8 25 19 13 7 1 18 12 6 5 24 11 10 4 23 17

You entered:

9 3 22 16 15

2 21 20 14 8

25 19 13 7 1

18 12 6 5 24

11 10 4 23 17

Analyzing...

This is a magic square! ------------------------------------------------------------------------- CODE IN JAVASCRIPT ONLY USE PARAMATERS SPECIFIED ONLY JAVA UTIL SCANNER AND OTHER IMPORTS CALLED UPON DO NOT USE READLINE OR ANY UNNECESSARY FUNCTIONS ------------------------------------------------------------------------

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

Automating Access Databases With Macros

Authors: Fish Davis

1st Edition

1797816349, 978-1797816340

More Books

Students also viewed these Databases questions