Averager

Averager - This method returns the number of numbers passed...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
/* A class to compute the running average of numbers passed to it */ public class Averager { // Private fields to hold the current state. private int nNum = 0; private double sum = 0.0, sumOfSquares = 0.0; /** * This method adds a new datum into the average. **/ public void addDatum(double x) { nNum++; sum += x; sumOfSquares += x * x; } /** This method returns the average of all numbers passed to addDatum() */ public double getAverage() { return sum / nNum; } /** This method returns the standard deviation of the data */ public double getStandardDeviation() { return Math.sqrt(((sumOfSquares - sum*sum/nNum)/nNum));
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: } /** This method returns the number of numbers passed to addDatum() */ public double getNum() { return nNum; } /** This method returns the sum of all numbers passed to addDatum() */ public double getSum() { return sum; } /** This method returns the sum of the squares of all numbers. */ public double getSumOfSquares() { return sumOfSquares; } /** This method resets the Averager object to begin from scratch */ public void reset() { nNum = 0; sum = 0.0; sumOfSquares = 0.0; } }...
View Full Document

This note was uploaded on 11/30/2009 for the course COMP 110 taught by Professor Stotts during the Spring '08 term at UNC.

Ask a homework question - tutors are online