## 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:

• the average temperature;
• the list of all the days when the temperature was below freezing (i.e., below 32);
• the hottest temperature, and the list of all the days when the temperature was the hottest, with the corresponding wind speeds.

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

• a field date that contains the date, and
• a field temperature that contains the temperature.
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;

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){

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

public static void main(String[] args){
DetailedRecord[] records = new DetailedRecord[366];
String day;
int temp;
int wind;
int numberOfDates = 0;
boolean done = false;
char yesNo;
int i = 0;

while (!done)
{
System.out.print("Do you want to input more data? Y/N");
if (yesNo == 'Y')
{
System.out.println("Enter the date");
System.out.println("Enter the temperature");
System.out.println("Enter the wind speed"):
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());}
}
}
```