Question

Create a program, Weather.java, which loads an unsorted set of weather data for Pendleton, sorts the...

Create a program, Weather.java, which loads an unsorted set of weather data for Pendleton, sorts the data by date (handling date errors), and performs some analysis of the data (e.g. min/max temperature), and outputs the sorted data set to a file.

Sample data:

2015-12-12,47,43,38,32,12,39,0.09,7,Rain,205
2015-05-08,73,56,38,18,7,22,0,0,,75
2015-11-10,51,44,37,18,8,21,0,4,,261
2016-02-02,45,36,26,12,5,14,0,1,,111
2015-06-28,109,89,68,38,11,48,T,0,Rain-Thunderstorm,296

Write methods to calculate each of the following on the given data set:

  • Max. temperature
  • Min. temperature
  • Max. wind gust
  • Max. precipitation and what the 'events' were for that day

Each result should include the day on which the max/min occurred. State the results in a text file called calculations.txt and upload it as part of your submission.

Since the WeatherData class does not contain a compareTo method you won't be able to just compare two of those objects (which you don't want to do anyway, since you want to compare the data properties of two objects). Hint: the LocalDate class doesincludes a compareTo method and each WeatherData object has an instance variable of that type.

Save the sorted data in a file called weather-data-sorted.csv.

Some of the lines in the input file given have some invalid dates. Your program should not halt when it reaches one; instead your code should handle the error by printing the entire line followed by a colon, then a description of the error given in the exception, to an output file called error.log

Submit your two Java source files: Weather.java and WeatherData.java; your two output files: weather-data-sorted.csv and error.log; and your extra calculations file: calculations.txt. A total of 5 files.

public class WeatherData {

        private LocalDate date;
        private int maxTemp, avgTemp, minTemp;
        private int maxWindSpeed, avgWindSpeed;
        private int maxWindGust;
        private double precipitation;
        private int cloudCover;
        private String events;
        private int windDirection;

        public WeatherData(LocalDate date, int maxTemp, int avgTemp, int minTemp, int maxWindSpeed, int avgWindSpeed,
                        int maxWindGust, double precipitation, int cloudCover, String events, int windDirection) {
                this.date = date;
                this.maxTemp = maxTemp;
                this.avgTemp = avgTemp;
                this.minTemp = minTemp;
                this.maxWindSpeed = maxWindSpeed;
                this.avgWindSpeed = avgWindSpeed;
                this.maxWindGust = maxWindGust;
                this.precipitation = precipitation;
                this.cloudCover = cloudCover;
                this.events = events;
                this.windDirection = windDirection;
        }

        public LocalDate getDate() {
                return date;
        }

        public int getMaxTemp() {
                return maxTemp;
        }

        public void setMaxTemp(int maxTemp) {
                this.maxTemp = maxTemp;
        }

        public int getAvgTemp() {
                return avgTemp;
        }

        public void setAvgTemp(int avgTemp) {
                this.avgTemp = avgTemp;
        }

        public int getMinTemp() {
                return minTemp;
        }

        public void setMinTemp(int minTemp) {
                this.minTemp = minTemp;
        }

        public int getMaxWindSpeed() {
                return maxWindSpeed;
        }

        public void setMaxWindSpeed(int maxWindSpeed) {
                this.maxWindSpeed = maxWindSpeed;
        }

        public int getAvgWindSpeed() {
                return avgWindSpeed;
        }

        public void setAvgWindSpeed(int avgWindSpeed) {
                this.avgWindSpeed = avgWindSpeed;
        }

        public int getMaxWindGust() {
                return maxWindGust;
        }

        public void setMaxWindGust(int maxWindGust) {
                this.maxWindGust = maxWindGust;
        }

        public double getPrecipitation() {
                return precipitation;
        }

        public void setPrecipitation(double precipitation) {
                this.precipitation = precipitation;
        }

        public int getCloudCover() {
                return cloudCover;
        }

        public void setCloudCover(int cloudCover) {
                this.cloudCover = cloudCover;
        }

        public String getEvents() {
                return events;
        }

        public void setEvents(String events) {
                this.events = events;
        }

        public int getWindDirection() {
                return windDirection;
        }

        public void setWindDirection(int windDirection) {
                this.windDirection = windDirection;
        }

}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

//Weather.java

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Scanner;

public class Weather {

    public static ArrayList<WeatherData> loadData(String fileName) throws IOException {
        File f = new File(fileName);
        Scanner sc = new Scanner(f);
        FileWriter fw = new FileWriter(new File("error.log"));
        ArrayList<WeatherData> data = new ArrayList<>();
        String t = null;
        while (sc.hasNextLine()) {

            try {
                t = sc.nextLine();
                String values[] = t.trim().split(",");
                LocalDate date = LocalDate.parse(values[0], DateTimeFormatter.ofPattern("yyyy-MM-dd"));
                int maxTemp = Integer.valueOf(values[1].trim());
                int avgTemp = Integer.valueOf(values[2].trim());
                int minTemp = Integer.valueOf(values[3].trim());
                int maxWindSpeed = Integer.valueOf(values[4].trim());
                int avgWindSpeed = Integer.valueOf(values[5].trim());
                int maxWindGust = Integer.valueOf(values[6].trim());
                double precipitation = Double.valueOf(values[7].trim());
                int cloudCover = Integer.valueOf(values[8].trim());
                String events = values[9];
                int windDirection = Integer.valueOf(values[10].trim());
                data.add(new WeatherData(date, maxTemp, avgTemp, minTemp, maxWindSpeed, avgWindSpeed,
                        maxWindGust, precipitation, cloudCover, events, windDirection));

            } catch (Exception e) {
                // if any data corrupted it will be logged to log.error
                fw.write(t+"\n");
            }
        }
        fw.flush();
        fw.close();
        return data;
    }

    public static void sort(ArrayList<WeatherData> data) {
        // sorting the data based on LocalDate
        for (int i = 0; i < data.size(); i++) {
            for (int j = i + 1; j < data.size(); j++) {
                if (data.get(i).getDate().compareTo(data.get(j).getDate()) > 0) {
                    WeatherData temp = data.get(i);
                    data.set(i, data.get(j));
                    data.set(j, temp);
                }
            }
        }
    }


    public static void calculations(ArrayList<WeatherData> data) throws IOException{
        File f = new File("calculations.txt");
        FileWriter fw = new FileWriter(f);
        int maxTemp = Integer.MIN_VALUE, minTemp = Integer.MAX_VALUE, maxWindgust = Integer.MIN_VALUE;
        LocalDate maxTempDate = null, minTempDate = null,maxWindjustDate=null, maxPerDate=null;
        String event = null;
        double maxPerceptions = Double.MIN_VALUE;
        for(WeatherData wd: data){
            if(wd.getMaxTemp() > maxTemp){
                maxTemp = wd.getMaxTemp();
                maxTempDate = wd.getDate();
            }

            if(wd.getMinTemp() < minTemp){
                minTemp = wd.getMinTemp();
                minTempDate = wd.getDate();
            }
            if(wd.getMaxWindGust() > maxWindgust){
                maxWindgust = wd.getMaxWindGust();
                maxWindjustDate = wd.getDate();
            }
            if(wd.getPrecipitation() > maxPerceptions){
                maxPerceptions = wd.getPrecipitation();
                event = wd.getEvents();
                maxPerDate = wd.getDate();
            }
        }
        fw.write("Max Temp: "+maxTemp+", Date: "+maxTempDate.toString()+"\n");
        fw.write("Min Temp: "+minTemp+", Date: "+minTempDate.toString()+"\n");
        fw.write("Max Wind Gust: "+maxWindgust+", Date: "+maxWindjustDate.toString()+"\n");
        fw.write("Max precipitation: "+maxPerceptions+", Date: "+maxPerDate.toString()+", Event: "+event+"\n");
        fw.flush();
        fw.close();
    }

    public static void writeSortedData(ArrayList<WeatherData> data) throws IOException{
        File f = new File("weather-data-sorted.csv");
        FileWriter fw = new FileWriter(f);
        for(WeatherData wd: data){
            fw.write(wd.getDate().toString()+",");
            fw.write(wd.getMaxTemp()+",");
            fw.write(wd.getAvgTemp()+",");
            fw.write(wd.getMinTemp()+",");
            fw.write(wd.getMaxWindSpeed()+",");
            fw.write(wd.getAvgWindSpeed()+",");
            fw.write(wd.getMaxWindGust()+",");
            fw.write(wd.getPrecipitation()+",");
            fw.write(wd.getCloudCover()+",");
            fw.write(wd.getEvents()+",");
            fw.write(wd.getWindDirection()+"\n");
        }
        fw.flush();
        fw.close();

    }


}

//WeatherData.java

public class WeatherData {

    private LocalDate date;
    private int maxTemp, avgTemp, minTemp;
    private int maxWindSpeed, avgWindSpeed;
    private int maxWindGust;
    private double precipitation;
    private int cloudCover;
    private String events;
    private int windDirection;

    public WeatherData(LocalDate date, int maxTemp, int avgTemp, int minTemp, int maxWindSpeed, int avgWindSpeed,
                       int maxWindGust, double precipitation, int cloudCover, String events, int windDirection) {
        this.date = date;
        this.maxTemp = maxTemp;
        this.avgTemp = avgTemp;
        this.minTemp = minTemp;
        this.maxWindSpeed = maxWindSpeed;
        this.avgWindSpeed = avgWindSpeed;
        this.maxWindGust = maxWindGust;
        this.precipitation = precipitation;
        this.cloudCover = cloudCover;
        this.events = events;
        this.windDirection = windDirection;
    }

    public LocalDate getDate() {
        return date;
    }

    public int getMaxTemp() {
        return maxTemp;
    }

    public void setMaxTemp(int maxTemp) {
        this.maxTemp = maxTemp;
    }

    public int getAvgTemp() {
        return avgTemp;
    }

    public void setAvgTemp(int avgTemp) {
        this.avgTemp = avgTemp;
    }

    public int getMinTemp() {
        return minTemp;
    }

    public void setMinTemp(int minTemp) {
        this.minTemp = minTemp;
    }

    public int getMaxWindSpeed() {
        return maxWindSpeed;
    }

    public void setMaxWindSpeed(int maxWindSpeed) {
        this.maxWindSpeed = maxWindSpeed;
    }

    public int getAvgWindSpeed() {
        return avgWindSpeed;
    }

    public void setAvgWindSpeed(int avgWindSpeed) {
        this.avgWindSpeed = avgWindSpeed;
    }

    public int getMaxWindGust() {
        return maxWindGust;
    }

    public void setMaxWindGust(int maxWindGust) {
        this.maxWindGust = maxWindGust;
    }

    public double getPrecipitation() {
        return precipitation;
    }

    public void setPrecipitation(double precipitation) {
        this.precipitation = precipitation;
    }

    public int getCloudCover() {
        return cloudCover;
    }

    public void setCloudCover(int cloudCover) {
        this.cloudCover = cloudCover;
    }

    public String getEvents() {
        return events;
    }

    public void setEvents(String events) {
        this.events = events;
    }

    public int getWindDirection() {
        return windDirection;
    }

    public void setWindDirection(int windDirection) {
        this.windDirection = windDirection;
    }

}

// Please run the code with data it wil generate the required files

//Tester.java

import java.io.IOException;
import java.util.ArrayList;

public class Tester {
    public static void main(String[] args) throws IOException {
        // reading data
        // TODO: update the file name as per urs
        String fileName = "weather.txt";
        // reading data into array of WeatherData
        System.out.println("Loading data..........");
        ArrayList<WeatherData> data = Weather.loadData(fileName);
        // generating calculations
        System.out.println("Calculating stats.......and writing to calculations.txt");
        Weather.calculations(data);
        //sorting the data
        Weather.sort(data);
        // writing sorted data
        System.out.println("Writing sorted data....to weather-data-sorted.csv");
        Weather.writeSortedData(data);

    }
}

//OUT

Loading data..........
Calculating stats.......and writing to calculations.txt
Writing sorted data....to weather-data-sorted.csv

calculation.txt

Max Temp: 73, Date: 2015-05-08
Min Temp: 26, Date: 2016-02-02
Max Wind Gust: 39, Date: 2015-12-12
Max precipitation: 0.09, Date: 2015-12-12, Event: Rain

weather-data-sorted.csv

2015-05-08,73,56,38,18,7,22,0.0,0,,75
2015-11-10,51,44,37,18,8,21,0.0,4,,261
2015-12-12,47,43,38,32,12,39,0.09,7,Rain,205
2016-02-02,45,36,26,12,5,14,0.0,1,,111

error.log

2015-06-28,109,89,68,38,11,48,T,0,Rain-Thunderstorm,296
Add a comment
Know the answer?
Add Answer to:
Create a program, Weather.java, which loads an unsorted set of weather data for Pendleton, sorts the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Modify the library program as follows: Create a method in the LibraryMaterial class that accepts a...

    Modify the library program as follows: Create a method in the LibraryMaterial class that accepts a string s as a parameter and returns true if the title of the string is equal to s. Create the same method for your library material copies. Note that it will need to be abstract in the LibraryMaterialCopy class MY CODE **************************************************************** LibraryCard: import java.util.List; import java.util.ArrayList; import java.time.LocalDate; import    java.time.temporal.ChronoUnit; public class LibraryCard {    private String id;    private String cardholderName;   ...

  • Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. E...

    Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. Execute the sort algorithms against the same list, recording information for the total number of comparisons and total execution time for each algorithm. Try several different lists, including at least one that is already in sorted order. ---------------------------------------------------------------------------------------------------------------- /** * Sorting demonstrates sorting and searching on an...

  • c#, i have multiple arrays, When i click the submit button i want all the data...

    c#, i have multiple arrays, When i click the submit button i want all the data in the textboxes and the date to store in those arrays respectively. after that it prompts the user a yes or no dialog where if yes is clicked the user enters more data and gets stored in the arrays(and loops if yes is chosen again.) when no is clicked the form closes. how do i do this? For example this is the first entry:...

  • Need Help finishing up MyCalendar Program: Here are the methods: Modifier and Type Method Desc...

    Need Help finishing up MyCalendar Program: Here are the methods: Modifier and Type Method Description boolean add​(Event evt) Add an event to the calendar Event get​(int i) Fetch the ith Event added to the calendar Event get​(java.lang.String name) Fetch the first Event in the calendar whose eventName is equal to the given name java.util.ArrayList<Event> list() The list of all Events in the order that they were inserted into the calendar int size() The number of events in the calendar java.util.ArrayList<Event>...

  • I need to create a code for this prompt: In this project we will build a...

    I need to create a code for this prompt: In this project we will build a generic UserInput class for getting keyboard input from the user. Implementation: The class UserInput is a 'Methods only' class, and all the methods should be declared static. Look at the TestScanner.java program at the bottom of this page that inputs a string, int and double. It shows you how to use Scanner class to get input from the keyboard. Write FOUR simple methods, one...

  • Help! Not sure how to create this java program to run efficiently. Current code and assignment...

    Help! Not sure how to create this java program to run efficiently. Current code and assignment attached. Assignment :Write a class Movies.java that reads in a movie list file (movies.txt). The file must contain the following fields: name, genre, and time. Provide the user with the options to sort on each field. Allow the user to continue to sort the list until they enter the exit option. ---------------------------------------------------------- import java.util.*; import java.io.*; public class Movies { String movieTitle; String movieGenre;...

  • Set-Up · Create a new project in your Eclipse workspace named: Lab13 · In the src...

    Set-Up · Create a new project in your Eclipse workspace named: Lab13 · In the src folder, create a package named: edu.ilstu · Import the following files from T:\it168\Labs\lab13. Note that the .txt file must be in the top level of your project, not inside your src folder. o Student.java o StudentList.java o students.txt Carefully examine the Student.java and StudentList.java files. You are going to complete the StudentList class (updating all necessary Javadoc comments), following the instruction in the code....

  • FOR JAVA: Summary: Create a program that adds students to the class list (see below). The...

    FOR JAVA: Summary: Create a program that adds students to the class list (see below). The solution should be named Roster402_v2.java. Allow the user to control the number of students added to the roster. Ask if the user would like to see their new roster to confirm additions. If yes, then display contents of the file, if no, end the program. ------------------------------------------------------------------------------------- List of student names and IDs for class (this will be your separate text file): Jones, Jim,45 Hicks,...

  • Please create two tests classes for the code down below that use all of the methods...

    Please create two tests classes for the code down below that use all of the methods in the Transaction class, and all of the non-inherited methods in the derived class. Overridden methods must be tested again for the derived classes. The first test class should be a traditional test class named "Test1" while the second test class should be a JUnit test class named "Test2". All I need is to test the classes, thanks! Use a package that contains the...

  • Create a program called GeneralizedBubbleSort that will make use of the Comparable Interface in the same...

    Create a program called GeneralizedBubbleSort that will make use of the Comparable Interface in the same way it is used in the GeneralizedSelectionSort. You should use the attached ComparableDemo to test your program. public class ComparableDemo { public static void main(String[] args) { Double[] d = new Double[10]; for (int i = 0; i < d.length; i++) d[i] = new Double(d.length - i); System.out.println("Before sorting:"); int i; for (i = 0; i < d.length; i++) System.out.print(d[i].doubleValue( ) + ", ");...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT