Aside from the use of BigDecimal
s functions mentioned in JvR's answer JvR's answer, I see one other glaring issue with your code.
Aside from the use of BigDecimal
s functions mentioned in JvR's answer, I see one other glaring issue with your code.
Aside from the use of BigDecimal
s functions mentioned in JvR's answer, I see one other glaring issue with your code.
Aside from the use of BigDecimal
s functions mentioned in JvR's answer, I see one other glaring issue with your code.
Your indentation is all over the place.
/**
* Give a new number to this Statistician.
*
* @param <CODE>number</CODE> the new number that is being given the this
* Statistician
* <dt><b>Postcondition:</b><dd>
* The specified number has been given to this Statistician and it will be
* included in any subsequent statistics.
*
*/
public void nextNumber(double number) {
length++;
sum += number;
last = number;
if (length == 1) {
max = number;
min = number;
}
else {
if (number > max) {
max = number;
}
}
if (number < min) {
min = number;
}
}
I strongly suggest you find and use the auto-indent functions of your IDE.
As a example, here's how the code should look:
/**
* Give a new number to this Statistician.
*
* @param <CODE>number</CODE> the new number that is being given the this
* Statistician
* <dt><b>Postcondition:</b><dd>
* The specified number has been given to this Statistician and it will be
* included in any subsequent statistics.
*
*/
public void nextNumber(double number) {
length++;
sum += number;
last = number;
if (length == 1) {
max = number;
min = number;
} else {
if (number > max) {
max = number;
}
}
if (number < min) {
min = number;
}
}
Proper indentation makes it easier to find optimizations too. You could convert
else {
if (number > max) {
max = number;
}
}
into
else if (number > max) {
max = number;
}
without any risk. This makes the code even shorter, without reducing meaning. It effectively becomes easier to understand.