Results 1 to 3 of 3
  1. #1
    nfurn is offline Member
    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0

    Angry 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];
    }

    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.

  2. #2
    nfurn is offline Member
    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0

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

    }

    }

    }

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,172
    Rep Power
    20

    Default 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.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Magic Square
    By danthegreat in forum New To Java
    Replies: 7
    Last Post: 10-06-2011, 02:53 AM
  2. Help with Magic square program
    By kkGG in forum New To Java
    Replies: 2
    Last Post: 02-16-2011, 05:10 PM
  3. Need Help with Magic Square
    By easybe in forum New To Java
    Replies: 10
    Last Post: 04-23-2010, 10:39 PM
  4. Magic square
    By gandalf5166 in forum New To Java
    Replies: 20
    Last Post: 04-15-2010, 08:18 PM
  5. Magic Square!!!... :D
    By joms999 in forum New To Java
    Replies: 4
    Last Post: 02-25-2010, 08:55 AM

Posting Permissions

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