Goodiny777

Java_Functions_exr_1-2

Jan 3rd, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.93 KB | None | 0 0
  1. import java.util.Scanner;
  2. public class MatsegetFunctions {
  3.     // targil 1.1
  4.     public static int numberAppears(int current_num, int num_to_find ) {
  5.         int counter = 0, temp_digit;
  6.         while (current_num>0){
  7.             temp_digit = current_num%10;
  8.             if(temp_digit == num_to_find){
  9.                 counter+=1;
  10.             }
  11.             current_num/=10;
  12.         }
  13.         return counter;
  14.     }
  15.  
  16.     //targil 1.2 בשביל לפתור חלק השני של תרגיל ראשון אפשר להעזר בפונקציה שכבר כתבנו בחלק ראשון של תרגיל הזה
  17.     public static int digitInArray(int[] temp_array, int numb_to_find ) {
  18.         int counter = 0;
  19.         for (int i=0; i<temp_array.length; i+=1) {
  20.             counter += numberAppears(temp_array[i], numb_to_find);
  21.         }
  22.         return counter;
  23.     }
  24.  
  25.     //targil 2
  26.     public static int [] sameChains(int [] first_arr, int [] second_arr) {
  27.         int current_place = 0;
  28.         int[] temp_arr = new int[first_arr.length]; //Declaration of array to return
  29.         for (int i = 0; i < first_arr.length; i += 1) {
  30.             for (int j = 0; j < second_arr.length; j += 1) {
  31.                 if (first_arr[i]==second_arr[j]){
  32.                     temp_arr[current_place] = first_arr[i];
  33.                     current_place+=1;
  34.                 }
  35.             }
  36.         }
  37.         return temp_arr;
  38.     }
  39.  
  40.     public static void main(String[] args) {
  41.         Scanner sc = new Scanner(System.in);
  42.  
  43.         //קליטת נתונים לתרגיל 1
  44.         int big_num, digit;
  45.         int [] arr1 = new int [10];
  46.         System.out.println("Enter a big number and a digit: ");
  47.         big_num=sc.nextInt(); //  קליטת מספר גדול בשביל חלק ראשון של התרגיל
  48.         digit=sc.nextInt();  // קליטת סיפרה בשביל חלק ראשון וגם כן חלק שני של התרגיל
  49.         System.out.printf("The number %d appears %d times\n", digit, numberAppears(big_num,digit));
  50.         System.out.printf("Enter %d numbers to array: \n",arr1.length);
  51.         for (int i=0;i<arr1.length;i+=1){
  52.             arr1[i] = sc.nextInt();
  53.         }
  54.         System.out.printf("This number %d appears %d times\n", digit, digitInArray(arr1,digit));
  55.  
  56.         //קליטת נתונים לתרגיל 2
  57.         int [] result_arr , arr2 = new int [5], arr3 = new int [5];
  58.         System.out.printf("Enter %d numbers to the first array : \n",arr2.length);
  59.         for (int i=0; i<arr2.length; i+=1){
  60.             arr2[i]=sc.nextInt();
  61.         }
  62.         System.out.printf("Enter %d numbers to the second array : \n",arr3.length);
  63.         for (int i=0; i<arr3.length;i+=1){
  64.             arr3[i]=sc.nextInt();
  65.         }
  66.         result_arr = sameChains(arr2, arr3); // שימוש בפונקציה
  67.         for (int i:result_arr) {
  68.             if (i!=0) {
  69.                 System.out.print(i + " ");
  70.             }
  71.         }
  72.  
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment