944,147 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 21306
  • Java RSS
Mar 21st, 2007
0

Magic Square

Expand Post »
Background:
An n x n matrix that is filled with the numbers 1, 2, 3, ... n squared 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. Write a program that reads in n squared values from the keyboard and tests whether they form a magic square when arranged as a square matrix. test for three features:
A. Did the user enter nsquared numbers for some n?
B. Do each of the numbers 1, 2, ...nsquared occur exactly once in the user input?
C. When the numbers are put into a square, are the sums of the rows, columns, and diagonals equal to each other?

If the size of the input is a square, test whether all numbers between 1 and nsquared are present. Then compute the row, column, and diagonal sums. Implement a class Square with methods

public void add (int i)
public bolean isMagic()

Here is what I have so far, not sure how to start the number part??


/**
* Write a program that creates a magic 4 x 4 square that is filled with
* numbers if the sum of the elements in each row, column, and in the 2 diagonals
* have the same value.
*
* @author (Todd Higdon)
* @version (CPSC 150-02 3-15-07)
*/
/**
* A 4 x 4 Magic Square.
*/
public class MagicSquares
{
private String [] [] square;
private static final int ROWS = 4;
private static final int COLUMNS = 4;
/**
* Constructs an empty square.
*/

public MagicSquares()
{
square = new String [ROWS][COLUMNS];
//fill with spaces
for (int i = 0; i < ROWS; i++)
for (int j = 0; j < COLUMNS; j++)
square [i][j] = " ";
}
/**
* Sets a field in the square. The field must be unoccupied.
@param i the row index
@param j the column index
@param user the user (n x n)
*/
public void set (int i, int j, String user)
{
if (square [i][j].equals (" "))
square [i][j] = user;
}

public String toString()
{
String r = " ";
for (int i = 0; i < ROWS; i++)
{
r = r + "|";
for (int j = 0; j < COLUMNS; j++)
r = r + square[i][j];
r = r + " |\n";
}
return r;

}
}

TESTER IS:


import java.util.*;
public class MagicSquaresTester
{
public static void main (String[]args)
{
Scanner in = new Scanner(System.in);
String user = "n";
MagicSquares square= new MagicSquares();
boolean done = false;
while (!done)
{
System.out.print (square.toString());
System.out.print(
"Row for " + user + " (-1 to exit): ");
int row = in.nextInt();
if (row <0) done = true;
else
{
System.out.print("Column for " + user + " : ");
int column = in.nextInt();
square.set(row, column, user);
if (user.equals("n"))
user = "n";

}
}
}
}
Similar Threads
Reputation Points: 13
Solved Threads: 0
Junior Poster in Training
volscolts16 is offline Offline
50 posts
since Feb 2007
Mar 22nd, 2007
0

Re: Magic Square

Not really read your assignment properly but maybe this:-

http://www.cs.princeton.edu/introcs/...uare.java.html
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: display image when button clicked
Next Thread in Java Forum Timeline: Am I missing something here?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC