import java.io.*;
import java.util.*;
/* This application reads data in from a file and stores
it into an array of classes. It then processes the data. */
public class Weather
{
public static void main (String []
args)
{
WeeksWeather
week = new WeeksWeather ();
week.readTemperatures
();
week.displayStatistics
();
} // main method
} // class Weather
// Class that holds weather data for a single day.
class WeatherData
{
private String dayName;
private int high, low;
WeatherData ()
{
high = 0;
low = 0;
dayName =
"";
} // constructor
public int getHigh ()
{
return high;
} // method getHigh
public int getLow ()
{
return low;
} // method getLow
public void readData (BufferedReader
infile, String name) throws IOException
{
dayName =
name;
high = Integer.parseInt
(infile.readLine ());
low = Integer.parseInt
(infile.readLine ());
} // method readData
public void displayTemps ()
{
System.out.println
(dayName + " High: " + high + " Low: " + low);
} // method displayTemps
} // class WeatherData
/* This class reads high and low temperatures into an
array of classes. It then calculates the average temperature and highest
temperature and displays the result on the screen. */
class WeeksWeather
{
private WeatherData [] temperatures;
private int numberTemps;
final int maxSize = 10;
WeeksWeather ()
{
numberTemps
= 0;
temperatures
= new WeatherData [maxSize];
} // constructor
public void readTemperatures ()
{
String name;
WeatherData
data;
BufferedReader
stdin = new BufferedReader (new InputStreamReader (System.in));
try
{
System.out.print ("File Name: ");
String fileName = stdin.readLine ();
BufferedReader infile = new BufferedReader (new InputStreamReader (new
FileInputStream (fileName)));
name = infile.readLine ();
while ((name != null) && (numberTemps < maxSize))
{
data = new WeatherData ();
data.readData (infile, name);
data.displayTemps ();
temperatures [numberTemps] = data;
numberTemps ++;
name = infile.readLine ();
}
} // try block
catch (IOException
e) { System.out.println ("IO Exception");}
catch (NumberFormatException
ex) { System.out.println ("Number format error.");}
} // method readTemperatures
private int calculateAverageHigh ()
{
int sum =
0;
for (int count
= 0; count < numberTemps; count ++)
sum = sum + temperatures [count].getHigh ();
int average
= sum / numberTemps;
return average;
} // method calculateAverage
private int findHighest ()
{
int highest;
highest =
temperatures [0].getHigh ();
for (int count
= 1; count < numberTemps; count ++)
{
if (highest < temperatures [count].getHigh ())
highest = temperatures [count].getHigh ();
}
return highest;
} // method findHighest
public void displayStatistics ()
{
int average
= calculateAverageHigh ();
int highest
= findHighest ();
System.out.println
("The average high was " + average);
System.out.println
("The highest temperature was " + highest);
} // method displayAverage
} // class WeeksWeather