Advertisement
eranseg

Functions

Aug 18th, 2019
315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.05 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Functions {
  4.     // ------------------ EX1 -----------------------------
  5.     /*
  6.     public static void main(String[] args) {
  7.         Scanner sc = new Scanner(System.in);
  8.         // declare values
  9.         int number, digit, len;
  10.         int[] nArr;
  11.         // Part 1 of ex1
  12.         System.out.println("Please enter a large number and a digit: ");
  13.         number = sc.nextInt();
  14.         digit = sc.nextInt();
  15.         System.out.printf("The digit %d appears %d times in the number %d\n", digit, checkNum(number, digit), number);
  16.         System.out.println("\n---------------------------------------");
  17.         // Part 2 of ex1 - value from array
  18.         System.out.println("Please enter the size of the numbers array and a digit: ");
  19.         len = sc.nextInt();
  20.         digit = sc.nextInt();
  21.         nArr = new int[len];
  22.         initArray(nArr);
  23.  
  24.         System.out.printf("The digit %d appears %d times in the given number array", digit, checkNumInArr(nArr, digit));
  25.     }
  26.  
  27.     public static void initArray(int[] arr) {
  28.         System.out.printf("Please enter %d integers: ", arr.length);
  29.         Scanner s = new Scanner(System.in);
  30.         for(int i = 0; i < arr.length; i++) {
  31.             arr[i] = s.nextInt();
  32.         }
  33.     }
  34.  
  35.     public static int checkNum(int num, int d) {
  36.         int n = 0;
  37.         do {
  38.             if(num % 10 == d) {
  39.                 n += 1;
  40.             }
  41.             num /= 10;
  42.         } while(num != 0);
  43.         return n;
  44.     }
  45.  
  46.     public static int checkNumInArr(int[] numArr, int d) {
  47.         int sum = 0;
  48.         for(int i = 0; i < numArr.length; i += 1) {
  49.             sum += checkNum(numArr[i], d);
  50.         }
  51.         return sum;
  52.     }
  53.     */
  54.  
  55.     // ---------------------- EX2 ------------------------
  56.     public static void main(String[] args) {
  57.         Scanner sc = new Scanner(System.in);
  58.         //Initializing arrays
  59.         int[] arr1 = {87, 13, 12, 56, 32}, arr2 = {19, 87, 13, 21, 56, 32}, res, util;
  60.         int resLen = arr1.length <= arr2.length ? arr1.length : arr2.length;
  61.         util = new int[resLen];
  62.         // Since the length is unknown the 'checkDuplicates' function
  63.         // copies the duplicate values to the util array and return
  64.         // the number of duplicate numbers in the given arrays
  65.         int len = checkDuplicates(arr1, arr2, util);
  66.         // The res array is initialized with the correct length
  67.         res = new int[len];
  68.         // copyToResultArray function copies the duplicate values to the result array
  69.         copyToResultArray(util, res);
  70.         // printResult displays the duplicate values to the screen
  71.         printResult(res);
  72.     }
  73.  
  74.     /**
  75.      * The checkDuplicates function gets 3 arrays as an input. The function checks for duplicate numbers
  76.      * in the first two arrays and copies them to the third array (The third array's is initialized
  77.      * in accordance with the shortest array.
  78.      * The function returns the number of duplicates found between the first two arrays
  79.      */
  80.     public static int checkDuplicates(int[] a1, int[] a2, int[] r) {
  81.         int l = 0, index = 0;
  82.         for(int i = 0; i < a1.length; i += 1) {
  83.             for(int j = 0; j < a2.length; j += 1) {
  84.                 if(a1[i] == a2[j]) {
  85.                     r[index++] = a1[i];
  86.                     l += 1;
  87.                 }
  88.             }
  89.         }
  90.         return l;
  91.     }
  92.  
  93.     /**
  94.      * A function for copying the values from the first array to the second given array with the right length
  95.      */
  96.     public static void copyToResultArray(int[] u, int[] r) {
  97.         for(int i = 0; i < r.length; i++) {
  98.             r[i] = u[i];
  99.         }
  100.     }
  101.  
  102.     /**
  103.      * A utility function for printing the values from the result array to the terminal / command line
  104.      */
  105.     public static void printResult(int[] resArr) {
  106.         System.out.print("[");
  107.         for(int n = 0; n < resArr.length - 1; n += 1) {
  108.             System.out.print(resArr[n] + ", ");
  109.         }
  110.         System.out.printf("%d]", resArr[resArr.length - 1]);
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement