Programming Fundamentals for CIS Majors 1
Fall, 2010

CIS 3022
Practice Lab

 

Introduction

A magic square is an n-by-n grid of integers with the property

when added up sum to the same constant. For instance, for this simple magic square the constant is (obviously) 15.

816
357
492

A normal magic square has the additional property that it contains all the integers from 1 to n squared. Thus

11
11

is a “non-normal“ magic square.

Problem statement

Implement and test a class that determines whether or not a square is “magic.” The method testAllCandidates( String filename ) shall open the specified file, each line of which contains the int values that comprise a single candidate. For each candidate, print the square (columns separated by TABS) and on the final row (one TAB after the last column) either

followed by 2 newline characters. For example, the input file:

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

would result in this output:

16	3	2	13
5	10	11	8
9	6	7	12
4	15	14	1	is magic (34)


1	1
1	1	is non-normal but magic (2)


1	2
3	4	is not a magic square

Notes and hints

Treat this like a QUIZ

Remember: the quizzes are closed book, closed notes, and closed internet. You may use only those files that are provided as part of the quiz (if any) and files that you write during that quiz period.

This website is an original work, Copyright © 2010 by Dave Small. All rights reserved.