Advertisement
mmayoub

functions, exercise 02, slide 35

Jul 4th, 2017
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.71 KB | None | 0 0
  1. package class170703;
  2.  
  3. import java.util.Random;
  4.  
  5. public class functions02 {
  6.     // Functions, exercise 02, slide 35
  7.     // input: two arrays of integer numbers
  8.     // output: array of the common numbers (intersection)
  9.  
  10.     // building a data sample: two arrays of integers
  11.     // then calling function intersection to calculate
  12.     // and print results
  13.     public static void main(String[] args) {
  14.         // sample data for testing only
  15.         int[] array1 = filledArray(20, 1, 50); // create and fill an array with
  16.                                                 // 20 numbers in the range 1 to
  17.                                                 // 50
  18.         int[] array2 = filledArray(20, 1, 50); // create and fill an array
  19.                                                 // with 20 numbers in the
  20.                                                 // range 1 to 50
  21.  
  22.         // get intersection array with repeated values not allowsed
  23.         int[] resultArrayNoRepaeat = intersection(array1, array2, false);
  24.  
  25.         // get intersection array with repeated values not allowsed
  26.         int[] resultArrayRepaeatAllowed = intersection(array1, array2, true);
  27.  
  28.         // print to screen
  29.         System.out.printf("first array  : %s\n", toString(array1));
  30.         System.out.printf("second array : %s\n", toString(array2));
  31.  
  32.         System.out.printf("\nrepeated values are allowed intersection : %s \n",
  33.                 toString(resultArrayRepaeatAllowed));
  34.         System.out.printf("\nrepeated values not allowed intersection : %s \n",
  35.                 toString(resultArrayNoRepaeat));
  36.     }
  37.  
  38.     // create and fill an array with random integer numbers
  39.     public static int[] filledArray(int size, int minValue, int maxValue) {
  40.         // creating random object
  41.         Random rnd = new Random();
  42.  
  43.         // allocate a new array
  44.         int[] resultArray = new int[size];
  45.  
  46.         for (int i = 0; i < resultArray.length; i += 1) {
  47.             resultArray[i] = minValue + rnd.nextInt(maxValue - minValue + 1);
  48.         }
  49.  
  50.         return resultArray;
  51.     }
  52.  
  53.     // find the intersection of two integer arrays
  54.     // input: two integer arrays
  55.     // output: an array containing the intersection the given arrays
  56.     private static int[] intersection(int[] array1, int[] array2,
  57.             boolean repeatAllowed) {
  58.         // optional: to reduce function calls, array1 should be the smaller
  59.         if (array1.length - array2.length > 10) // for example
  60.             return intersection(array2, array1, repeatAllowed);
  61.  
  62.         // initialize an array for the result
  63.         int[] resultArray = {};
  64.  
  65.         // for each number in array1
  66.         for (int aNumber : array1) {
  67.             // if it already exists in result array also
  68.             if (isArrayContains(resultArray, aNumber)) {
  69.                 // if repeated values are allowed
  70.                 if (repeatAllowed) {
  71.                     // add this number to the result array
  72.                     resultArray = addToArray(resultArray, aNumber);
  73.                 }
  74.             } else
  75.             // if it exists in array2 also
  76.             if (isArrayContains(array2, aNumber)) {
  77.                 // add this number to the result array
  78.                 resultArray = addToArray(resultArray, aNumber);
  79.             }
  80.  
  81.         }
  82.  
  83.         // return the array of intersection result
  84.         return resultArray;
  85.     }
  86.  
  87.     // check if a number exists in an array
  88.     // input: array of integers and an integer number
  89.     // output: true if the number exists in the array, otherwise false
  90.     private static boolean isArrayContains(int[] array, int aNumber) {
  91.         // for each number in array
  92.         for (int num : array) {
  93.             // if it is equal to the number to be searched
  94.             if (num == aNumber) {
  95.                 // return true
  96.                 return true;
  97.             }
  98.         }
  99.         // not exist in the array, return false
  100.         return false;
  101.     }
  102.  
  103.     // append a number to an array
  104.     // input: array of integers and an integer number
  105.     // output:
  106.     // array that contains the given array with the given number appended
  107.     private static int[] addToArray(int[] array, int aNumber) {
  108.         // determine the size of the given array
  109.         int oldSize = array == null ? 0 : array.length;
  110.  
  111.         // define a new array with one more cell than the given array
  112.         int[] newArray = new int[oldSize + 1];
  113.  
  114.         // copy the numbers from the given array to the new one
  115.         for (int i = 0; i < oldSize; i += 1) {
  116.             newArray[i] = array[i];
  117.         }
  118.  
  119.         // add the given number to empty cell at the end of the new array
  120.         newArray[oldSize] = aNumber;
  121.  
  122.         // return the new array
  123.         return newArray;
  124.     }
  125.  
  126.     // convert an array of integer numbers to a string
  127.     // input: an array of integer values
  128.     // output: a string with all values from the array separated by comma
  129.     public static String toString(int[] array) {
  130.         // if array is empty
  131.         if (array.length == 0) {
  132.             // return result and quit
  133.             return "[]";
  134.         }
  135.  
  136.         // building a string from all numbers in the array
  137.         String result = "" + array[0]; // start with the first number
  138.  
  139.         // scan the numbers in the array without the first
  140.         for (int i = 1; i < array.length; i += 1) {
  141.             // convert number to string and add it to the result string
  142.             result += ", " + array[i];
  143.         }
  144.  
  145.         // return the result surrounded by brackets
  146.         return "[" + result + "]";
  147.     }
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement