/* Day.java - store climate information about a single day. * Attributes are the raw data: high and low temperature, and amount of rain. */ public class Day { private int dayNumber; private int high; private int low; private double rain; private static final double COMFORT = 65.0; public Day(int dayNumber, int high, int low, double rain) { this.dayNumber = dayNumber; this.high = high; this.low = low; this.rain = rain; } // Let's have a way of printing the values of one day. public String toString() { return String.format("day %d, high %d, low %d, rain = %.2f\n", dayNumber, high, low, rain); } // findAverage() - the average of the high and low public double findAverage() { return 0.5 * (high + low); } // findHeat - We need heat if the average temperature is less than 65. // For each degree it's less than 65, this is an extra "degree day". // So the formula is: 65 - average temp. // However, if the average temp is above 65, we need no heat: return 0. // But we shouldn't hard code the constant 65. We may want to change // the comfort threshold to some other value. Use a named constant. public double findHeat() { if (findAverage() < COMFORT) return COMFORT - findAverage(); else return 0.0; } // findAC is analogous public double findAC() { if (findAverage() > COMFORT) return findAverage() - COMFORT; else return 0.0; } // "get" functions, to return attribute values public int getHigh() { return high; } public int getLow() { return low; } public double getRain() { return rain; } }