7 Replies - 7935 Views - Last Post: 24 April 2009 - 09:19 AM Rate Topic: -----

#1 49erSTANG  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 23-April 09

Magic Square Program

Posted 23 April 2009 - 08:59 AM

i have this program and honestly i am just completely lost. i was wondering if anyone could help me with out on where i should start for this program. im not asking for yall to do the program, i just need to be pointed in the right direction and just any other help would be great. thanks.

here is the description of the assignment:
Robust Magic Square Tester

Your next-to-last programming project for this semester will be to write a program to determine if a file of numbers is a magic square. (In mathematics, a magic square of order n is an arrangement of n² numbers in a square, such that the n numbers in all rows, all columns, and both diagonals sum to the same constant. A normal magic square contains the integers from 1 to n². See http://en.wikipedia....ki/Magic_square for additional information on magic squares.)

You will also have a new goal for this project: I want you to write you program so that it will not crash. I want you to check for and handle every possible problem with the input. Handling a problem means detecting the problem, printing a clear and concise description of the problem, and ending the test gracefully.

The input to your program will be a text file with n lines, each line containing n integers, and each number separated by whitespace. For example:

2 7 6
9 5 1
4 3 8
The file(s) to be tested will be specified as command line arguments. For example, if the above lines were in a file named 3x3.txt, your program would be invoked as follows:

java MagicSquare 3x3.txt
Since the arrangement of numbers in this file does form a magic square, your program should produce the following output:

The file "3x3.txt" is a magic square of order 3; the magic constant is 15.
If the file is not a magic square, indicate this fact and include a clear and concise description of the problem.

Your program should be in a class named MagicSquare.

Your program should take any number of files to check as command line arguments, and check each file specified in sequence. Ignoring this requirement will significantly impact your grade. If there is a problem with any of the specified files, including a file not existing, your program should print the appropriate error message, and continue to process the other specified files.

If the file is a magic square, you should only print one line of text indicating this, as above.

Ideally, you should figure out the possible problems. The best way to do this would be to think through the problems during your design process, and determine all the possible reasons the input file might fail to be a magic square. If we had more time, I would have you submit this list as a separate assignment. Realizing that time is short, I will work with you on the Blackboard Discussion area to identify all the possible problems.

I would suggest you use the techniques we've gone over for Tokenizing Strings for parsing the input lines of the file.

Some problems you'll detect and handle with the logic of code you write. You'll likely have to directly identify the situation where the input file does not contain a "square" of numbers, or doesn't contain the integers from 1 to n². Other problems you'll handle by writing an exception handler for exceptions thrown from other methods, such as if one of the input files specified doesn't exist.

Is This A Good Question/Topic? 0
  • +

Replies To: Magic Square Program

#2 markhazlett9  Icon User is offline

  • Coding is a lifestyle
  • member icon

Reputation: 60
  • View blog
  • Posts: 1,666
  • Joined: 12-July 08

Re: Magic Square Program

Posted 23 April 2009 - 10:06 AM

Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.

Please post like this:

:code:

[/b]
Your code here
[b]


Thank you for helping us helping you.
Was This Post Helpful? 0
  • +
  • -

#3 SayMoi  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 135
  • Joined: 08-April 09

Re: Magic Square Program

Posted 23 April 2009 - 11:53 AM

I'll be nice and give you a few pointer questions for you to answer and expand a bit on your part of the effort. (You'll find people are actually really happy to help if you don't make it look like your giving them your assignment. :) )

Have you researched what a Magic Square is? Do you understand that?

What kind of a project design have you started? Do you have the functions of your program broken down to classes (or at least categories)?

Do you understand File reading? Do you have a plan for file storage?

Be a bit more specific and we'll see what we can do.
Was This Post Helpful? 0
  • +
  • -

#4 49erSTANG  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 23-April 09

Re: Magic Square Program

Posted 23 April 2009 - 12:44 PM

View PostSayMoi, on 23 Apr, 2009 - 10:53 AM, said:

I'll be nice and give you a few pointer questions for you to answer and expand a bit on your part of the effort. (You'll find people are actually really happy to help if you don't make it look like your giving them your assignment. :) )

Have you researched what a Magic Square is? Do you understand that?

What kind of a project design have you started? Do you have the functions of your program broken down to classes (or at least categories)?

Do you understand File reading? Do you have a plan for file storage?

Be a bit more specific and we'll see what we can do.



yes i have researched what a magic square is and i understand what it is

right now im just trying to figure out the file reading bc i have kinda figured out how to see if it is a magic square or not but then im lost on kinda what to do. i know i need to read in the files to see if they are a magic square or not and i also know that i need to check for errors and expect them with exceptions
Was This Post Helpful? 0
  • +
  • -

#5 SayMoi  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 135
  • Joined: 08-April 09

Re: Magic Square Program

Posted 23 April 2009 - 02:30 PM

Alright, I'll see if I can give a few pointers. First, here's a basic FileWriter example…that's what you'll want to use to get the numbers, except instead of printing them out you'll want to save each number as a parsed integer. http://www.java2s.co...ndSystemout.htm

Check the file with if statements while you're doing that (things like length, are the numbers really numbers, etc). Next, if you've learned about 2D arrays, I'd keep all the numbers in a 2D array…the first dimension being the rows of the square, the second being the three numbers of each row.

In terms of the actual Files, Java has a nice File object with a lot of useful methods to check if they exist etc…here's the API specs: http://java.sun.com/javase/6/docs/api/. Read through the methods and use anything that applies.

If you have any other questions just post. Oh, and another rule around here is proper English…no chatspeak (at least in the forums). :)
Was This Post Helpful? 0
  • +
  • -

#6 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Magic Square Program

Posted 23 April 2009 - 02:35 PM

oooor
Scanner in = new Scanner(new FIle("fileName")); 
int one = in.nextLine();
int two = in.nextLine();
int three = in.nextLine();
in.nextLIne(); // clears the buffer and goes to the next line to be read.


I think that is the easiest way you would read in ints. You would put the code in a while loop and just add the code onto it. Goodluck!and Hope this helps!
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,912
  • Joined: 06-March 08

Re: Magic Square Program

Posted 23 April 2009 - 06:51 PM

I would rather use a Scanner to read in a String the first line
then use s StringTokenizer to get the number of elements... build the array on NXN
then read the other lines
have an int method that valid if the square is magic (returns -1 or the magic number)
Was This Post Helpful? 0
  • +
  • -

#8 49erSTANG  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 23-April 09

Re: Magic Square Program

Posted 24 April 2009 - 09:19 AM

ok this is some of what i have so far...im still kinda confused on the stringTokenizer and the arrays of the square
public class MagicSquare
{

public static void main (String [] args) 
{

String FileData = "";
for(int i=0, i<args.length, i++){
FileData = readFileData(args[i]);
System.out.println(args[i] + "\n" + FileData);
}

}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1