Advertisement
mmayoub

functions, exercise 01, slide 34

Jul 4th, 2017
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.67 KB | None | 0 0
  1. package class170703;
  2.  
  3. public class functions01 {
  4.     // Functions, exercise 01, slide 34
  5.     // input: array of numbers and a digit
  6.     // output: number of appearances of the digit in the array numbers
  7.  
  8.     // building a data sample: array of integers and a digit.
  9.     // then calling function countAppearances to calculate appearances
  10.     // and print results
  11.     public static void main(String[] args) {
  12.         // create a sample data for testing only
  13.         // array of integers
  14.         int[] numbersArray = { 144, 47, 42, 74, 19 };
  15.         // a digit
  16.         int aDigit = 4;
  17.  
  18.         // printing a message containing array elements and the digit
  19.         System.out.printf("searching for %d in %s\n", aDigit,
  20.                 toString(numbersArray));
  21.  
  22.         // count and print appearances of the digit in the array values
  23.         System.out.printf("%d appears %d times\n", aDigit,
  24.                 countAppearances(numbersArray, aDigit));
  25.  
  26.     }
  27.  
  28.     // convert an array of integer numbers to a string
  29.     // input: an array of integer values
  30.     // output: a string with all values from the array separated by comma
  31.     public static String toString(int[] array) {
  32.         // if array is empty
  33.         if (array.length == 0) {
  34.             // return result and quit
  35.             return "[]";
  36.         }
  37.  
  38.         // building a string from all numbers in the array
  39.         String result = "" + array[0]; // start with the first number
  40.  
  41.         // scan the numbers in the array without the first
  42.         for (int i = 1; i < array.length; i += 1) {
  43.             // convert number to string and add it to the result string
  44.             result += ", " + array[i];
  45.         }
  46.  
  47.         // return the result surrounded by brackets
  48.         return "[" + result + "]";
  49.     }
  50.  
  51.     // count the appearances of a digit in an array of integers
  52.     // input: an array of integer values and a digit
  53.     // output: number of times the digit appears in the values of the array
  54.     public static int countAppearances(int[] numbersArray, int digit) {
  55.         // initialize a counter
  56.         int counter = 0;
  57.  
  58.         // for each number in the array
  59.         for (int aNumber : numbersArray) {
  60.             // count appearances of digit in it and update counter
  61.             counter += countAppearance(aNumber, digit);
  62.         }
  63.  
  64.         return counter;
  65.     }
  66.  
  67.     // count the appearances of a digit in an integer number
  68.     // input:an integer number and a digit
  69.     // output: number of times the given digit appears in the given number
  70.     public static int countAppearance(int number, int digit) {
  71.         // initialize a counter
  72.         int count = 0;
  73.  
  74.         // loop to scan each digit in the number
  75.         do {
  76.             // update counter by one iff most right digit is equal to digit
  77.             count += number % 10 == digit ? 1 : 0;
  78.             // drop the most right digit
  79.             number /= 10;
  80.         } while (number > 0); // loop while number has more digits to check
  81.  
  82.         // return the result
  83.         return count;
  84.     }
  85.  
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement