Results 1 to 3 of 3
Thread: Helppp ASAP please MAGIC SQUARE
 11202012, 06:15 PM #1Member
 Join Date
 Nov 2012
 Posts
 2
 Rep Power
 0
Helppp ASAP please MAGIC SQUARE
Hey I am doing a project in my CS class in which I have to use a 2d array and give of certain info about it such as if it is a magic square or not i need help with it.
The project 5 is based on your Lab 10, Lab 11 and Chapter 7.8 (TwoDimensional Arrays).
Requirement: you will create a twodimensional array which is 4 x 4 matrixes, load 16 numbers into this array,
display the 16 elements on the screen by a table format, check if same numbers were entered twice or not,
find Max and Minimum number, check if any number is greater than 16 or not, and test if this 4 x 4 matrix is a magic square,
which means that the sum of the elements in each raw, in each column, in the left diagonal, and in the right diagonal is the same value. For instance, the below is a magic square:
16 3 2 13
5 10 11 8
9 6 7 12
4 15 14 1
Task 1: launching the Eclipse program, click on “File” button on the toolbar, select “New Java Project”, and give Project5 as the project name.
Task 2: Right click on Project5 folder on the left column, select “New” and then “class”. Name the class as “Square”. Then click on “Finish”. A Java class called Square, which can be used to load numbers into a twodimensional array, print these numbers in a table format on the screen, check if same numbers were entered twice, find Max and Minimum number, check if any number is greater than 16, and test if this 4 x 4 matrix is a magic square, which means that if the sum of the elements in each raw, in each column, in the left diagonal, and in the right diagonals is the same value.
Part 1: Instance variables: (Make a private): a twodimensional array.
private int [] [] dataTable;
Part 2: Constructors: create one constructor to initialize row and col numbers :
public Square (int row, int col){
dataTable=new int [row][col];
}
Part 3: create seven public Methods:
1. 10 points. Create a method called loadData() which can read 16 numbers from the keyboard and load into the twodimensional array; Hint, please see your Lab 11.
2. 10 points. Create a method called printData() which can print 16 numbers from this array with a table format on the screen; Hint, please see your Lab 11.
3. 10 points. Create a method called findMaxNumber() which can find the maximum number in the array; Hint, please see your Lab 10.
4. 10 points. Create a method called findMinmumNumber() which can find the minimum number in the array; Hint, please see your Lab 10.
5. 10 points. Create a method called checkForNumberBeyond16() which can check how many numbers are greater than 16 or not;
6. 20 points. Create a method called checkDuplicatNumbers () which can check if same numbers were entered twice in this array;
7. 30 points. Create a method called isMagicSquare ( ) : see Lab 11; hint>
public boolean isMagicSquare()
{ … }
these are the instructions and this is what i have so far....
import java.util.Scanner;
public class Square {
private int[][] dataTable;
public Square(int row, int col) {
dataTable = new int[row][col];
}
public void loadData() {
Scanner input = new Scanner(System.in);
for (int i = 0; i < dataTable.length; i++) {
for (int j = 0; j < dataTable[0].length; j++) {
System.out.print("Enter a number");
dataTable[i][j] = input.nextInt();
}
}
}
public void printData() {
for (int i = 0; i < dataTable.length; i++) {
for (int j = 0; j < dataTable[0].length; j++) {
System.out.println(dataTable[i][j]);
}
}
}
public static double findMaxNumber(double[] dataTable) {
double highest = 0.0;
for (int i = 0; i < dataTable.length; i++) {
if (highest < dataTable[i])
highest = dataTable[i];
}
return highest;
}
public static double findMinmumNumber(double[] dataTable) {
double lowest = 100;
for (int i = 0; i < dataTable.length; i++) {
if (lowest > dataTable[i])
lowest = dataTable[i];
}
return lowest;
}
public static double checkForNumberBeyond16(double[] dataTable) {
double numb1 = 0.0;
for (int i = 0; i < dataTable.length; i++) {
if (numb1 > 16.0)
numb1 = numb1 + 1.0;
}
return numb1;
}
public double checkDuplicatNumbers() {
double dupNumbs=0.0;
for (int i = 0; i < dataTable.length; i++) {
for (int j = 0; j < dataTable[0].length; j++) {
if( )
}
}
public boolean isMagicSquare(){
}
}
as you can see i have work to be done witht eh duplicate methods and the isMagicSquare one..... any help would be appreciated..... and the isMagicSquare has to be a method not a class of its own.
 11202012, 06:17 PM #2Member
 Join Date
 Nov 2012
 Posts
 2
 Rep Power
 0
Re: Helppp ASAP please MAGIC SQUARE
i also have a tester class that uses this:
import java.util.Scanner;
public class Tester {
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
System.out.print("Please enter row number like 4: ");
int row = kb.nextInt();
System.out.print("Please enter column number like 4: ");
int col = kb.nextInt();
Square sq = new Square(row, col);
sq.loadData();
sq.printData();
System.out.println("The maximum number in this array is "
+ sq.findMaxNumber());
System.out.println("The minimum number in this array is "
+ sq.findMinmumNumber());
System.out.println(sq.checkForNumberBeyond16());
System.out.println(sq.checkDuplicatNumbers());
if (sq.isMagicSquare()) {
System.out.println("Good job! This is a magic square");
} else {
System.out
.println("Sorry, this is NOT a magic square because the rows, columns, and diagonals do not all have the same sum.");
}
}
}
 11202012, 06:25 PM #3Moderator
 Join Date
 Apr 2009
 Posts
 12,014
 Rep Power
 20
Similar Threads

Magic Square
By danthegreat in forum New To JavaReplies: 7Last Post: 10062011, 01:53 AM 
Help with Magic square program
By kkGG in forum New To JavaReplies: 2Last Post: 02162011, 04:10 PM 
Need Help with Magic Square
By easybe in forum New To JavaReplies: 10Last Post: 04232010, 09:39 PM 
Magic square
By gandalf5166 in forum New To JavaReplies: 20Last Post: 04152010, 07:18 PM 
Magic Square!!!... :D
By joms999 in forum New To JavaReplies: 4Last Post: 02252010, 07:55 AM
Bookmarks