Computer Science 121
Example using an array of classes

import java.io.*;

/* This application finds the average high temperature and highest temperature for a week. It uses an array of classes to store the data. */
public class Weather4
{
    public static void main (String [] args)
    {
        WeeksWeather week = new WeeksWeather ();
        week.readTemperatures ();
        week.displayStatistics ();
    } // main method
} // class Weather

// WeatherData is a class that stores high and low temperatures.
class WeatherData
{
    private int high, low;

    WeatherData (int h, int l)
    {
        high = h;
        low = l;
    } // constructor

    public int getHigh ()
    {
        return high;
    } // method getHigh

    public int getLow ()
    {
        return low;
    } // method getLow
} // class WeatherData

/* This class reads high and low temperatures into an array of classes. It then calculates the average temperature and displays the result on the screen. */
class WeeksWeather
{
    WeatherData [] temperatures;
    int numberTemps;
    final int maxSize = 10;

    WeeksWeather ()
    {
        numberTemps = 0;
        temperatures = new WeatherData [maxSize];
    } // constructor

    public void readTemperatures ()
    {
        int high, low;
        WeatherData data;
        BufferedReader stdin = new BufferedReader (new InputStreamReader (System.in));
        try
        {
            System.out.println ("Enter high and low temperatures ending with a high of 200.");
            System.out.print ("Enter first high temperature: ");
            high = Integer.parseInt (stdin.readLine ());
            while ((high < 200) && (numberTemps < maxSize))
            {
                System.out.print ("Enter next low temperature: ");
                low = Integer.parseInt (stdin.readLine ());
                data = new WeatherData (high, low);
                temperatures [numberTemps] = data;
                numberTemps ++;
                System.out.print ("Enter next high temperature: ");
                high = Integer.parseInt (stdin.readLine ());
            }
        } // try block
        catch (IOException e) { System.out.println ("IO Exception");}
        catch (NumberFormatException ex) { System.out.println ("Number format error.");}
    } // method readTemperatures

    public void displayStatistics ()
    {
        int average = calculateAverageHigh ();
        System.out.println ("The average high was " + average);
    } // method displayAverage

    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
} // class WeeksWeather