/* Month.java - handle a month's worth of climate data. * There is only one attribute: an array of Days * But we have several functions to implement. */ import java.util.Scanner; // to read input import java.io.FileInputStream; // to open input file import java.io.FileNotFoundException; // in case file doesn't exist public class Month { private Day [] day; // Note: if an attribute is an object or array, then you need to // allocate space and initialize in the constructor. You cannot // do the entire declaration and initialization all on one line as // in something like this: "int [] a = new int [10]". // Here is why: You cannot have an assignment statement where the // attributes are declared. And if you move the array declaration // to inside the constructor, then it's no longer an attribute but // instead a local variable. // Let's assume the input file is called "month.txt". public Month() throws FileNotFoundException { Scanner in = new Scanner(new FileInputStream("month.txt")); // Let's assume that the first line of the file tells us // how many days are in the month. int numDays = in.nextInt(); // Allocate space for the array. day = new Day [numDays]; // Loop to read in each day's information. // Create a Day variable, and put it into the array. int index = 0; while (index < numDays) { // Read the data about one day. int dayNumber = in.nextInt(); int high = in.nextInt(); int low = in.nextInt(); double rain = in.nextDouble(); // Create a Day, and put into array. // Note that we don't need the actual day number. Day d = new Day(dayNumber, high, low, rain); day[index] = d; index = index + 1; // don't forget! } } // We can now implement the 5 "find" functions. Each one will // loop through the array. public int findMax() { // First assume that the first day is the max. int max = day[0].getHigh(); // For each other day in the month, see if it has a higher temp. // Note that we can use the array's length attribute (built-in) // to find out how big the array is. int index = 1; while (index < day.length) { int dailyHigh = day[index].getHigh(); if (dailyHigh > max) max = dailyHigh; index = index + 1; } return max; } // The findMin() function is analogous to findMax. // For the sake of variety, I want to show you how you could search // for a particular object in an array, as opposed to searching for // just some object's attribute. // It turns out that returning an entire object is more informative. public Day findMin() { Day minDay = day[0]; int index = 1; while (index < day.length) { if (day[index].getLow() < minDay.getLow()) minDay = day[index]; index = index + 1; } return minDay; } public double findTotalRain() { double total = 0.0; int index = 0; while (index < day.length) { total = total + day[index].getRain(); index = index + 1; } return total; } // The findHeat and findAC are also analogous to findTotalRain. // For brevity, I'll only implement findHeat. public double findHeat() { double total = 0.0; int index = 0; while (index < day.length) { total = total + day[index].findHeat(); index = index + 1; } return total; } }