Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

// **************************************************************** // MagicSquare.java // // Text below is to be filled by student. // // **************************************************************** import java.util.Scanner; public class MagicSquare { int[][] square;

image text in transcribedimage text in transcribedimage text in transcribed

// **************************************************************** // 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; for(int i = 0; i

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

// **************************************************************** // 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 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++; } } }

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

magicText.txt file

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

Page ot :3 2OOM Test your work with the following website: //www.codestepbystep roblem/view/java/arrays/squaredAr 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 76-15 9 5 1-15 4 3 8-15 15 15 15 15 15 (source: https://en.wikipedia.org/wiki/Magic_square) California State University, Sacramento Page 1

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

Beginning C# 5.0 Databases

Authors: Vidya Vrat Agarwal

2nd Edition

1430242604, 978-1430242604

More Books

Students also viewed these Databases questions

Question

Name 2 programing by contract annotations (@xxxx) in Java

Answered: 1 week ago

Question

What are the purposes of promotion ?

Answered: 1 week ago