CS 1401 Assignment #11, Solutions

Date Assigned: Monday, April 9, or Tuesday, April 10, 2007.

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

Objective: The main objective of this assignment is to learn to use inheritance and polymorphism.

Programming assignment: The task is the same as in the previous Assignments 9 and 10: Write a program for analyzing El Paso weather. This program must allow the user to enter dates followed by temperatures and wind speeds 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:

Main difference: In the previous assignment 10, we advised you to define a class Record whose objects contain two fields:

To be able to to store wind speeds, design a new class DetailedRecord which contains the third field: wind speed. Use objects of the new type in your program.

Deliverables: as instructed by your TA.


Solution:

import java.util.*;

public class Record{
  private String date;
  private int temperature;

  public Record(String adate, int atemperature){
    date = adate; temperature = atemperature;}

  public String getDate(){return date;}
  public int getTemperature(){return temperature;}

  public void setDate(String newDate){date = newDate;}
  public void setTemperature(int newTemperature)
    {temperature = newTemperature;}
}

public class DetailedRecord extends Record{
  private int windSpeed;

  public DetailedRecord(String adate, int atemperature, int awind){
    super(adate, atemperature); windSpeed = awind;}

  public int getWind(){return windSpeed;}
  public void setWind(int newWind){windSpeed = newwind;}
}

public static void main(String[] args){
  DetailedRecord[] records = new DetailedRecord[366];
  Scanner reader = new Scanner(System.in);
  String day;
  int temp;
  int wind;
  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");
      day = reader.next();
      System.out.println("Enter the temperature");
      temp = reader.nextInt();
      System.out.println("Enter the wind speed"):
      wind = reader.nextInt();
      records[i] = new DetailedRecord(day, temp, wind);
      i++;
      numberOfDates++;
    }
    else
      {done = true;}
  }

  //computing the average
  double sum = 0.0;
  for (i = 0; i < numberOfDates; i++)
    {sum += records[i].getTemperature();}
  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 (records[i].getTemperature() < 32)
      {System.out.println(records[i].getDate());}
    }

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

  //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 (records[i].getTemperature() == hottestSoFar)
      {System.out.println(records[i].getDate() + " wind " +
         records[i].getWind());}
    }
}