Question
COS 160- Weather Analysis Part 1: In Program 9 you wrote methods to work on arrays. Copy those methods into this program and use them
COS 160- Weather Analysis
Part 1:
In Program 9 you wrote methods to work on arrays. Copy those methods into this program and use them to calculate and print:
The highest temperature in tmax and the date it occured on (use arrayMax(), arrayFirstIndexOf(), and then use that index for your month[], day[], and year[] arrays)
The lowest temperature in tmin and the date it occured on
The average tmax (As a check, your value should be 55.596... If you are not getting this value, there is something wrong with your code.)
The average tmin
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 months = allDates.nextInt(); int days = allDates.nextInt(); int years = allDates.nextInt(); int tmaxs = allDates.nextInt(); int tmins = allDates.nextInt(); month[i] = months; day[i] = days; year[i] = years; tmax[i] = tmaxs; tmin[i] = tmins; i++; } int max = arrayMax(tmax); System.out.println("Maximum value: " + max); int occurs = firstOccurrence(tmax, max); System.out.println(occurs); } 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 max) { for (int i = 0; i < a.length; i++) { if(a[i] == max) { return i; } } return -1; } //EDIT arrayMonth public static int arrayMonth() { }
//EDIT arrayDay
public static int arrayDay() { }
//EDIT arrayYear
public static int arrayYear() { } 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 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; }
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started