Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.text.*;
- import java.util.*;
- public class ICSClimateChange {
- public static void main(String[] args) throws IOException {
- Scanner sc = new Scanner(new FileReader("temperatures.txt")); // read from temperatures.txt
- NumberFormat d = new DecimalFormat("0.0");
- int temp[] = new int[365]; // int variable for 365 temp variables
- int total = 0; // int variable for temp total
- int frequency[] = new int[61]; // int variable array for frequency
- int largestFreq = 0; // int variable for highest frequency
- for (int x = 0; x < 365; x++) // read temperatures in and add to total
- {
- temp[x] = sc.nextInt();
- total += temp[x];
- }
- sc.close();
- Arrays.sort(temp); // sorts temperature from highest to lowest
- tempFrequency(temp, frequency); // call tempFrequency method
- System.out.println("Climate Change");
- System.out.println("Temperature" + "\t" + "Frequency");
- System.out.println();
- for (int x = 0; x < 61; x++) // output the temperatures and their frequency
- {
- System.out.println("\t" + (x - 20) + "\t\t" + frequency[x]);
- }
- System.out.println();
- System.out.println("Average Temperature" + "\t" + d.format(tempAvg(total))); // print the average temperature
- System.out.println("Highest Frequency" + "\t" + calcFreq(largestFreq, frequency)); // display highest frequency, works with "+ largestFreq" as well
- for (int x = 0; x < 61; x++) // find if there are multiple highest frequencies
- {
- if (calcFreq(largestFreq, frequency) == frequency[x]) {
- System.out.println("Mode Temperature " + "\t" + (x - 20)); // print out temperatures for each highest frequency
- }
- }
- }
- public static void tempFrequency(int temp[], int frequency[]) // find frequencies of each temperature
- {
- for (int x = 0; x < 365; x++) {
- for (int i = 0; i < 61; i++) {
- if (temp[x] == (i - 20)) {
- frequency[i] += 1;
- }
- }
- }
- }
- public static double tempAvg(int total) // calculate average temperature
- {
- double avg = (total / 365);
- return avg;
- }
- public static int calcFreq(int largestFreq, int frequency[]) {
- largestFreq = frequency[0]; // set first number to highest and compare wit each temperature for highest frequency
- for (int x = 0; x < 60; x++)
- {
- if (largestFreq < frequency[x + 1]) {
- largestFreq = frequency[x + 1];
- }
- }
- return largestFreq;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement