Question: home / study / engineering / computer science / computer science questions and answers / assignment #1 magic square electronic submission due by monday, 10/1,

home / study / engineering / computer science / computer science questions and answers / assignment #1 magic square electronic submission due by monday, 10/1, 11:00pm. bring your typed, ...

Your question has been answered

Let us know if you got a helpful answer. Rate this answer

Question: Assignment #1 Magic Square Electronic submission due by Monday, 10/1, 11:00pm. Bring your typed, ...

Assignment #1 Magic Square

Electronic submission due by Monday, 10/1, 11:00pm. Bring your typed, written report to class Tuesday, 10/2.

Concepts

Review: class design, 2-dimensional arrays, etc.

Get your problem-solving brain cells firing!

Problem

A magic square of order N is an N x N arrangement of the integers from 1 to N2 into rows and columns such that the rows, columns, and the 2 diagonals add up to the same, specific number: N(N2 + 1)/2. Here is an example of a 3 x 3 Magic Square:

Notice that each row, each column, and the 2 diagonals add up to 15 [ 3(32 + 1)/2 ]. You are going to write a program that allows the user to enter an odd integer and create a Magic Square of that size.

Specification

Implementation: Your solution will only deal with Magic Squares of odd order N. The algorithm you must use to fill the cells is as follows:

Starting from the middle row, last column, with the number 1, the fundamental movement for filling the squares is diagonally right and down, one step at a time.

If a filled square is encountered, move to the left one square instead, then continue as before.

When a move would leave the Magic Square, wrap around.

You are to create a class according to this public specification:

class OddMagicSquare

+ OddMagicSquare(int n) : construct a MagicSquare of order n. If n is not a positive odd number, throw an appropriate exception. + String toString(): returns a String representation of this MagicSquare. The String should be made up of "lines" of numbers, one line for each row. No special formatting is required, but if you are interested, you can investigate the static method String.format(). + static boolean isMagic(int[][] a) : a static method that is passed in a 2 - dimensional array and returns true if it is a Magic Square, false if not. (This method should not throw an exception under any circumstances.) + static void main(String[] args) : the method that contains the user interface and runs the program.

Not extra credit, but an extra challenge... A given magic square can be flipped horizontally, flipped vertically or rotated 90 degrees clockwise or counterclockwise and still remain a magic square. For this extra challenge, add methods to accomplish one or more of these. These "update" or "mutator" methods should change the state of the object (change the data stored in the array) to correspond to the updated configuration.

User Interface: Your program will have a text-based interface, using the Scanner class for keyboard input and println for output. The program should prompt the user for an odd integer. If it is valid, then create a MagicSquare and display it. If the user input is invalid, then display a helpful message.

The program should repeat until the user enters an exit code to quit (like 0). Ensure that the user knowswhat that exit code is!

Suggestions

You always need to fully understand a problem before you can design a program to solve it. Try drawing a 5x5 magic square on paper following the instructions above. Then, test it by summing up at least one row, one column and one diagonal to see if it works.

You will have success if you code and test in pieces. I would recommend getting your class methods working first. Then work on the user interface.

Think carefully about the isMagic method. It is the most challenging. Keep in mind that the method should work properly for ANY value passed into the parameter. When you design the testing, consider all possible values that might be used when the method is called.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!