Question: COS 160- Using Java programming We are now going to analyze the data to look for long-range weather trends such as evidence of climate change.
COS 160- Using Java programming
We are now going to analyze the data to look for long-range weather trends such as evidence of climate change. First we are going to find the ranges of years corresponding to each decade in our file. The data starts in 1941 and ends in 2016, thus the 1940's and 2010's aren't complete decades but we have enough data that we want to include them in our analysis. Your task for Part 4 is to create a loop that prints:
| Decades: 1940's [1941-1949] 1950's [1950-1959] 1960's [1960-1969] 1970's [1970-1979] 1980's [1980-1989] 1990's [1990-1999] 2000's [2000-2009] 2010's [2010-2016] |
There are many ways to write this loop.
If you want to make it general you can use year[0] to get the starting year and year[n-1] to get the last year. (I used the variable n for the number of data records.) Note, you will need special case tests for the starting year and last year because those aren't full decades.
If you just want to get this working in the simplest fashion possible, you can put the starting and ending years of each decade in arrays. (Although that would be considered poor code because someone would then have to change those arrays if they wanted to process a different file.)
However you choose to do it, it will be best for the next part if you just have a single loop and a single printf() statement.
Code:
import java.io.*; import java.util.*;
public class program10 {
public static void main(String[] args) throws FileNotFoundException { // TODO Auto-generated method stub Scanner input = new Scanner(new File("PortlandWeather1941to2016.txt")); int [] month = new int [27762]; int [] day = new int [27762]; int [] year = new int [27762]; int [] tmax = new int [27762]; int [] tmin = new int [27762]; int i = 0; input.nextLine(); input.nextLine(); input.nextLine(); while(input.hasNext()) { String dates = input.nextLine(); Scanner allDates = new Scanner (dates); allDates.useDelimiter("[/ \t ]+"); int newMonth = allDates.nextInt(); int newDay = allDates.nextInt(); int newYear = allDates.nextInt(); int newTmax = allDates.nextInt(); int newTmin = allDates.nextInt(); month[i] = newMonth; day[i] = newDay; year[i] = newYear; tmax[i] = newTmax; tmin[i] = newTmin; i++; } int max = arrayMax(tmax); System.out.println("The highest temperature: " + max); int findMonth = arrayMonth(month, tmax, max); int findDay = arrayDay(day, tmax, max); int findYear = arrayYear(year, tmax, max); System.out.println("The temperature occured on: " + findMonth + "/" + findDay + "/" + findYear); int min = arrayMin(tmin); System.out.println("The lowest temperature: " + min); int findM = arrayMonth2(month, tmin, min); int findD = arrayDay2(day, tmin, min); int findY = arrayYear2(year, tmin, min); System.out.println("The temperature occured on: " + findM + "/" + findD + "/" + findY); double average = arrayAverage(tmax); System.out.println("Average TMAX temperature: " + average); double average2 = arrayAverage(tmin); System.out.println("Average TMIN temperature: " + average2); } public static int arrayMax(int[] a) { int max = a[0];
for (int i = 0; i < a.length; i++) if (a[i] > max) max = a[i];
return max; } public static int firstOccurrence(int[] a, int temp) { for (int i = 0; i < a.length; i++) { if(a[i] == temp) { return i; } } return -1; } public static int arrayMonth(int[] month, int[] a,int max) { int f = firstOccurrence(a, max); return month[f]; } public static int arrayDay(int day[], int[] a, int max) { int f = firstOccurrence(a, max); return day[f]; } public static int arrayYear(int year[], int[] a, int max) { int f = firstOccurrence(a, max); return year[f]; } public static int arrayMin(int[] a) { int min = a[0]; for (int i = 0; i < a.length; i++) { if (a[i] < min) min = a[i]; } return min; } public static int arrayMonth2(int[] month, int[] a,int min) { int f = firstOccurrence(a, min); return month[f]; } public static int arrayDay2(int day[], int[] a, int min) { int f = firstOccurrence(a, min); return day[f]; } public static int arrayYear2(int year[], int[] a, int min) { int f = firstOccurrence(a, min); return year[f]; } public static double arrayAverage(int[] a) { int sum = 0; for (int i = 0; i < a.length; i++) { sum = sum + a[i]; } double average = (double)sum/a.length; return average; } }
File: PortlandWeather1941to2016.txt
27759 data records DATE TMAX TMIN ---------- ---- ---- 01/01/1941 38 25 01/02/1941 32 20 01/03/1941 31 22 01/04/1941 34 25 01/05/1941 32 20 01/06/1941 29 5 01/01/1951 34 10 01/02/1951 40 16 01/03/1951 46 33 01/04/1951 52 36 01/05/1951 36 21 01/06/1951 40 18 01/01/1960 34 6 01/02/1960 37 0 01/03/1960 48 36 01/04/1960 37 27 01/05/1960 33 15 01/06/1960 27 8 01/01/1970 29 11 01/02/1970 26 6 01/03/1970 30 8 01/04/1970 26 12 01/05/1970 29 3 01/06/1970 29 1 01/01/1980 33 13 01/02/1980 38 18 01/03/1980 27 10 01/04/1980 28 4 01/05/1980 21 14 01/06/1980 25 6 01/01/1990 42 23 01/02/1990 37 18 01/03/1990 43 19 01/04/1990 43 27 01/05/1990 44 25 01/06/1990 37 20 01/01/2000 42 11 01/02/2000 39 27 01/03/2000 59 34 01/04/2000 44 30 01/05/2000 44 15 01/06/2000 32 11 01/01/2001 32 18 01/02/2001 27 6 01/03/2001 27 1 01/04/2001 29 7 01/05/2001 25 2 01/06/2001 33 21 01/01/2010 33 24 01/02/2010 26 23 01/03/2010 34 23 01/04/2010 35 29 01/05/2010 33 28 01/06/2010 34 20 01/01/2011 54 29 01/02/2011 44 37 01/03/2011 40 22 01/04/2011 33 17 01/05/2011 35 17 01/06/2011 30 13
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
