Results 1 to 2 of 2
  1. #1
    ssunlimited is offline Member
    Join Date
    Jul 2013
    Posts
    10
    Rep Power
    0

    Default I don't get this code. I would appreciate an explanation for this

    Please help me understand this code:

    Java Code:
    public class TwoDimMethods
    {
        static final int rows = 6; //this can be set to any number
        static final int columns = 6; //this can be set to any number
    
        public static void main(String[] args)
        {
            int[][] board ={{23,5,6,15,18,3},
                            {4,16,24,67,10,6},
                            {12,54,23,76,11,22},
                            {1,12,34,22,8,65},
                            {81,54,32,67,33,12}
    								 ,{56,22,90,22,-6,63}};
    		 int[ ] onedim = new int[36];
    		     printMatrix(board);
    		  largestinRows(board);
            largestinColumns(board);
    
    
               swapper(board, onedim,1<2);
    		 
               sortit(onedim);
    		 
               swapper(board, onedim, 1>2);
    		  printMatrix(board);
    
            sumRows(board);
            sumColumns(board);
       
            reverseDiagonal(board);
            System.out.println("After Reversing Diagonal the board");
            printMatrix(board);
        }
    	 public static void sortit(int [] single){
          int temp;
           for(int i=0; i< single.length-1 ; i++)
            for(int j=0; j< single.length-1-i ; j++){
    
              if( single[j]> single[j+1]) {
                      temp = single[j];
                      single[j]= single[j+1];
                      single[j+1] =temp;}
          }}
        public static void swapper(int[][] matrix, int [] single, boolean t)
        {
            int row, col, pos=0;
    
            for(row = 0; row < matrix.length; row++)
            {
                for(col = 0; col < matrix[row].length; col++){
                   if(t) single[pos++]= matrix[row][col] ;
                     else matrix[row][col]= single[pos++]; }
    					                    }
        }
    
        public static void printMatrix(int[][] matrix)
        {
            int row, col;
    
            for(row = 0; row < matrix.length; row++)
            {
                for(col = 0; col < matrix[row].length; col++)
                    System.out.print(matrix[row][col] + "\t");
                System.out.println();
            }
        }
    
        public static void sumRows(int[][] matrix)
        {
            int row, col;
            int sum;
    
                    //sum of each individual row
            for(row = 0; row < matrix.length; row++)
            {
                sum = 0;
                for(col = 0; col < matrix[row].length; col++)
                    sum = sum + matrix[row][col];
                System.out.println("Sum of row " + (row+1) + " = " + sum);
            }
        }
    
        public static void sumColumns(int[][] matrix)
        {
            int row, col;
            int sum;
    
                  //sum of each individual column
            for(col = 0; col < matrix[0].length; col++)
            {
                sum = 0;
                for(row = 0; row < matrix.length; row++)
                    sum = sum + matrix[row][col];
                System.out.println("Sum of column " + (col+1) + " = " + sum);
            }
        }
    
        public static void largestinRows(int[][] matrix)
        {
            int row, col;
            int largest;
    
                    //Largest element in each row
            for(row = 0; row < matrix.length; row++)
            {
                largest = matrix[row][0]; //assume that the first element of
                                          //the row is largest
                for(col = 1; col < matrix[row].length; col++)
                    if(largest < matrix[row][col])
                        largest = matrix[row][col];
                System.out.println("Largest element of row " + (row+1) + " = "
                                 + largest);
            }
        }
    
        public static void largestinColumns(int[][] matrix)
        {
            int row, col;
            int largest;
    
                      //Largest element in each column
            for(col = 0; col < matrix[0].length; col++)
            {
                largest = matrix[0][col]; //assume the the first element of
                                      //the column is largest
                for(row = 1; row < matrix.length; row++)
                    if(largest < matrix[row][col])
                        largest = matrix[row][col];
                System.out.println("Largest element of col " + (col+1) + " = "
                                  + largest);
            }
        }
    
        public static void reverseDiagonal(int[][] matrix)
        {
            int row;
            int temp;
    
                    //Reverse main diagonal
            for(row = 0; row < matrix.length / 2; row++)
            {
                temp = matrix[row][row];
                matrix[row][row] = matrix[rows-1-row][rows-1-row];
                matrix[rows-1-row][rows-1-row] = temp;
            }
    
                    //Reverse the opposite diagonal
            for(row = 0; row < matrix.length / 2; row++)
            {
                temp = matrix[row][rows-1-row];
                matrix[row][rows-1-row] = matrix[rows-1-row][row];
                matrix[rows-1-row][row] = temp;
            }
        }
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: I don't get this code. I would appreciate an explanation for this

    Do you have any specific questions about some lines of code? Please post the question and say which lines you have questions about.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Explanation of Java code
    By ubuntu_jdk in forum AWT / Swing
    Replies: 3
    Last Post: 05-29-2013, 09:59 PM
  2. Explanation of Java Zip code
    By joshhazel in forum New To Java
    Replies: 0
    Last Post: 01-25-2012, 02:59 AM
  3. some code explanation please?
    By javawreker in forum New To Java
    Replies: 14
    Last Post: 12-31-2011, 06:05 PM
  4. Need a java code and explanation for this
    By Crossover in forum New To Java
    Replies: 4
    Last Post: 09-21-2010, 01:23 AM
  5. need code explanation
    By marie in forum New To Java
    Replies: 3
    Last Post: 03-05-2010, 12:41 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
  •