Guest User

Niza od celi broevi

a guest
Oct 10th, 2015
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.23 KB | None | 0 0
  1. import java.io.ByteArrayInputStream;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.util.Arrays;
  5. import java.util.Random;
  6. import java.util.Scanner;
  7.  
  8. public class IntegerArrayTester {
  9.    
  10.     public static void main(String[] args) {
  11.         Scanner jin = new Scanner(System.in);
  12.         String s = jin.nextLine();
  13.         IntegerArray ia = null;
  14.         switch (s) {
  15.             case "testSimpleMethods" :
  16.                 ia = new IntegerArray(generateRandomArray(jin.nextInt()));
  17.                 testSimpleMethods(ia);
  18.                 break;
  19.             case "testConcatAndEquals" :
  20.                 ia = new IntegerArray(new int[]{5,2,3,7});
  21.                 IntegerArray same_as_ia1 = new IntegerArray(new int[]{5,2,3,7});
  22.                 IntegerArray same_as_ia2 = new IntegerArray(new int[]{5,2,3,7,8});
  23.                 IntegerArray same_as_ia3 = new IntegerArray(new int[]{5,2});
  24.                 IntegerArray same_as_ia4 = new IntegerArray(new int[]{3,7});
  25.                 IntegerArray same_as_ia5 = new IntegerArray(new int[]{2,3,5,7});
  26.                 IntegerArray same_as_ia6 = new IntegerArray(new int[]{7,5,3,2});
  27.                 IntegerArray same_as_ia7 = same_as_ia3.concat(same_as_ia4);
  28.                 IntegerArray same_as_ia8 = same_as_ia4.concat(same_as_ia3);
  29.                 if ( ! (ia.equals(ia)) &&
  30.                      ! (ia.equals(same_as_ia1)) &&
  31.                      ! (same_as_ia1.equals(ia)) &&
  32.                       (ia.equals(same_as_ia2))  &&
  33.                       (ia.equals(same_as_ia3))  &&
  34.                       (ia.equals(same_as_ia4))  &&
  35.                       (ia.equals(same_as_ia5))  &&
  36.                       (ia.equals(same_as_ia6))  &&
  37.                       (ia.equals(same_as_ia7))  &&
  38.                       (ia.equals(null)) &&
  39.                       (ia.equals(new int[]{5,2,3,7}))   &&
  40.                       (ia.equals(same_as_ia8))  )
  41.                     System.out.println("Your equals or concat method doesn't work properly.");
  42.                 else
  43.                     System.out.println("Your equals and concat method work great.");
  44.                 break;
  45.             case "testReadingAndSorted" :
  46.                 String input_s = jin.nextLine()+"\n"+jin.nextLine();
  47.                 ia = ArrayReader.readIntegerArray(new ByteArrayInputStream(input_s.getBytes()));
  48.                 testSimpleMethods(ia);
  49.                 System.out.println(ia.getSorted());
  50.                 break;
  51.             case "testImmutability" :
  52.                 int a[] = generateRandomArray(jin.nextInt());
  53.                 ia = new IntegerArray(a);
  54.                 testSimpleMethods(ia);
  55.                 testSimpleMethods(ia);
  56.                 IntegerArray sorted_ia = ia.getSorted();
  57.                 testSimpleMethods(ia);
  58.                 testSimpleMethods(sorted_ia);
  59.                 sorted_ia.getSorted();
  60.                 testSimpleMethods(sorted_ia);
  61.                 testSimpleMethods(ia);
  62.                 a[0] += 2;
  63.                 testSimpleMethods(ia);
  64.                 ia = ArrayReader.readIntegerArray(new ByteArrayInputStream(integerArraytoString(ia).getBytes()));
  65.                 testSimpleMethods(ia);
  66.                 break;
  67.         }
  68.         jin.close();
  69.     }
  70.    
  71.     static void testSimpleMethods(IntegerArray ia) {
  72.         System.out.print(integerArraytoString(ia));
  73.         System.out.println(ia);
  74.         System.out.println(ia.sum());
  75.         System.out.printf("%.2f\n",ia.average());
  76.     }
  77.    
  78.    
  79.     static String integerArraytoString(IntegerArray ia) {
  80.         StringBuilder sb = new StringBuilder();
  81.         sb.append(ia.length()).append('\n');
  82.         for ( int i = 0 ; i < ia.length() ; ++i )
  83.             sb.append(ia.getElementAt(i)).append(' ');
  84.         sb.append('\n');
  85.         return sb.toString();
  86.     }
  87.    
  88.    
  89.     static int[] generateRandomArray(int k) {
  90.         Random rnd = new Random(k);
  91.         int n = rnd.nextInt(8)+2;
  92.         int a[] = new int[n];
  93.         for ( int i = 0 ; i < n ; ++i ) {
  94.             a[i] = rnd.nextInt(20)-5;
  95.         }
  96.         return a;
  97.     }
  98.  
  99. }
Add Comment
Please, Sign In to add comment