We found an excellent website that has around 20 years of temperature and wind data for Fort Collins. For this assignment, we downloaded 10 years of hourly data starting January 1, 2006 and ending December 31, 2015, for a total of almost 87,000 samples. The purpose of this assignment is to write code to read the downloaded data, and allow querying the maximum, minimum, and average temperature and highest wind speed over arbitrary periods within this date range.
The website for the weather data is here. I copied from the data from the HTML into an Excel spreadsheet called Temperatures.xlsx, which was culled in a .txt file, where values are whitespace-separated, called Temperatures.txt. You only need to download the .csv file for the purposes of this assignment, and put it into the P10 project. Do not change anything in this file, it is identical to the file we will use for testing your code.
import java.util.Date; // Java interface definition public interface Interface { // Read temperatures into an array public Temperature[] readTemperatures(String filename); // Find minimum temperature over a period public double findMinimum(Date start, Date end, Temperature[] data); // Find maximum temperature over a period public double findMaximum(Date start, Date end, Temperature[] data); // Find average temperature over a period public double findAverage(Date start, Date end, Temperature[] data); // Find highest windspeed over a period public double findHighest(Date start, Date end, Temperature[] data); }Here is what you need to add to your P10 class definition to make use of the interface:
public class P10 implements Interface {After you import the interface into the project, Eclipse will show a compile error on the class. If you fly over the error with the mouse, Eclipse will give you the option to create a stub for each method, so that you don't have to type in all the method signatures. Letting Eclipse make the stubs ensures that all the methods will be correctly defined.
import java.text.SimpleDateFormat; import java.util.Date; public class Temperature { // Instance data public Date date; public double temperature; public double windspeed; // Class constructor public Temperature(String dayMonthYear, String hour, double degrees, double speed) { } // Method to create date // Look at hints in assignment specification public static Date createDate(String date, String hour) { Date returnDate = null; SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy HH:mm"); String stringDate = date + " " + hour; try { returnDate = formatter.parse(stringDate); } catch (Exception e) { System.out.println("Invalid format: " + stringDate); } return returnDate; } // Check if date is in interval // Look at hints in assignment specification public boolean inInterval(Date start, Date end) { } }
The class constructor Temperature() calls createDate to translate the first two fields in an entry into a Java Date object. It then stores the date, temperature, and windspeed into instance variables.
The method createDate() translates the first two fields in an entry, which contain the date, for example 1-Jan-2006, and the hour, for example 13:00 into a Java Date object, and returns that object. We have supplied the code for this method.
The method inInterval() compares the instance variable date to a start and end date. If the instance variable is greater than or equal to the start date and less than or equal to the end date, return true, otherwise false. You might find the Date method compareTo() to be very useful, look at the documentation on the web.
Implement the readTemperatures() method by declaring a local array of Temperature objects, creating a Scanner for the file, reading in the number of samples (first line), allocating the local array, and reading the stated number of samples from the file into the array. Return the array at the end of the method. Test by printing out each entry in the array, probably not with Arrays.toString(), since this is lots of data. This method must be completed before any of the remaining code in P10.java can be written.
Implement the remaining methods by iterating the array and checking whether each entry is in the date range specified. If the entry is in the date array, figure out the minimum, maximum, or average temperature, otherwise ignore the entry. The inInterval should come in very useful. All four of the remaining methods use an almost identical loop.
public static void main(String[] args) { // Instantiate student code P10 p10 = new P10(); // Test readTemperatures Temperature[] data = p10.readTemperatures(args[0]); // Test findMinimum Date start = Temperature.createDate("04-Jul-2008", "06:00"); Date end = Temperature.createDate("17-Aug-2010", "23:00"); System.out.println("Verifying findMinimum method:"); System.out.println("Start date: " + start.toString()); System.out.println("End date: " + end.toString()); System.out.printf("Minimum = %.1f degrees\n", p10.findMinimum(start, end, data)); // Test findMaximum start = Temperature.createDate("19-Sep-2011", "07:00"); end = Temperature.createDate("23-Mar-2015", "13:00"); System.out.println("Verifying findMaximum method:"); System.out.println("Start date: " + start.toString()); System.out.println("End date: " + end.toString()); System.out.printf("Maximum = %.1f degrees\n", p10.findMaximum(start, end, data)); // Test findAverage start = Temperature.createDate("09-Apr-2006", "19:00"); end = Temperature.createDate("31-Oct-2013", "10:00"); System.out.println("Verifying findAverage method:"); System.out.println("Start date: " + start.toString()); System.out.println("End date: " + end.toString()); System.out.printf("Average = %.1f degrees\n", p10.findAverage(start, end, data)); // Test findHighest start = Temperature.createDate("01-Jan-2015", "00:00"); end = Temperature.createDate("31-Dec-2015", "23:00"); System.out.println("Verifying findHighest method:"); System.out.println("Start date: " + start.toString()); System.out.println("End date: " + end.toString()); System.out.printf("Highest windspeed = %.1f\n", p10.findHighest(start, end, data)); }
Verifying findMinimum method: Start date: Fri Jul 04 06:00:00 MDT 2008 End date: Tue Aug 17 23:00:00 MDT 2010 Minimum = -14.4 degrees Verifying findMaximum method: Start date: Mon Sep 19 07:00:00 MDT 2011 End date: Mon Mar 23 13:00:00 MDT 2015 Maximum = 99.8 degrees Verifying findAverage method: Start date: Sun Apr 09 19:00:00 MDT 2006 End date: Thu Oct 31 10:00:00 MDT 2013 Average = 50.9 degrees Verifying findHighest method: Start date: Thu Jan 01 00:00:00 MST 2015 End date: Thu Dec 31 23:00:00 MST 2015 Highest windspeed = 17.0