CSU Banner

CS 163/164, Fall 2017

Programming Assignment - P10

Temperature Analysis

Due Monday, Apr. 9th at 06:00 pm

Late Tuesday, Apr. 10th at 08:00am


Objectives of this Assignment

This lab has the goal of teaching you how to:
  1. Read a large amount of data from a file into an array of objects,
  2. play around with the highly useful Java Date class,
  3. implement an interface given to you, and
  4. figure the minimum, maximum, and average temperature in Fort Collins,
  5. over an arbitrary range of dates.

Description

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 .csv or comma-separated values file called Temperatures.csv. 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.

Instructions

Part One

Create a project and class called P10, with an empty main method. Make the class implement the interface provided in Interface.java, which you must download to the src directory from here. Here is the interface that is defined in this file:
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.

Part Two

Before you implement the methods in P10.java, you must import the Temperature class and complete two of the methods. The Temperature class represents an entry in the temperatures data file, with instance variables including a Date object and double variables for the temperature and windspeed. Here is your starting point for the Temperature class, which you can download here.
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.

Part Three

Implement the readTemperatures() method by declaring a local array of Temperature objects, creating a Scanner for the file, reading in the number of samples, allocating the local array, and reading that number of entries 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.

Test Code

Put the following test code in the main method in P10, and compare the output to the correct answer shown below:
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));
}

Test Results

Here is the expected output from the test code shown above:
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

Submissions

To create the P10.jar file containing Temperature.java and P10.java, you must export the project from Eclipse in the JAR format. The default in Eclipse is to make a JAR with Temperature.class and P10.class. This will not pass automated grading. To avoid this and correctly submit the source files, follow these directions: Export Java source files with Eclipse.

Please follow the usual rules for submitting Java programs. NOTE: We will test your code with the data file provided, but different date ranges!

Grading Criteria


Submit P10.jar to the Checkin tab


CS Banner
CS Building