AP Computer Science I

advertisement
AP Computer Science
Lab08d
The "Magic Square" Program
Assignment Purpose:
100 & 110 Point Versions
The purpose of this program is to work as a team to design a class that creates and manipulate
two-dimensional arrays using the “magic square” algorithm.
Write a program that creates odd-sized Magic Squares. A magic square is a square matrix of consecutive numbers,
such that the numbers in the rows, the numbers in the columns, and the numbers in the diagonals add up to the same
sum. For this program you will only be concerned with “odd-sized” magic squares. Odd refers to the size of the matrix,
such as a 3 X 3, or a 5 X 5, or a 9 X 9 sized matrix.
Examples of a 3 X 3 magic square and a 5 X 5 magic square are shown below:
8
3
4
1
5
9
6
7
2
Every row, column and diagonal in the 3 X 3 matrix adds up to 15.
17
23
4
10
11
24
5
6
12
18
1
7
13
19
25
8
14
20
21
2
15
16
22
3
9
Every row, column and diagonal in the 5 X 5 matrix adds up to 65.
The magic square program is quite challenging, and comes in two different levels of difficulty. It is very important that you
first are able to create magic squares, of any odd size, yourself on paper. On the next page is a five-step algorithm to
create an odd magic square. Study these steps carefully and use them to create magic squares on paper. Only after you
are confident that you understand the magic square algorithm, are you ready to proceed to write a program.
Magic Square Algorithm:
The creation of a magic square involves using the following algorithm. It will be your assignment to translate this algorithm
into a program.
[1]
Start number 1 in the top row, middle column. (HINT: To get median, divide “size” by 2.)
[2]
Place consecutive integers in a diagonally-up-to-the-right pattern. (HINT: Subtract 1 from row, add 1 to column).
[3]
Any number that goes outside the matrix above the top row is moved to the bottom row (still 1 column over ).
(HINT: Set row to size-1).
[4]
Any number that goes outside the matrix past the right column is moved to the leftmost column. (HINT: Set
column to 0.)
[5]
Any number, which follows the multiple of the matrix size, is moved down 1 row, same column as previous
number (HINT: Column stays the same, but subtract 1 from row.).
APCS
Lab 08d
Page 1
Lastname1stinitial_Lab08d.java
Start with the program below
// Hannag_Lab08d_MagicSquare.java
// The "Odd Magic Square" Program. This is the driver program that calls object methods in the MagicSquare class which is a separate
// Java file.
// This is the 100 Point Version.
import java.util.Scanner;
import java.text.DecimalFormat;
public class Hannag_Lab08d_MagicSquare
{
public static void main(String args[ ])
{
Scanner input = new Scanner(System.in);
System.out.print("Enter the odd# size of the Magic Square --> ");
int size = input.nextInt();
//Instantiate an object called “magic” of the MagicSquare class here, passing “size” to the constructor.
magic.computeMagicSquare();
magic.displayMagicSquare();
magic.checkRows();
magic.checkColumns();
magic.checkDiagonals();
System.out.println();
}//end of main
}//end of driver program
//SEPARATE JAVA FILE!!
public class MagicSquare
{
private int size;
private int magic[][];
private DecimalFormat output;
//Add Constructor method here. Initialize all 3 data attributes above in the constructor. Set size equal to size passed in and
//then use size to initialize rows & columns of the magic 2-dimensional array. Initialize the output object of the
//DecimalFormat class to “000”.
public void computeMagicSquare()
{
int j;
int c = size / 2; //Compute median column.
int r = 0;
magic[r][c] = 1; //Put 1 in the top row, middle column
//Add a “for” loop here to loop through numbers from 2 to size*size (e.g. 3*3=9). Use “if/else” and/or “if” statements to
//decide where to place each subsequent number.
}//end of computeMagicSquare
public void displayMagicSquare()
{
System.out.println();
System.out.println(size + "x" + size + " Magic Square");
System.out.println("==================");
System.out.println();
//Use nested “for” loops here to retrieve values in “magic” square.
APCS
Lab 08d
Page 2
}//end of displayMagicSquare
public void checkRows()
{
//Use nested “for” loops here to add values across rows together to verify the sums equal size*size, and print the values
//out with “+” signs and an “=” sign and the total sum per row. (See output.)
}//end of checkRows
public void checkColumns()
{
//Use nested “for” loops here to add values in columns together to verify the sums equal size*size, and print the values
//out with “+” signs and an “=” sign and the total sum per column. (See output.)
}//end of checkColumns
public void checkDiagonals()
{
//Use a “for” loop here to add values in diagonal from top-left to bottom-right (i.e. row=0 & column=0) together to
//verify the sum equals size*size, and print the values out with “+” signs and an “=” sign and the total sum. (See output.)
//Use a 2nd “for” loop (not nested!) here to add values in diagonal from top-right totop-left (i.e. row=0 & column=0)
//together to verify the sum equals size*size, and print the values out with “+” signs and an “=” sign and the total sum.
//(See output.)
System.out.println(" = " + sum);
}//end of checkDiagonals
}//end of magicSquare class
100 Point Version
The 100-point version enters the magic square size in a text window prompt. This should be a number in the [3..17]
integer range. It is not necessary to protect against erroneous input. (EXTRA CREDIT 10 points! See 110 Point Version
description beliw). Five MagicSquare class object methods are called from the main method in the driver program. The
MagicSquare Class is a separate Java file containing object (non-static) methods. In the MagicSquare Class:
1.) Complete computeMagicSquare, which places consecutive integers in the proper matrix locations, based on
the Magic Square Algorithm (see below). Important HINT: Place each consecutive number, starting at 1, in the
correct matrix location, not the other way around. Think what you do when you create a magic square on paper.
You start by placing number 1 in the right place, then number 2, number 3 and so on.
2.) Complete displayMagicSquare, which displays the values of the matrix. Numbers for all versions of this
assignment need to use a three digits "000" decimal format.
3.) Add 3 more methods. These methods are checkrows, checkColumns, and checkDiagonals. The
purpose of all 3 of these methods is to prove the magic square is truly a magic square by adding up and
displaying the sums of all of the numbers in every row, every column, and both diagonals. All of the sums should
be identical.
Magic Square Sum Rule
The sum of the rows, columns and diagonals in an odd
magic square will always be the size of the square times
the median number in the square.
In a 3 X 3 that means 3 * 5 = 15.
In a 5 X 5 that means 5 * 13 = 65.
APCS
Lab 08d
Page 3
These 3 methods also print out all the numbers in every row, every column, and both diagonals, including the
sum. See the sample output below:
110 Point Version (EXTRA CREDIT)
Put error processing in the main method of the driver program if the user enters a size that is not odd and is not 3 <= size
<=17. (HINT: while loop recommended for this error processing.)
APCS
Lab 08d
Page 4
Download