Advertisement
Guest User

ALLLLLEEEEEEXXXXXXXXXXX

a guest
Mar 6th, 2015
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.16 KB | None | 0 0
  1. import java.util.Random;
  2.  
  3. public class Lab9
  4. {
  5.    
  6.     public static void main(String[] args)
  7.     {
  8.         int[] [] testArrayOne = random(5, 0, 5);
  9.         int[] [] testArrayTwo = random(10, 2, 10);
  10.         int[] [] testArrayThree = random(3, 0, 3);
  11.         int[] [] testArrayFour = random(15, 0, 55);
  12.         int[] [] testArrayFive = random(25, 0, 25);
  13.         int[][] allneg = { {-10,-12,-3}, {-4,-5,-6,-8}, {-7,-8} };
  14.         int[][] nonsquare = { {1,2,3}, {4,5}, {6,7,8,9} };
  15.         int[][] latin = { {1,2,3}, {2,3,1}, {3,1,2} };
  16.         int[][] notlatin = { {2,1,3}, {2,3,1}, {3,1,2} };
  17.        
  18.         testArray(testArrayOne);
  19.         testArray(testArrayTwo);
  20.         testArray(testArrayThree);
  21.         testArray(testArrayFour);
  22.         testArray(testArrayFive);
  23.         testArray(allneg);
  24.         testArray(nonsquare);
  25.         testArray(latin);
  26.         testArray(notlatin);
  27.     }
  28.    
  29.     private static void testArray(int [] [] arrayTested)
  30.     {
  31.         System.out.println("The sum of the first row is " + rowSum(arrayTested, 0));
  32.         System.out.println("The sum of the first column is " + columnSum(arrayTested, 0));
  33.         System.out.println("Is the array a square? : " + isSquare(arrayTested));
  34.         System.out.println("Is the array a latin square? : " + isLatin(arrayTested));
  35.     }
  36.    
  37.     public static int [] [] random(int N, int start, int end)
  38.     {
  39.         int [] [] returnArray = new int[N] [N];
  40.         for(int i = 0; i < N; i++)
  41.         {
  42.             for(int j = 0; j < N; j++)
  43.             {
  44.                 returnArray[i][j] = randInt(start, end);
  45.             }
  46.         }
  47.         return returnArray;
  48.     }
  49.    
  50.     private static int randInt(int min, int max)
  51.     {
  52.         Random rand = new Random();
  53.         int randomNum = rand.nextInt((max - min) + 1) + min;
  54.         return randomNum;
  55.     }
  56.    
  57.     public static int rowSum(int [] [] arrayOne, int rowNum)
  58.     {
  59.         int sum = 0;
  60.         for(int i = 0; i < arrayOne.length; i++)
  61.         {
  62.             sum += arrayOne[i][rowNum];
  63.         }
  64.         return sum;
  65.     }
  66.    
  67.     public static int columnSum(int [] [] arrayOne, int colNum)
  68.     {
  69.         int sum = 0;
  70.         for(int i = 0; i< arrayOne[colNum].length; i++)
  71.         {
  72.             sum+= arrayOne[colNum][i];
  73.         }
  74.         return sum;
  75.     }
  76.    
  77.     public static boolean isSquare(int [] [] arrayOne)
  78.     {
  79.         boolean squareArray = true;
  80.         for(int i = 0; i < arrayOne.length; i++)
  81.         {
  82.             if(arrayOne.length != arrayOne[i].length)
  83.             {
  84.                 squareArray = false;
  85.             }
  86.         }
  87.         return squareArray;
  88.     }
  89.    
  90.     public static boolean isLatin(int [] [] arrayOne)
  91.     {
  92.         boolean isLatin = true;
  93.         int checkSum = 0;
  94.         if(isSquare(arrayOne))
  95.         {
  96.             for(int i = arrayOne.length; i > 0; i--)
  97.             {
  98.                 checkSum += i;
  99.             }
  100.            
  101.             for(int i = 0; i < arrayOne.length; i++)
  102.             {
  103.                 int sumArrayNums = 0;
  104.                 if(isLatin == true)
  105.                 {
  106.                     for(int j = 0; j < arrayOne.length; j++)
  107.                     {
  108.                         sumArrayNums += (arrayOne[i][j]);
  109.                     }
  110.                     if(sumArrayNums != checkSum)
  111.                     {
  112.                         isLatin = false;
  113.                     }
  114.                 }
  115.             }
  116.            
  117.             if(isLatin == true)
  118.             {
  119.                 for(int i = 0; i < arrayOne.length; i++)
  120.                 {
  121.                     int sumArrayNums = 0;
  122.                     if(isLatin == true)
  123.                     {
  124.                         for(int j = 0; j < arrayOne.length; j++)
  125.                         {
  126.                             sumArrayNums += arrayOne[j][i];
  127.                         }
  128.                         if(sumArrayNums != checkSum)
  129.                         {
  130.                             isLatin = false;
  131.                         }
  132.                     }
  133.                 }
  134.             }
  135.         }
  136.         else
  137.         {
  138.             isLatin = false;
  139.         }
  140.         return isLatin;
  141.     }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement