Shuffle an Array and Test Magic Square (Due 22 July 2009)

This assignment has two parts. These are two independent programs that you will be writing and submitting separately.

I. Shuffle an Array

You will be writing a program to shuffle an array of integers. Your program will have two methods shuffleArray() and printArray(). The method signatures will be:
public static void shuffleArray ( int[] anArray )

public static void printArray ( int[] anArray )

To shuffle an array you can use this basic algorithm:

Use Math.random() to generate a random number between 0 and 1. Scale the number by multiplying by the array size and casting it to an integer. This will give you a random index in the array.

The method printArray() will print the contents of an array in a nicely formatted fashion such that there is at least two blank spaces between adjacent numbers and no more than five numbers to a line.

In your method main() you will define an array. Print out the contents. Shuffle the array and print out the result. Shuffle the array again and print out the contents. Thus your main will look like:

int[] theArray = { 3, 5, 1, 7, 0, 9, 2, 6, 4, 8 };

printArray ( theArray );

shuffleArray ( theArray );

printArray ( theArray );

shuffleArray ( theArray );

printArray ( theArray );

The file that you will be turning in will be called Shuffle.java. You will follow the standard Java coding convention that I have appended below. The file will have a header of the following form:

/*
  File: Shuffle.java

  Description:

  Student Name:

  Student UT EID:

  Course Name: CS 303E

  Unique Number:  

  Date Created:

  Date Last Modified:

*/

You will follow the standard Java Coding Conventions. You can either view the HTML page or download the PDF or Postscript and print it out. There is a modification that I would like to make to the standard coding conventions. Please align the opening and closing braces vertically so that you can easily make out the blocks of code. For example:

Do this:
if ( x > 5 )
{
  a = b + c;
}

Not this:
if ( x > 5 ) {
  a = b + c;
}

II. Magic Square (Due 22 July 2009)

A magic square is a two dimensional array where the sum of the rows is equal to the sum of the columns and is equal to the sum of the two diagonals respectively. In this program you will write a method isMagic() that will determine if a two dimensional array forms a magic square. The method should be general enough to accept magic squares of any size greater than or equal to 3. The method signature is:
public static void isMagic ( int[][] b )
Your method will print the sum of each row, sum of each column, and the sum of each diagonal. It will then write out if the 2-D array was a magic square. Your output would look like:
Sum of rows: 15 15 15
Sum of columns:  15 15 15
Sum of diagonals: 15 15

The array is a magic square.

In your method main() you will test whether the method isMagic() is working correctly or not. Hard code the following arrays and test them: There is a mix of Magic Squares and non-Magic Squares and your test results should reflect that.

int[][] xArray = {{5, 9, 1}, {3, 4, 8}, {7, 2, 6}};
int[][] yArray = {{1, 3, 16, 14}, {8, 15, 2, 9},
		  {13, 6, 11, 4}, {12, 10, 5, 7}};
int[][] zArray = {{18, 24, 5, 6, 12}, {10, 11, 17, 23, 4},
		  {22, 3, 9, 15, 16}, {14, 20, 21, 2, 8},
                  {1, 7, 13, 19, 25}};

The file that you will be turning in will be called MagicSquare.java. You will follow the standard Java coding convention that I have appended below. The file will have a header of the following form:

/*
  File: MagicSquare.java

  Description:

  Student Name:

  Student UT EID:

  Course Name: CS 303E

  Unique Number: 

  Date Created:

  Date Last Modified:

*/

You will follow the standard Java Coding Conventions. You can either view the HTML page or download the PDF or Postscript and print it out. There is a modification that I would like to make to the standard coding conventions. Please align the opening and closing braces vertically so that you can easily make out the blocks of code. For example:

Do this:
if ( x > 5 )
{
  a = b + c;
}

Not this:
if ( x > 5 ) {
  a = b + c;
}

Use the turnin program to submit your Shuffle.java and MagicSquare.java files. The TAs should receive your work by 5 PM on Wednesday, 22 July 2009. There will be substantial penalties if you do not adhere to the guidelines.