Computer Science 121
Example using an array of integer temperatures.

import java.io.*;

// This application finds the average high temperature for a week.
public class Weather
{
    public static void main (String [] args)
    {
        WeeksWeather week= new WeeksWeather ();
        week.readTemperatures ();
        week.displayAverage ();
    } // main method
} // class Weather

/* This class reads high temperatures into an array of integers. It then calculates the average temperature and displays the result on the screen. */
class WeeksWeather
{
    int [] highs;
    int numberTemps;

    WeeksWeather ()
    {
        numberTemps = 0;
        highs = new int [10];
    } // constructor

    public void readTemperatures ()
    {
        int temperature;
        BufferedReader stdin = new BufferedReader (new InputStreamReader (System.in));
        try
        {
            System.out.println ("Enter high temperatures ending with a zero.");
            System.out.print ("First temperature: ");
            temperature = Integer.parseInt (stdin.readLine ());
            while (temperature > 0)
            {
                highs [numberTemps] = temperature;
                numberTemps ++;
                System.out.print ("Next temperature: ");
                temperature = 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

    private int calculateAverage ()
    {
        int sum = 0;
        for (int count = 0; count < numberTemps; count ++)
            sum = sum + highs [count];
        int average = sum / numberTemps;
        return average;
    } // method calculateAverage

    public void displayAverage ()
    {
        int average = calculateAverage ();
        System.out.println ("The Average high was " + average);
    } // method displayAverage
} // class WeeksWeather