Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hi! I\'m really lost on this programming assignment I have, it would be helpful

ID: 3914796 • Letter: H

Question

Hi! I'm really lost on this programming assignment I have, it would be helpful if you could follow the directions as followed:

Part One

Create a project and class called P10, with an empty main method. Make the class implement the interface provided in Interface.java (the following code), which you must download to the src directory from here. Here is the interface that is defined in this file:

--INTERFACE.JAVA--

Here is what you need to add to your P10 class definition to make use of the 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.

--TEMPERATURE.JAVA--

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 (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.

Test Code

Put the following test code in the main method in P10, and compare the output to the correct answer shown below:

--P10 MAIN METHOD--

Test Results

Here is the expected output from the test code shown above:

Thank you for any help! I might have missed spaces where code is needed.

Explanation / Answer

Hi,

Please find the complete classes below:

//////////////////////////////////////


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) {
      this.date=createDate(dayMonthYear,hour);
      this.temperature=degrees;
      this.windspeed=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) {
       boolean inFlag=false;
       if (this.date.compareTo(start) >= 0) {
            if (this.date.compareTo(end) <= 0) {
               inFlag=true;
               return inFlag;
            }
       }
            return inFlag;
       }  
}

////////////////////////////////////////////////////////////
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Scanner;

public class P10 implements Interface {

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

   }

   //
   @Override
   public Temperature[] readTemperatures(String filename) {
       ArrayList<Temperature> temps=new ArrayList<Temperature>();
       String dayMonthYear;
       String hour;
       double degrees;
       double speed;
       String line;
       Scanner tempFile=null;
       try {
       File tfile = new File(filename);
       tempFile = new Scanner(tfile);             
       while(tempFile.hasNext())
       {
           line = tempFile.nextLine();
           String[] tokens=line.split(",");
           dayMonthYear=tokens[0].trim();
           hour=tokens[1].trim();
           degrees=Double.parseDouble(tokens[2]);
           speed=Double.parseDouble(tokens[3]);
           Temperature t=new Temperature(dayMonthYear,hour,degrees,speed);
           temps.add(t);
       }
       }catch(FileNotFoundException fn) {
           fn.printStackTrace();
       }
       return temps.toArray(new Temperature[temps.size()]);
   }
  

   @Override
   public double findMinimum(Date start, Date end, Temperature[] data) {
       double minTemp=Double.MAX_VALUE;
        for(Temperature t : data)
        {
           System.out.println(t.date);
           if(t.inInterval(start, end))
           {
               if(t.temperature < minTemp)
                   minTemp=t.temperature;
           }
        }
       return minTemp;
   }

   @Override
   public double findMaximum(Date start, Date end, Temperature[] data) {
       double maxTemp=Double.MIN_VALUE;
        for(Temperature t : data)
        {
           if(t.inInterval(start, end))
           {
               if(t.temperature > maxTemp)
                   maxTemp=t.temperature;
           }
        }
       return maxTemp;
   }

   @Override
   public double findAverage(Date start, Date end, Temperature[] data) {
       double sum=0.0;
       int count=0;
        for(Temperature t : data)
        {
           if(t.inInterval(start, end))
           {
           sum=sum+t.temperature;
           count++;
           }
        }
       return sum/count;
   }

   @Override
   public double findHighest(Date start, Date end, Temperature[] data) {
       double highest=Double.MIN_VALUE;
        for(Temperature t : data)
        {
           if(t.inInterval(start, end))
           {
               if(t.windspeed > highest)
                   highest=t.windspeed;
           }
        }
       return highest;
   }
  
  
}

////////////////////////////////////////////////////////////////////////

Format of input file is comma seperated.

For example, below is the sample data

04-Jul-2008,06:00,34.3,75.0
17-Aug-2010,23:00,33.2,50.0
19-Sep-2011,07:00,49.0,120.0
01-Jan-2015,19:00,42.5,10.0
17-Aug-2009,07:00,41.0,99.5
01-Aug-2015,19:00,42.5,100.5

Sample program output:

Verifying findMinimum method:
Start date: Fri Jul 04 06:00:00 IST 2008
End date: Tue Aug 17 23:00:00 IST 2010
Fri Jul 04 06:00:00 IST 2008
Tue Aug 17 23:00:00 IST 2010
Mon Sep 19 07:00:00 IST 2011
Thu Jan 01 19:00:00 IST 2015
Mon Aug 17 07:00:00 IST 2009
Sat Aug 01 19:00:00 IST 2015
Minimum = 33.2 degrees
Verifying findMaximum method:
Start date: Mon Sep 19 07:00:00 IST 2011
End date: Mon Mar 23 13:00:00 IST 2015
Maximum = 49.0 degrees
Verifying findAverage method:
Start date: Sun Apr 09 19:00:00 IST 2006
End date: Thu Oct 31 10:00:00 IST 2013
Average = 39.4 degrees
Verifying findHighest method:
Start date: Thu Jan 01 00:00:00 IST 2015
End date: Thu Dec 31 23:00:00 IST 2015
Highest windspeed = 100.5

Hope this helps.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote