CS 1401 Assignment #9, Solutions

Date Assigned: Monday, March 26, or Tuesday, March 27, 2007.

Due Date: Monday, April 2, or Tuesday, April 3, 2007, before the beginning of your lab section.

Objective: The main objective of this assignment is to learn to use arrays.

Programming assignment: Write a program for analyzing El Paso weather. This program must allow the user to enter dates followed by temperatures at these dates. Assume that we are only tracing one year, so the overall number of such dates does not exceed 366. Your must then output the following information:


public static void main(String[] args){
  Scanner reader = new Scanner(System.in);
  String[] dates = new String[366];
  int[] temperature = new int[366];
  int numberOfDates = 0;
  Boolean done = false;
  char yesNo;
  int i = 0;

  //reading the data
  while (!done)
    System.out.print("Do you want to input more data? Y/N");
    yesNo = reader.next().charAt(0);
    if (yesNo == 'Y')
      System.out.println("Enter the date");
      dates[i] = reader.next():
      System.out.println("Enter the temperature");
      temperature[i] = reader.nextInt();
      {done = true;}

  //computing the average
  double sum = 0.0;
  for (i = 0; i < numberOfDates; i++)
    {sum += temperature[i];}
  double average = sum / numberOfDates;
  System.out.println("The average temperature in El Paso is " + average);

  //printing all the days when the temperature was below freezing
  System.out.println("Days when temperature was below freezing:");
  for (i = 0; i < numberOfDates; i++)
    {if (temperature[i] < 32) {System.out.println(date[i]);}}

  //computing the hottest temperature
  int hottestSoFar = temperature[0];
  for (i = 1; i < numberOfDates; i++)
    {if (temperature[i] > hottestSoFar) {hottestSoFar = temperature[i];}}

  //printing all the days when the temperature was the hottest
  System.out.println("Days when temperature was the hottest:");
  for (i = 0; i < numberOfDates; i++)
    {if (temperature[i] == hottestSoFar) {System.out.println(date[i]);}}

Homework assignment: on a separate sheet of paper, solve Ex. 2, 4, and 10 at the end of Chapter 9 (on pp. 598, 599, and 602).

Deliverables: as instructed by your TA.