Advertisement
Guest User

Untitled

a guest
Jan 30th, 2015
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.79 KB | None | 0 0
  1. //Mitchell Smith
  2. //Homework 2 - Performing and formatting weather calculations
  3. package homework2;
  4. import java.io.File;
  5. import java.io.IOException;
  6. import java.util.Scanner;
  7.  
  8. public class Homework_2 {
  9.  
  10. public static void main(String[] args) throws IOException {
  11.  
  12. System.out.println("Mitchell Smith\nCOS225 Assgn2\nThis program computes temperature statistics\n");
  13.  
  14. String town, //Town weather was reported in
  15. date = null, //Date weather was reported. MM/DD/YY format
  16. direction, //Direction of wind
  17. weather_data, //Entire line of weather data
  18. temp_type, //C or F degrees
  19. avgmon; //Months used in averages.txt
  20. double temp = 0, //Temperature in weather.txt
  21. wind = 0, //Speed of wind in weather.txt
  22. celcius, //Temperature in celcius
  23. fahrenheit, //Temperature in fahrenheit
  24. chill, //Result of chill equation
  25. windavg = 0, //Average of all wind
  26. favg = 0, //Average of fahrenheit temp
  27. cavg = 0, //Average of celcius temp
  28. avgtemp, //Average temps per month from averages.txt
  29. realavgtemp = 0; //Average temp of month used
  30. int count = 0, //A count to keep track of # of lines in weather.txt
  31. istrue = 1, //If everything works this value is 1, if something fails value is 0 and prints N/A for values
  32. month; //Numerical month, 1-12
  33.  
  34.  
  35.  
  36. String divider = "-------------------------------------------------------------------------------------------------";
  37.  
  38. Scanner file_reader = new Scanner(new File("weather.txt"));
  39. Scanner average_reader = new Scanner(new File("averages.txt"));
  40.  
  41.  
  42. System.out.println("Town Date Wind Speed Direction F C Wind Chill");
  43. System.out.println(divider);
  44.  
  45. while(file_reader.hasNext()) //If the next line exists
  46. {
  47. weather_data = file_reader.nextLine();
  48. Scanner line_scanner = new Scanner(weather_data); //Search the next line
  49.  
  50. while(line_scanner.hasNext()){
  51. town = line_scanner.next();
  52. date = line_scanner.next();
  53.  
  54. if (line_scanner.hasNextDouble()){
  55. wind = line_scanner.nextDouble();
  56. }
  57.  
  58. else{ //If data isn't there set istrue to not 1
  59. wind = 404; //Set wind to 404 to ensure user knows error
  60. istrue = 0;
  61. }
  62.  
  63. direction = line_scanner.next();
  64.  
  65. if (line_scanner.hasNextDouble()){
  66. temp = line_scanner.nextDouble();
  67. }
  68.  
  69. else{
  70. temp = 404; //If data isn't there set istrue to not 1
  71. istrue = 0; //Set temp to 404 to show error
  72. }
  73. temp_type = line_scanner.next();
  74. String weatherManFirst = line_scanner.next(); //These aren't used, I just can't work around it...
  75. String weatherManLast = line_scanner.next(); //...without killing the entire program
  76. count++;
  77.  
  78. if (temp_type.equals("C")){ //Converts C -> F and assigns both to seperate doubles
  79. celcius= temp;
  80. fahrenheit = toFahrenheit(celcius);
  81. }
  82. else{ //Converts F-> C and assigns both to seperate doubles
  83. fahrenheit = temp;
  84. celcius = toCelsius(fahrenheit);
  85. }
  86. windavg += wind; //Gets the sum of the wind
  87. favg += fahrenheit;
  88. cavg += celcius;
  89. chill = getWindChill(celcius, wind);
  90.  
  91. System.out.printf("%-17s %4s %10.2f %13s %13.2f %14.2f %15.2f\n",town, date, wind, direction, fahrenheit, celcius, chill);
  92.  
  93. }
  94. line_scanner.close();
  95. }
  96. String[] parts = date.split("/"); //Splits into MM, DD, YY
  97. date = parts[0]; //Grabs the first chunk, aka MM
  98. month = Integer.parseInt(date); //Saves as an int
  99. date = checkMonth(month); //Changes int month to string
  100. System.out.println("\n\nAll readings taken for month: "+date);
  101. System.out.println("Number of readings: "+count);
  102.  
  103. if( istrue == 1) //If everything works as intended give the averages
  104. {
  105. windavg = Double.parseDouble(getAverage(count,windavg));
  106. favg = Double.parseDouble(getAverage(count,favg));
  107. cavg = Double.parseDouble(getAverage(count,cavg));
  108. System.out.printf("Average wind speed: %.2f",windavg);
  109. System.out.printf("\nAverage temperature (F): %.2f ",favg);
  110. System.out.printf("\nAverage temperature (C): %.2f",cavg);
  111. }
  112. else //Print N/A if a data type is non-existant
  113. {
  114. System.out.println("Average wind speed: N/A");
  115. System.out.println("Average temperature (F): N/A");
  116. System.out.println("Average temperature (C): N/A");
  117. }
  118. System.out.println(" \n");
  119. System.out.println("Historical Averages");
  120. System.out.println("Month Temp Average (F)");
  121. System.out.println("-------------------------------------------------");
  122. for(int ct1= 0; ct1 <=11;ct1++){ //1-12
  123. avgmon = average_reader.next();
  124. avgtemp = average_reader.nextDouble();
  125. if(month-1 == ct1) //ct1 starts at 0 so month-1 is just setting both to start at 0
  126. {
  127. realavgtemp = avgtemp; //If ct1 and month-1 coincide assign avgtemp to a more perminent state
  128. }
  129. System.out.printf("%-10s %10.2f \n",avgmon,avgtemp);
  130. }
  131. if (istrue == 1) //If all values were there continue as normal
  132. {
  133. favg -= realavgtemp;
  134. System.out.printf("\nThe difference this "+date+" from historical average is %.2f",favg);
  135. }
  136. else //If a value was missing
  137. {
  138. System.out.println("\nThe difference this "+date+" from historical average is N/A");
  139. }
  140. file_reader.close();
  141. average_reader.close();
  142. }
  143.  
  144.  
  145. private static double getWindChill(double celcius, double wind) { //POST: Will return value of windchill
  146. return 33 - ((10*(Math.sqrt(wind))-wind+10.5)*(33-celcius)/23.1);
  147. }
  148.  
  149. private static double toCelsius(double fahrenheit) { //POST: Will return celcius degrees
  150. return (5.0/9.0)*(fahrenheit-32.0);
  151. }
  152.  
  153. private static double toFahrenheit(double celcius) { //POST: Will return fahrenheit degrees
  154. return 1.8*celcius+32.0;
  155. }
  156. private static String getAverage(int count, double sum){ //PRE: Count has to be greater than 0
  157. double avg; //POST: Will return average
  158. String outavg;
  159. {
  160. avg = sum/count;
  161. outavg = String.valueOf(avg);
  162. return outavg;
  163. }
  164. }
  165. private static String checkMonth(int month) //PRE: Int from 1-12
  166. { //POST: Return a string for the month
  167. String[] months = {"January","Febuary","March","April","May","June","July",
  168. "August","September","October","November","December"};
  169. return months[month-1];
  170. }
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement