Advertisement
Guest User

ICSClimateChange-Armand

a guest
Oct 23rd, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.42 KB | None | 0 0
  1. import java.io.*;
  2. import java.text.*;
  3. import java.util.*;
  4.  
  5. public class ICSClimateChange {
  6.  
  7.     public static void main(String[] args) throws IOException {
  8.         Scanner sc = new Scanner(new FileReader("temperatures.txt")); // read from temperatures.txt
  9.         NumberFormat d = new DecimalFormat("0.0");
  10.  
  11.         int temp[] = new int[365]; // int variable for 365 temp variables
  12.         int total = 0; // int variable for temp total
  13.         int frequency[] = new int[61]; // int variable array for frequency
  14.         int largestFreq = 0; // int variable for highest frequency
  15.        
  16.         for (int x = 0; x < 365; x++) // read temperatures in and add to total
  17.         {
  18.             temp[x] = sc.nextInt();
  19.             total += temp[x];
  20.         }
  21.  
  22.         sc.close();
  23.         Arrays.sort(temp); // sorts temperature from highest to lowest
  24.  
  25.         tempFrequency(temp, frequency); // call tempFrequency method
  26.  
  27.         System.out.println("Climate Change");
  28.         System.out.println("Temperature" + "\t" + "Frequency");
  29.         System.out.println();
  30.  
  31.         for (int x = 0; x < 61; x++) // output the temperatures and their frequency
  32.            
  33.         {
  34.             System.out.println("\t" + (x - 20) + "\t\t" + frequency[x]);
  35.         }
  36.         System.out.println();
  37.         System.out.println("Average Temperature" + "\t" + d.format(tempAvg(total))); // print the average temperature
  38.  
  39.         System.out.println("Highest Frequency" + "\t" + calcFreq(largestFreq, frequency)); // display highest frequency, works with "+ largestFreq" as well
  40.  
  41.         for (int x = 0; x < 61; x++) // find if there are multiple highest frequencies
  42.                                        
  43.         {
  44.             if (calcFreq(largestFreq, frequency) == frequency[x]) {
  45.                 System.out.println("Mode Temperature " + "\t" + (x - 20)); // print out temperatures for each highest frequency
  46.             }
  47.         }
  48.     }
  49.  
  50.     public static void tempFrequency(int temp[], int frequency[]) // find frequencies of each temperature
  51.                                                                
  52.     {
  53.         for (int x = 0; x < 365; x++) {
  54.             for (int i = 0; i < 61; i++) {
  55.                 if (temp[x] == (i - 20)) {
  56.                     frequency[i] += 1;
  57.  
  58.                 }
  59.             }
  60.         }
  61.     }
  62.  
  63.     public static double tempAvg(int total) // calculate average temperature
  64.     {
  65.         double avg = (total / 365);
  66.         return avg;
  67.     }
  68.  
  69.     public static int calcFreq(int largestFreq, int frequency[]) {
  70.         largestFreq = frequency[0]; // set first number to highest and compare wit each temperature for highest frequency
  71.         for (int x = 0; x < 60; x++)
  72.                                        
  73.         {
  74.             if (largestFreq < frequency[x + 1]) {
  75.                 largestFreq = frequency[x + 1];
  76.             }
  77.         }
  78.         return largestFreq;
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement