import java.lang.reflect.*;

/**
 * A class that contains examples of accumulators.
 *
 * This implementation does not use the .length attribute
 * of the arrays.
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */
public class AccumulatorsNoAttributes {
    /**
     * A method that does something with an array.
     *
     * @param data  The data
     */
    public static void something(double[] data) {
        int n;

        n  = Array.getLength(data);       
//[loop
        for (int i = 0; i < n; i++) {
            // Do something with data[i]
        }
//]loop
    }

    /**
     * Determine if an array contains a targey value.
     *
     * @param data  The data
     * @param target  The target to look for
     * @return true if present; false otherwise
     */
    public static boolean contains(int[] data, int target) {
//[contains
        boolean      found;       
        int          n;
       
        n = Array.getLength(data);       
        found = false;

        for (int i = 0; ((i < n) && !found); i++) {
            if (target == data[i]) found = true;
        }
//]contains
        return found;       
    }

    /**
     * Calculate the mean after dropping the minimum.
     *
     * @param data  The data
     * @return The mean after dropping
     */
    public static double meanWithDroppedLowest(double[] data) {
//[droplowest
        double lowest, result, total;       
        int n;
       
        n = Array.getLength(data);       
        total = 0.0;       
        lowest = Double.POSITIVE_INFINITY;

        for (int i = 0; i < n; i++) {
            total += data[i];
            if (data[i] < lowest) lowest = data[i];
        }
        result = (total - lowest) / (n - 1);
//]droplowest
        return result;
    }

    /**
     * Find the maximum value in an array.
     *
     * @param data  The data
     * @return The max
     */
    public static double max(double[] data) {
//[max
        double maximum;       
        int n;
       
        n = Array.getLength(data);       
        maximum = Double.NEGATIVE_INFINITY;       

        for (int i = 0; i < n; i++) {
            if (data[i] > maximum) maximum = data[i];
        }
//]max
        return maximum;       
    }

    /**
     * Create a Gmail address from a name.
     *
     * @param name  The name
     * @return  The Gmail address
     */
    public static String createGmailAddress(String[] name) {
//[createGmailAddress
        int n;        
        String address;

        n = Array.getLength(name);
        if (n == 0) {
            address = "no-reply";
        } else {
            address = name[0];
            for (int i = 1; i < n; i++) {
                address += "." + name[i];            
            }
        }
        address += "@gmail.com";
//]createGmailAddress
        
        return address;
    }

    /**
     * Find the index of the maximum value in an array.
     *
     * @param data  The data
     * @return The index of the max
     */
    public static int argmax(double[] data) {
//[argmax
        double maximum;       
        int index, n;
       
        n = Array.getLength(data);       
        maximum = Double.NEGATIVE_INFINITY;       
        index = -1;

        for (int i = 0; i < n; i++) {
            if (data[i] > maximum) {
                index   = i;
                maximum = data[i];
            }
        }
//]argmax
        return index;       
    }

    /**
     * Calculate the total of the elements in an array.
     *
     * @param data The data
     * @return The total
     */
    public static double total(double[] data) {
//[total
        double total;       
        int n;
       
        n = Array.getLength(data);       
        total = 0.0;       

        for (int i = 0; i < n; i++) {
            total += data[i];
        }
//]total
        return total;       
    }
}
