Advertisement
RexyBadDog

Lesson009_18_08_19_Functions

Aug 20th, 2019
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.33 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Lesson009_18_08_19_HomeWork2 {
  4.     private static Scanner sc = new Scanner(System.in);
  5.  
  6.     public static void main(String[] args) {
  7.         int option = classWorkMenu();
  8.         while (option != 0) {       // while the user didn't put '0', keep pop-up the menu!
  9.             switch (option) {
  10.                 case 11:
  11.                     option = task1_1();
  12.                     break;
  13.                 case 12:
  14.                     option = task1_2();
  15.                     break;
  16.                 case 2:
  17.                     option = task2();
  18.                     break;
  19.                 default:
  20.                     System.out.println(option + " is invalid option! try again...\n");
  21.                     option = classWorkMenu();
  22.             }
  23.         }
  24.     }
  25.  
  26.     public static int classWorkMenu() {
  27.         System.out.print("\n...::: MENU :::...\n" +       // A menu for class tasks
  28.                 "From presentation '004_18-08-19 functions' Page 34:\n" +
  29.                 "press [11] for task 1.1\n" +
  30.                 "press [12] for task 1.2\n" +
  31.                 "From presentation '004_18-08-19 functions' Page 35:\n" +
  32.                 "press [2] for task 2\n" +
  33.                 "press [0] to end this program.\n" +
  34.                 "your option is: ");
  35.         int option = sc.nextInt();
  36.         System.out.println();
  37.         return option;          // back to 'Public Static Void Main();'
  38.     }
  39.  
  40.     // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  41.     public static int task1_1() {
  42.         int num, digit;
  43.         System.out.print("Please enter any number: ");
  44.         num = sc.nextInt();
  45.         System.out.print("Please enter any digit: ");
  46.         digit = sc.nextInt();
  47.         System.out.printf("The digit %d in the number %d appears %d times", digit, num, countDigit(num, digit));
  48.         int opt = 0;                    // back to Class Work Menu.
  49.         return opt = classWorkMenu();   // back to Class Work Menu.
  50.     }
  51.  
  52.     public static int countDigit(int num, int digit) {
  53.         int count = 0;
  54.         do {
  55.             count = num % 10 == digit ? count + 1 : count;
  56.             num /= 10;
  57.         } while (num > 0);
  58.         return count;
  59.     }
  60.  
  61.     // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  62.     public static int task1_2() {
  63.         int num[] = new int[5], digit;
  64.         for (int i = 0; i < num.length; i += 1) {
  65.             System.out.printf("Please enter a number (%d of %d): ", (i + 1), num.length);
  66.             num[i] = sc.nextInt();
  67.         }   // end for i loop
  68.         System.out.print("Please enter any digit: ");
  69.         digit = sc.nextInt();
  70.         System.out.printf("The digit %d appears %d times in the array\n", digit, countDigitArray(num, digit));
  71.         int opt = 0;                    // back to Class Work Menu.
  72.         return opt = classWorkMenu();   // back to Class Work Menu.
  73.     }
  74.  
  75.     public static int countDigitArray(int[] num, int digit) {
  76.         int count = 0;
  77.         for (int i = 0; i < num.length; i += 1) {
  78.             count += countDigit(num[i], digit);
  79.         }
  80.         return count;
  81.     }
  82.     // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  83.  
  84.     public static int task2() {
  85.         int arr1[] = {87, 12, 56, 32};
  86.         int arr2[] = {19, 87, 13, 21, 56};
  87.         int arrNew[] = sameNum(arr1, arr2);
  88.         System.out.println("\nArray 1:");
  89.         for (int i: arr1) { System.out.print(i + " "); }
  90.         System.out.println("\nArray 2:");
  91.         for (int i: arr2) { System.out.print(i + " "); }
  92.         System.out.println("\nNew array:");
  93.         for (int i: arrNew) { System.out.print(i + " "); }
  94.         int opt = 0;                    // back to Class Work Menu.
  95.         return opt = classWorkMenu();   // back to Class Work Menu.
  96.     }
  97.     public static int[] sameNum(int arr1[], int arr2[]) {
  98.         int size = arr1.length > arr2.length ? arr1.length: arr2.length;
  99.         int arrNew[] = new int[size];
  100.         int i=0;
  101.         for (int j = 0; j < arr1.length; j += 1 ) {
  102.             for (int k = 0; k < arr2.length; k += 1 ) {
  103.                 if (arr1[j] == arr2[k]) {
  104.                     arrNew[i] = arr1[j];
  105.                     i += 1;
  106.                 }   // end if
  107.             }   // end for k loop
  108.         }   // end for j loop
  109.         return arrNew;
  110.     }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement