Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Temperature Analysis Description We found an excellent website that has around 20 years of temperature and wind data for Fort Collins. For this assignment, we

Temperature Analysis

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

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:

  • Right click the project and select Export.
  • Select Java JAR file format.
  • Click the "Export Java source file and resources" tab.
  • Name the file P10.jar, which will be stored in the workspace.
  • Submit the resulting P10.jar to the Checkin tab.

image text in transcribed

Please follow the usual rules for submitting Java programs.

  • Work on your own.
  • The name of the archive file must be exactly P10.jar
  • See the Submission section for how to build a Java archive (.jar).
  • Comments at the top with your name, e-Name, date and course.
  • We expect programming assignments to be implemented in Eclipse using Java 1.8.

NOTE: We will test your code with the data file provided, but different date ranges!

The website for the weather data is here.

http://ccc.atmos.colostate.edu/~autowx/fclwx_access.php

Make the class implement the interface provided in Interface.java, which you must download to the src directory from here.

// Interface.java: Interface for Temperature Analysis // Author: Chris Wilcox // Updated: Russ Wakefield // Date: 4/1/2017 // Class: CS163/CS164 // Email: waker@colostate.edu 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 your starting point for the Temperature class, which you can download here.

// Temperature.java: Class for Temperature Analysis // Author:  // Date:  // Class: CS163/CS164 // Email:  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) { } } 
JAR Export JAR File Specification The export destination will be relative to your workspace Select the resources to export: MusicLibrary . ? Interface-Java O@pl DP10 java Temperature java (default package) settings Export generated class files and resources Export all output folders for checked projects Export Java source files and resources Export refactorings for checked projects Select the export destination: JAR file: P10.jar Options: Browse Compress the contents of the JAR file Add directory entries Overwrite existing files without warning Cancel Finish

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions