/* See README.TXT for information about this software */ /** A Rain Gauge. */ public class RainGauge { private double totalRainfall; // the total amount of accumulated rainfall private int numberOfDays; // the number of days that we're measuring /** Create a rain gauge, with no rainfall and no days recorded. */ public RainGauge() { this.totalRainfall = 0.0; this.numberOfDays = 0; } /** Return the current total amount of accumulated rainfall, in inches. @return the total accumulated rainfall. */ public double getTotalRainfall() { return this.totalRainfall; } /** Return the number of days that we've recorded rainfall data. @return the number of days recorded. */ public int getNumberOfDays() { return this.numberOfDays; } /** Return the average amount of rainfall per day, in inches, or 0 if we've not recorded anything yet @return the average rainfall per day */ public double getAverageRainfall() { if (this.numberOfDays == 0) { return 0.0; } else { return this.totalRainfall / this.numberOfDays; } } /** Record another day's rainfall data. @param dailyRainfall the rainfall for this day, in inches. */ public void recordDailyRainfall(double dailyRainfall) { this.totalRainfall = this.totalRainfall + dailyRainfall; this.numberOfDays = this.numberOfDays + 1; } /** Compute a string representation of the rain gauge, e.g. for printing out */ public String toString() { return "RainGauge: " + totalRainfall + " inches of rain over the past " + numberOfDays + " days"; } /** A method to test out the RainGauge operations */ public static void test() { RainGauge gauge1 = new RainGauge(); gauge1.recordDailyRainfall(0.5); gauge1.recordDailyRainfall(0.4); gauge1.recordDailyRainfall(1.2); gauge1.recordDailyRainfall(0.0); gauge1.recordDailyRainfall(0.2); System.out.println(gauge1); // automatically calls gauge.toString() System.out.println("Average daily rainfall = " + gauge1.getAverageRainfall()); RainGauge gauge2 = new RainGauge(); System.out.println(gauge2); System.out.println("Average daily rainfall = " + gauge2.getAverageRainfall()); } }