Question
1. To declare an array, initialize an array , and refer to individual elements of an array. 2. To manipulate a multidimensional array. 3. To
1.
To declare an array, initialize an array
,
and refer to individual elements
of an array.
2.
To manipulate
a
multidimensional array.
3.
To output a
content of a multidimensional array.
// **************************************** // MagicSquare.java //// Text below is to be filled by student. //// **************************************************************** import java.util.Scanner; public class MagicSquare {int[][] square;
public MagicSquare(int size){} //-------------------------------------- //return the sum of the values in the given row //-------------------------------------- private int sumMagicRow(int row){int sum = 0; return sum;} //-------------------------------------- //return the sum of the values in the given column //-------------------------------------- private int sumMagicCol(int col) { int sum = 0; // return sum;} //-------------------------------------- //return the sum of the values in the main diagonal //-------------------------------------- private int sumMagicDiagMain() {int sum = 0; // return sum;} //-------------------------------------- //return the sum of the values in the other ("reverse") diagonal //-------------------------------------- private int sumMagicDiagRev(){int sum = 0;// ......return sum; }//-------------------------------------- //return true if the square is magic (all rows, cols, and diags// have same sum), false otherwise //-------------------------------------- public boolean isMagicSquare(){boolean answer = true; // return answer;} //-------------------------------------- //compute and display sums of square including row, column, main diagonal, and other diagonal //-------------------------------------- public void printMagicSquareSums() {// } //-------------------------------------- //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 //// Text below is to be filled by student. //// **************************************************************** 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 directoryint count = 1; //count which square we're onint size = scan.nextInt(); //size of next square //Expecting -1 at bottom of input filewhile (size != -1) { //create a new Square of the given sizeMagicSquare 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 squares.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++; } } }
--------------------------------------------------------------------------------------------------
txt file is
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
Lab work (in school laboratory): 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 6 15 9 5 1-15 438-15 15 15 15 15 15Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started