1. Member 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 (Two-Dimensional Arrays).
Requirement: you will create a two-dimensional 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 two-dimensional 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 two-dimensional 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 two-dimensional 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];
}

Scanner input = new Scanner(System.in);
for (int i = 0; i < dataTable.length; i++) {
for (int j = 0; j < dataTable.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.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.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.  Reply With Quote

2. Member 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.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.");

}

}

}  Reply With Quote

3. Moderator   Join Date
Apr 2009
Posts
13,541
Rep Power
27

## Re: Helppp ASAP please MAGIC SQUARE

Please use [code] tags [/code] when posting code.
Many people won't bother reading unformatted code.

This will help whoever has time to look at this...I'm off for dinner.  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•