Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java This assignment gives you an opportunity to use arrays to solve a problem and experiment with the reading and writing of files. For this

Java

This assignment gives you an opportunity to use arrays to solve a problem and experiment with the reading and writing of files. For this assignment you are to develop a Java Application for a Magic Squares Game. An n x n matrix is filled with the numbers 1, 2, 3, , n^2 is a magic square if the sum of the elements in each row, in each column, and in the two diagonals is the same value. For example,

image text in transcribed

You are to create a Java application, named MagicSquares. The application (a Java program with a main method) allows the user to select the type of algorithm they would like to execute, obtains any required input, computes and outputs the result and allows the user to select again.

Your program should display a menu similar to the following and then process the input accordingly:

(1) Input magic values from file

(2) Construct magic squares from input size and write to file

(3) Exit the program

Write a method named, inputMagicValues(), that prompts the user for a filename, reads in 2 n values from that file (one number per line) and then tests whether they form a magic square when put into array form. You need to test three features:

Did the file contain n^2 numbers for some n ?

Does each of the numbers 1, 2, , n^2 occur exactly once in the input?

When the numbers are put into a square (left to right, top to bottom), are all of the sums of the rows, columns, and diagonals equal to each other?

(If the size of the input is a square, test whether all of the numbers between 1 and n are present. Then compute the row, column and diagonal sums. The program should print appropriate error messages to the console window when the input is not valid, and print the completed magic square to the console window, similar to that shown above when the input is valid.)

Write another method named, constructMagicSquare(), that reads in a value for n from the keyboard, prompts the user for a filename where results should be stored, and then implements the following algorithm to construct magic n x n squares. This algorithm works only if n is odd, so if the use enters an even value for n , you must display an error message and prompt the user for an odd integer. To start the algorithm, place a 1 in the middle of the bottom row. After k has been placed in the [i][j] square, place k + 1 into the square to the right and down, wrapping around the borders. However, if the square to the right and down has already been filled, or if you are in the lower right corner, then you must move to the square straight up instead. Here is the 5x5 square that you get if you follow this method:

image text in transcribed

Your method should write the magic square of order n to the specified file, if n is odd.

1811 2 1 2171 2 3161 5 1594

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

Conceptual Database Design An Entity Relationship Approach

Authors: Carol Batini, Stefano Ceri, Shamkant B. Navathe

1st Edition

0805302441, 978-0805302448

More Books

Students also viewed these Databases questions

Question

What does Processing of an OLAP Cube accomplish?

Answered: 1 week ago