Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

// **************************************************************** // MagicSquare.java // // // // **************************************************************** import java.util.Scanner; public class MagicSquare { int[][] square; public MagicSquare(int size) { /* private int SumMagicRow

image text in transcribed

image text in transcribed

image text in transcribed

// **************************************************************** // MagicSquare.java // // // // **************************************************************** import java.util.Scanner; public class MagicSquare {

int[][] square;

public MagicSquare(int size) { /* private int SumMagicRow (int row) { int sum = 0; for (int c =0; c

//-------------------------------------- //read info into the square from the input stream associated with //the Scanner parameter //-------------------------------------- public void readSquare(Scanner scan) { for (int row = 0; row

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// **************************************************************** // MagicSquareTest.java // // // // **************************************************************** import java.util.Scanner; import java.io.IOException; import java.io.File; public class MagicSquareTest { public static void main(String[] args) throws IOException { Scanner scan = new Scanner(new File("magicText.txt")); // make sure that the file magicData is in the current directory int count = 1; //count which square we're on int size = scan.nextInt(); //size of next square //Expecting -1 at bottom of input file while (size != -1) { //create a new Square of the given size MagicSquare s = new MagicSquare(size); //call its read method to read the values of the square System.out.println(" ***** Square " + count + " *****"); s.readSquare(scan); //print the square s.printSquare();

//print the square id

//print the sums //determine and print whether it is a magic square //get size of next square size = scan.nextInt(); count++; } } }

///////////////////////////////////////////////////////////////////////////////////////////////////////// MagicTxt.txt below

3

8 1 6

3 5 7

4 9 2

7

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

4

48 9 6 39

27 18 21 36

15 30 33 24

12 45 42 3

3

6 2 7

1. 5 3

2. 9 4

4

3. 16 2 13

6 9 7 12

10 5 11 8

15 4 14 1

5

17 24 15 8 1

23 5 16 14 7

4. 6 22 13 20

10 12 3 21 19

11 18 9 2 25

7

30 39 48 1 10 28 19

38 47 7 9 18 29 27

46 6 8 17 26 37 35

5. 14 16 25 34 45 36

13 15 24 33 42 4 44

21 23 32 41 43 12 3

22 31 40 49 2 20 11

- 1

This lab's objective is to exercise with usages of Java's array features. We would like to apply this learning to determine if a two-dimensional array is a magic square. A magic square is a square matrix in which the sum of every row, every column, and both diagonals is the same. An example below is a magic square: 2 7 615 95115 43815 15% 15 15 15 (source: https://en.wikipedia.org/wiki/Magic_square)

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 Administration The Complete Guide To Dba Practices And Procedures

Authors: Craig S. Mullins

2nd Edition

0321822943, 978-0321822949

More Books

Students also viewed these Databases questions