Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Lesson009_18_08_19_HomeWork2 {
- private static Scanner sc = new Scanner(System.in);
- public static void main(String[] args) {
- int option = classWorkMenu();
- while (option != 0) { // while the user didn't put '0', keep pop-up the menu!
- switch (option) {
- case 11:
- option = task1_1();
- break;
- case 12:
- option = task1_2();
- break;
- case 2:
- option = task2();
- break;
- default:
- System.out.println(option + " is invalid option! try again...\n");
- option = classWorkMenu();
- }
- }
- }
- public static int classWorkMenu() {
- System.out.print("\n...::: MENU :::...\n" + // A menu for class tasks
- "From presentation '004_18-08-19 functions' Page 34:\n" +
- "press [11] for task 1.1\n" +
- "press [12] for task 1.2\n" +
- "From presentation '004_18-08-19 functions' Page 35:\n" +
- "press [2] for task 2\n" +
- "press [0] to end this program.\n" +
- "your option is: ");
- int option = sc.nextInt();
- System.out.println();
- return option; // back to 'Public Static Void Main();'
- }
- // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
- public static int task1_1() {
- int num, digit;
- System.out.print("Please enter any number: ");
- num = sc.nextInt();
- System.out.print("Please enter any digit: ");
- digit = sc.nextInt();
- System.out.printf("The digit %d in the number %d appears %d times", digit, num, countDigit(num, digit));
- int opt = 0; // back to Class Work Menu.
- return opt = classWorkMenu(); // back to Class Work Menu.
- }
- public static int countDigit(int num, int digit) {
- int count = 0;
- do {
- count = num % 10 == digit ? count + 1 : count;
- num /= 10;
- } while (num > 0);
- return count;
- }
- // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
- public static int task1_2() {
- int num[] = new int[5], digit;
- for (int i = 0; i < num.length; i += 1) {
- System.out.printf("Please enter a number (%d of %d): ", (i + 1), num.length);
- num[i] = sc.nextInt();
- } // end for i loop
- System.out.print("Please enter any digit: ");
- digit = sc.nextInt();
- System.out.printf("The digit %d appears %d times in the array\n", digit, countDigitArray(num, digit));
- int opt = 0; // back to Class Work Menu.
- return opt = classWorkMenu(); // back to Class Work Menu.
- }
- public static int countDigitArray(int[] num, int digit) {
- int count = 0;
- for (int i = 0; i < num.length; i += 1) {
- count += countDigit(num[i], digit);
- }
- return count;
- }
- // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
- public static int task2() {
- int arr1[] = {87, 12, 56, 32};
- int arr2[] = {19, 87, 13, 21, 56};
- int arrNew[] = sameNum(arr1, arr2);
- System.out.println("\nArray 1:");
- for (int i: arr1) { System.out.print(i + " "); }
- System.out.println("\nArray 2:");
- for (int i: arr2) { System.out.print(i + " "); }
- System.out.println("\nNew array:");
- for (int i: arrNew) { System.out.print(i + " "); }
- int opt = 0; // back to Class Work Menu.
- return opt = classWorkMenu(); // back to Class Work Menu.
- }
- public static int[] sameNum(int arr1[], int arr2[]) {
- int size = arr1.length > arr2.length ? arr1.length: arr2.length;
- int arrNew[] = new int[size];
- int i=0;
- for (int j = 0; j < arr1.length; j += 1 ) {
- for (int k = 0; k < arr2.length; k += 1 ) {
- if (arr1[j] == arr2[k]) {
- arrNew[i] = arr1[j];
- i += 1;
- } // end if
- } // end for k loop
- } // end for j loop
- return arrNew;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement