/**
 * ArrayModifyingMethods.java
 * 
 * Computer Science S-111
 * 
 * Examples of methods that modify an array that is passed in as a parameter.
 * Because methods can modify arrays that are passed in as parameters,
 * there's no need for these methods to return anything.
 */

import java.util.*;     // needed for the Arrays class, so we can use Arrays.toString()

public class ArrayModifyingMethods {
    /*
     * triple - multiply by 3 each of the values in the array that is
     * passed in as a parameter
     */
    public static void triple(int[] values) {
        for (int i = 0; i < values.length; i++) {
            values[i] *= 3;
        }
    }
    
    /*
     * swap - swap values[i] with values[j] in the array that is passed
     * in as a parameter
     */
    public static void swap(int[] values, int i, int j) {
        int temp = values[i];
        values[i] = values[j];
        values[j] = temp;
    }

    /*
     * countMultiples - Count the number of multiples of 2, the number of multiples of 3,
     * and the number of multiples of 5 in the values array that is passed in as the
     * first parameter, storing the counts in the multipleCounts array that is passed in 
     * as the second parameter.
     *
     * precondition: the countMultiples array must be an array of length 6 
     * so that we can use it as follows:
     *    counts[2] will store the number of multiples of 2
     *    counts[3] will store the number of multiples of 3
     *    counts[5] will store the number of multiples of 5
     * 
     * postcondition: the array passed in for multipleCounts will hold the counts
     */
    public static void countMultiples(int[] values, int[] multipleCounts) {
        for (int i = 0; i < values.length; i++) {
            // count multiples of 2
            if (values[i] % 2 == 0) {
                multipleCounts[2]++;
            }
            
            // count multiples of 3
            if (values[i] % 3 == 0) {
                multipleCounts[3]++;
            }
            
            // count multiples of 5
            if (values[i] % 5 == 0) {
                multipleCounts[5]++;
            }
        }
    }
    
    public static void main(String[] args) {
        int[] a = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        System.out.println("a = " + Arrays.toString(a));
        System.out.println();
        
        // Triple the values in the array.
        System.out.println("calling triple(a)");
        triple(a);
        System.out.println("a = " + Arrays.toString(a));
        System.out.println();
        
        // Swap the values in positions 2 and 5 of the array.
        System.out.println("calling swap(a, 2, 5)");
        swap(a, 2, 5);
        System.out.println("a = " + Arrays.toString(a));
        System.out.println();
        
        //
        // Count the number of multiples of 2, the number of multiples of 3,
        // and the number of multiples of 5 in the array a.
        //
        // Because the method can't return three different values, we create
        // an array to store the counts and pass the array into the method.
        //
        // We make the counts array an array of length 6 so that we can
        // use it as follows:
        //      counts[2] will store the number of multiples of 2
        //      counts[3] will store the number of multiples of 3
        //      counts[5] will store the number of multiples of 5
        //
        // When the method returns, the counts array will store the 
        // counts made by the method.
        //
        int[] counts = new int[6];
        System.out.println("counts = " + Arrays.toString(counts));
        System.out.println("calling countMultiples(a, counts)");
        countMultiples(a, counts);
        System.out.println("counts = " + Arrays.toString(counts));
        System.out.println("There are:");
        System.out.println("    " + counts[2] + " multiples of 2");
        System.out.println("    " + counts[3] + " multiples of 3");
        System.out.println("    " + counts[5] + " multiples of 5");
    }
}