Note that the due date for this lab has been postponed from the original due date as announced in the course outline.
Magic squares: Do Programming Exercise P8.18 (page 318).
Although the exercise does not explicitly say so, you are to provide two Java classes:
the Square class mentioned in the assignment, and
a SquareTester class that includes a main method that handles all input and output (I/O) and calls the methods in the Square class to determine whether or not the numbers entered by the user form a magic square.
In addition to the methods add() and isMagic() specified in the assignment, provide a toString() method in the Square class that returns a String representing the values added to the square in the shape of a square, unless the sequence of values does not form a legal magic square, in which case linear list of the values is returned. Hint: the string returned by the toString() method can include newline characters ('\n').
The main method should prompt the user to enter a sequence of positive integers, signalling the end of input with 'q' or 'Q'. As the input values are read, your program should check that the input actually corresponds to a positive integer, rejecting any input values that do not. For example, the following transcript might represent an interaction that results in a correct magic square being entered (where text in navy represents output produced by the program):
Welcome to the Magic Square Tester Please enter a sequence of positive integers that uses each of the integers between 1 and N in some order, where N is some square number (e.g., 9, 16, or 25). Signal the end of input by entering the letter 'Q'. 4 9 2 3 a5 Input values must be positive integers (or 'Q' to quit). 5 7 8 1 6.0 Input values must be positive integers (or 'Q' to quit). 6 q The input values form a magic square: 4 9 2 3 5 7 8 1 6
Here is an example of the final output from the program if the input sequence does not form a legal magic square:
The input values do not form a magic square:
4 9 2 3 5 8 1 6
Note that the isMagic() method of the Square class checks the three features of a magic square specified in the assignment as presented in the textbook. The main method in the SquareTester class only tests that the input values are positive integers, but does not count them or ensure that each integer is entered only once or that all the integers between 1 and n2 are entered.
Your solution should display the following characteristics:
Correctness - The program should conform to the specifications for which it was written. It should include correct handling of error conditions as specified above.
Design and Efficiency - Each class should be constructed from small, coherent methods. The control constructs and data structures used should be those appropriate to the problem at hand. The program should not perform unnecessary steps, use extraneous variables, nor implement the algorithm in a contorted or inefficient way.
Style and Documentation - The program should conform to generally accepted principles of style, such as a consistent pattern of indentation, use of meaningful identifiers, generous use of space, etc., as specified in the Programming Style Guide (see Appendix A, pp. 863ff.) Internal documentation should include class and method headers, and in-line comments to clarify the code where appropriate.
This assignment will account for 6% of your final grade in the course.
Submit your source code (i.e., your .java files) electronically using the following submission form. If you have not changed your web submission password, it will be your 8-digit Augustana ID number (with no spaces or hyphens).
You may submit either or both files multiple times; only the last version submitted will be graded. Late submissions will be accepted, but will be subject to a penalty of 1% per hour or portion thereof.
Your assignment submissions are password-protected. These passwords apply only to form data submitted via the Web server. They are separate from (and typically different from) your network password.