Here you can find the source of calculateMean(Collection extends Number> values)
Parameter | Description |
---|---|
values | the values we're calculating a mean for |
Parameter | Description |
---|---|
IllegalArgumentException | if the size of the input is empty |
public static double calculateMean(Collection<? extends Number> values) throws IllegalArgumentException
//package com.java2s; /*/* w ww. j a va 2 s.c o m*/ * Copyright (c) 2010 The Jackson Laboratory * * This is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this software. If not, see <http://www.gnu.org/licenses/>. */ import java.util.Collection; public class Main { /** * Calculate a mean for the input * @param values * the values we're calculating a mean for * @return * a mean value * @throws IllegalArgumentException * if the size of the input is empty */ public static double calculateMean(Collection<? extends Number> values) throws IllegalArgumentException { if (values.isEmpty()) { throw new IllegalArgumentException("can't calculate a mean for an empty collection"); } else { double sum = 0.0; for (Number currValue : values) { sum += currValue.doubleValue(); } return sum / values.size(); } } /** * Calculate a mean for the input * @param values * the values we're calculating a mean for * @return * a mean value * @throws IllegalArgumentException * if the size of the input is empty */ public static double calculateMean(double[] values) throws IllegalArgumentException { if (values.length == 0) { throw new IllegalArgumentException("can't calculate a mean for an empty list"); } else { double sum = 0.0; for (Number currValue : values) { sum += currValue.doubleValue(); } return sum / values.length; } } }