Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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.

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. http://www.cs.colostate.edu/~cs163/.Fall17/assignments/P10/src//Temperatures.csv

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

// Interface.java: Interface for Temperature Analysis

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:

// Temperature.java: Class for Temperature Analysis

import java.text.SimpleDateFormat;

import java.util.Date;

public class Temperature {

// Instance data

public Date date;

public double temperature;

public double windspeed;

// Class constructor

// Look at hints in assignment specification

public Temperature(String dayMonthYear, String hour, double degrees, double speed) {

}

// Method to create date

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 ", 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 ", 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 ", 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 ", 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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data And Databases

Authors: Jeff Mapua

1st Edition

1978502257, 978-1978502253

More Books

Students also viewed these Databases questions