Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By vitaliy1901

Thread: Java Task with matrix.

  1. #1
    vitaliy1901 is offline Member
    Join Date
    Sep 2012
    Posts
    5
    Rep Power
    0

    Question Java Task with matrix.

    Hi, every one! Please help me with my task. I don't know which way to think. Thanks!!!
    Given a matrix of elements equal to 0 or 1. Units in such a matrix form
    domains, and the domain contains only the neighboring elements in the horizontal and
    vertical. The program should determine the number of domains in a given matrix.
    input
    A string containing the path to the file containing the matrix.
    Example of a matrix:
    1 0 0 0 1 0
    1 1 0 0 0 1
    0 0 0 0 0 0
    0 0 0 0 0 1
    0 0 0 0 0 1
    This matrix can be determined 4 different domain:
    {(1,1), (2,1), (2,2)}, {(1,5)}, {(2,6)}, {(4,6), (5,6)}.

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: Java Task with matrix.

    Hi, every one! Please help me with my task. I don't know which way to think
    ...and? What have you tried? Where exactly are you stuck? Contributors won't do this for you, but will point you in the right direction. Without knowing the answers to those question, we have no clue which direction to point

  3. #3
    vitaliy1901 is offline Member
    Join Date
    Sep 2012
    Posts
    5
    Rep Power
    0

    Default Re: Java Task with matrix.

    Hi, every one! I written code for reading this maxrix from file and displaying all coordinats of ones in matrix. But when I try display only ones without the neighboring elements in the horizontal and vertical:
    1 0 0 0 1 0
    1 1 0 0 0 1
    0 0 0 0 0 0
    0 0 0 0 0 1
    0 0 0 0 0 1
    And I have got exception - ArrayIndexOutOfBoundsException, because I exceed the length of the array. How to fix it? Thanks!

    Java Code:
    public class Matrix {
    
        public static int a;
    
        public static void main(String[] args) throws IOException {
    
            File file = new File("src\\array.txt");
            int row = 0;
            int col = 0;
            int[][] data = new int[5][6];
            BufferedReader bufRdr = new BufferedReader(new FileReader(file));
            String line = null;
    
            while ((line = bufRdr.readLine()) != null && row < data.length) {
                StringTokenizer st = new StringTokenizer(line, " ");
                while (st.hasMoreTokens()) {
                    data[row][col] = Integer.parseInt(st.nextToken());
                    col++;
                }
                col = 0;
                row++;
            };
    
            for (int i = 0; i < data.length; i++) {
                for (int j = 0; j < data[i].length; j++) {
                    int nextRow = i + 1;
                    int nextCol = j + 1;
                    if (data[i][j] == 1 && data[nextRow][j]!=1 && data[i][nextCol]!=1) {
                        System.out.println("(" + i + "," + j + ")");
                    }
                }
            };
        };
    
    };
    Last edited by vitaliy1901; 10-25-2012 at 12:32 PM.

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,679
    Blog Entries
    7
    Rep Power
    21

    Default Re: Java Task with matrix.

    A flood fill mechanism can do the job; i.e. if a point (x,y) is part of a domain, recursively check its neighbouring points. Check for array bounds when finding the neighbours. The stop criterium is easy: if (x,y) == 0, it is not part of the domain under construction, else if (x,y) == 1 and it is already part of the domain under construction, then stop, else add (x,y) to the new domain and recursively check its neighbours again.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    vitaliy1901 is offline Member
    Join Date
    Sep 2012
    Posts
    5
    Rep Power
    0

    Default Re: Java Task with matrix.

    Hi Jos! Thanks for the help, It's work.
    Java Code:
    public class Matrix {
    
        private static int quantity;
    
        public static void main(String[] args) throws IOException {
    
            for (int i = 0; i < matrix().length - 1; i++) {
                for (int j = 0; j < matrix()[i].length - 1; j++) {
    
                    int nextRow = i + 1;
                    int nextCol = j + 1;
                    int nextNextRow = i + 2;
                    int nextNextCol = j + 2;
                    boolean three0111 = matrix()[i][j] != 1 && matrix()[i][nextCol] == 1 && matrix()[nextRow][j] == 1 && matrix()[nextRow][nextCol] == 1;
                    boolean three1011 = matrix()[i][j] == 1 && matrix()[i][nextCol] != 1 && matrix()[nextRow][j] == 1 && matrix()[nextRow][nextCol] == 1;
                    boolean three1101 = matrix()[i][j] == 1 && matrix()[i][nextCol] == 1 && matrix()[nextRow][j] != 1 && matrix()[nextRow][nextCol] == 1;
                    boolean three1110 = matrix()[i][j] == 1 && matrix()[i][nextCol] == 1 && matrix()[nextRow][j] == 1 && matrix()[nextRow][nextCol] != 1;
                    boolean threeRight1110 = matrix()[i][j] == 1 && matrix()[i][nextCol] == 1 && matrix()[i][nextNextCol] == 1 && matrix()[nextRow][j] != 1;
                    boolean threeDown1110 = matrix()[i][j] == 1 && matrix()[nextRow][j] == 1 && matrix()[nextNextRow][j] == 1 && matrix()[i][nextCol] != 1;
    
                    boolean one0100 = matrix()[i][j] != 1 && matrix()[i][nextCol] == 1 && matrix()[nextRow][j] != 1 && matrix()[nextRow][nextCol] != 1;
                    boolean one0110 = matrix()[i][j] != 1 && matrix()[i][nextCol] == 1 && matrix()[nextRow][j] == 1 && matrix()[nextRow][nextCol] != 1;
    
                    boolean two0101 = matrix()[i][j] != 1 && matrix()[i][nextCol] == 1 && matrix()[nextRow][j] != 1 && matrix()[nextRow][nextCol] == 1;
                    boolean two1010 = matrix()[i][j] == 1 && matrix()[i][nextCol] != 1 && matrix()[nextRow][j] == 1 && matrix()[nextRow][nextCol] != 1;
                    boolean two0011 = matrix()[i][j] != 1 && matrix()[i][nextCol] != 1 && matrix()[nextRow][j] == 1 && matrix()[nextRow][nextCol] == 1;
    
                    if (three0111) {
                        System.out.print("(" + i + "," + j + ")");
                        System.out.print("(" + nextRow + "," + nextCol + ")");
                        System.out.print("(" + nextRow + "," + j + ")");
                        System.out.print(" || ");
                        quantity++;
                    } else if (three1011) {
                        System.out.print("(" + i + "," + j + ")");
                        System.out.print("(" + nextRow + "," + nextCol + ")");
                        System.out.print("(" + nextRow + "," + j + ")");
                        System.out.print(" || ");
                        quantity++;
                    } else if (three1101) {
                        System.out.print("(" + i + "," + j + ")");
                        System.out.print("(" + nextRow + "," + nextCol + ")");
                        System.out.print("(" + nextRow + "," + j + ")");
                        System.out.print(" || ");
                        quantity++;
                    } else if (three1110) {
                        System.out.print("(" + i + "," + j + ")");
                        System.out.print("(" + nextRow + "," + nextCol + ")");
                        System.out.print("(" + nextRow + "," + j + ")");
                        System.out.print(" || ");
                        quantity++;
                    } else if (threeRight1110) {
                        System.out.print("(" + i + "," + j + ")");
                        System.out.print("(" + i + "," + nextCol + ")");
                        System.out.print("(" + i + "," + nextNextCol + ")");
                        System.out.print(" || ");
                        quantity++;
                    } else if (threeDown1110) {
                        System.out.print("(" + i + "," + j + ")");
                        System.out.print("(" + nextRow + "," + j + ")");
                        System.out.print("(" + nextNextRow + "," + j + ")");
                        System.out.print(" || ");
                        quantity++;
                    } else if (one0100) {
                        System.out.print("(" + i + "," + nextCol + ")");
                        System.out.print(" || ");
                        quantity++;
                    }else if (one0110) {
                        System.out.print("(" + i + "," + nextCol + ")");
                        System.out.print(" || ");
                        quantity++;
                    } else if (two0101) {
                        System.out.print("(" + i + "," + nextCol + ")");
                        System.out.print("(" + nextRow + "," + nextCol + ")");
                        quantity++;
                    } else if (two1010) {
                        System.out.print("(" + i + "," + j + ")");
                        System.out.print("(" + nextRow + "," + j + ")");
                        quantity++;
                    }else if (two0011) {
                        System.out.print("(" + nextRow + "," + j + ")");
                        System.out.print("(" + nextRow + "," + nextCol + ")");
                        quantity++;
                    }
                }
            }
    
            System.out.println("\nQuantity of domains is: " + quantity);
        }
    
        ;
    
        public static int[][] matrix() throws IOException {
            File file = new File("src\\array.txt");
            int row = 0;
            int col = 0;
            int[][] data = new int[6][6];
            BufferedReader bufRdr = new BufferedReader(new FileReader(file));
            String line;
    
            while ((line = bufRdr.readLine()) != null && row < data.length) {
                StringTokenizer st = new StringTokenizer(line, " ");
                while (st.hasMoreTokens()) {
                    data[row][col] = Integer.parseInt(st.nextToken());
                    col++;
                }
                col = 0;
                row++;
            }
            ;
            return data;
        }
    
    };
    DarrylBurke likes this.

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,679
    Blog Entries
    7
    Rep Power
    21

    Default Re: Java Task with matrix.

    Quote Originally Posted by vitaliy1901 View Post
    Hi Jos! Thanks for the help, It's work.
    I'm glad it works for you but I don't understand a thing of your code; I don't see any recursive calls so it definitely isn't a flood fill mechanism ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. java matrix liberary
    By amrmb09 in forum Advanced Java
    Replies: 7
    Last Post: 12-01-2011, 02:22 PM
  2. Sparse matrix in java
    By Zerpol in forum New To Java
    Replies: 2
    Last Post: 12-15-2010, 01:35 PM
  3. JAVA task.
    By DEA in forum New To Java
    Replies: 2
    Last Post: 04-08-2010, 10:39 PM
  4. Java Task
    By Sokox in forum NetBeans
    Replies: 4
    Last Post: 12-13-2009, 10:57 AM
  5. java-task.com
    By marcellis in forum Introductions
    Replies: 0
    Last Post: 09-28-2009, 11:46 AM

Tags for this Thread

Posting Permissions

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