Project 'tdt4100/v2020/kont2020' was moved to 'tdt4100/eksamener/kont2020'. Please update any links and bookmarks that may still have the old path.
-
= authored963083f9
AverageComputer.java 1.02 KiB
package stuff;
import java.util.ArrayList;
import java.util.Collection;
/**
* Encapsulates a set of values and computes their average.
*/
public class AverageComputer {
private Collection<Integer> values;
/**
* Initializes with the provided values.
* @param initialValues
*/
public AverageComputer(Collection<Integer> initialValues) {
this.values = initialValues;
}
/**
* Initializes with no values.
*/
public AverageComputer() {
this(new ArrayList<>());
}
/**
* Appends value to the values.
* @param value
*/
public void appendValue(int value) {
values.add(value);
}
/**
* Computes the average of all values.
* @return the average of all values
* @throws IllegalStateException if there are no values
*/
public double computeAverage() throws IllegalStateException {
if (values.size() == 0) {
throw new IllegalStateException("No values");
}
int sum = 0;
for (int value : values) {
sum += value;
}
return sum / values.size();
}
}