Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Mitchell Smith
- //Homework 2 - Performing and formatting weather calculations
- package homework2;
- import java.io.File;
- import java.io.IOException;
- import java.util.Scanner;
- public class Homework_2 {
- public static void main(String[] args) throws IOException {
- System.out.println("Mitchell Smith\nCOS225 Assgn2\nThis program computes temperature statistics\n");
- String town, //Town weather was reported in
- date = null, //Date weather was reported. MM/DD/YY format
- direction, //Direction of wind
- weather_data, //Entire line of weather data
- temp_type, //C or F degrees
- avgmon; //Months used in averages.txt
- double temp = 0, //Temperature in weather.txt
- wind = 0, //Speed of wind in weather.txt
- celcius, //Temperature in celcius
- fahrenheit, //Temperature in fahrenheit
- chill, //Result of chill equation
- windavg = 0, //Average of all wind
- favg = 0, //Average of fahrenheit temp
- cavg = 0, //Average of celcius temp
- avgtemp, //Average temps per month from averages.txt
- realavgtemp = 0; //Average temp of month used
- int count = 0, //A count to keep track of # of lines in weather.txt
- istrue = 1, //If everything works this value is 1, if something fails value is 0 and prints N/A for values
- month; //Numerical month, 1-12
- String divider = "-------------------------------------------------------------------------------------------------";
- Scanner file_reader = new Scanner(new File("weather.txt"));
- Scanner average_reader = new Scanner(new File("averages.txt"));
- System.out.println("Town Date Wind Speed Direction F C Wind Chill");
- System.out.println(divider);
- while(file_reader.hasNext()) //If the next line exists
- {
- weather_data = file_reader.nextLine();
- Scanner line_scanner = new Scanner(weather_data); //Search the next line
- while(line_scanner.hasNext()){
- town = line_scanner.next();
- date = line_scanner.next();
- if (line_scanner.hasNextDouble()){
- wind = line_scanner.nextDouble();
- }
- else{ //If data isn't there set istrue to not 1
- wind = 404; //Set wind to 404 to ensure user knows error
- istrue = 0;
- }
- direction = line_scanner.next();
- if (line_scanner.hasNextDouble()){
- temp = line_scanner.nextDouble();
- }
- else{
- temp = 404; //If data isn't there set istrue to not 1
- istrue = 0; //Set temp to 404 to show error
- }
- temp_type = line_scanner.next();
- String weatherManFirst = line_scanner.next(); //These aren't used, I just can't work around it...
- String weatherManLast = line_scanner.next(); //...without killing the entire program
- count++;
- if (temp_type.equals("C")){ //Converts C -> F and assigns both to seperate doubles
- celcius= temp;
- fahrenheit = toFahrenheit(celcius);
- }
- else{ //Converts F-> C and assigns both to seperate doubles
- fahrenheit = temp;
- celcius = toCelsius(fahrenheit);
- }
- windavg += wind; //Gets the sum of the wind
- favg += fahrenheit;
- cavg += celcius;
- chill = getWindChill(celcius, wind);
- System.out.printf("%-17s %4s %10.2f %13s %13.2f %14.2f %15.2f\n",town, date, wind, direction, fahrenheit, celcius, chill);
- }
- line_scanner.close();
- }
- String[] parts = date.split("/"); //Splits into MM, DD, YY
- date = parts[0]; //Grabs the first chunk, aka MM
- month = Integer.parseInt(date); //Saves as an int
- date = checkMonth(month); //Changes int month to string
- System.out.println("\n\nAll readings taken for month: "+date);
- System.out.println("Number of readings: "+count);
- if( istrue == 1) //If everything works as intended give the averages
- {
- windavg = Double.parseDouble(getAverage(count,windavg));
- favg = Double.parseDouble(getAverage(count,favg));
- cavg = Double.parseDouble(getAverage(count,cavg));
- System.out.printf("Average wind speed: %.2f",windavg);
- System.out.printf("\nAverage temperature (F): %.2f ",favg);
- System.out.printf("\nAverage temperature (C): %.2f",cavg);
- }
- else //Print N/A if a data type is non-existant
- {
- System.out.println("Average wind speed: N/A");
- System.out.println("Average temperature (F): N/A");
- System.out.println("Average temperature (C): N/A");
- }
- System.out.println(" \n");
- System.out.println("Historical Averages");
- System.out.println("Month Temp Average (F)");
- System.out.println("-------------------------------------------------");
- for(int ct1= 0; ct1 <=11;ct1++){ //1-12
- avgmon = average_reader.next();
- avgtemp = average_reader.nextDouble();
- if(month-1 == ct1) //ct1 starts at 0 so month-1 is just setting both to start at 0
- {
- realavgtemp = avgtemp; //If ct1 and month-1 coincide assign avgtemp to a more perminent state
- }
- System.out.printf("%-10s %10.2f \n",avgmon,avgtemp);
- }
- if (istrue == 1) //If all values were there continue as normal
- {
- favg -= realavgtemp;
- System.out.printf("\nThe difference this "+date+" from historical average is %.2f",favg);
- }
- else //If a value was missing
- {
- System.out.println("\nThe difference this "+date+" from historical average is N/A");
- }
- file_reader.close();
- average_reader.close();
- }
- private static double getWindChill(double celcius, double wind) { //POST: Will return value of windchill
- return 33 - ((10*(Math.sqrt(wind))-wind+10.5)*(33-celcius)/23.1);
- }
- private static double toCelsius(double fahrenheit) { //POST: Will return celcius degrees
- return (5.0/9.0)*(fahrenheit-32.0);
- }
- private static double toFahrenheit(double celcius) { //POST: Will return fahrenheit degrees
- return 1.8*celcius+32.0;
- }
- private static String getAverage(int count, double sum){ //PRE: Count has to be greater than 0
- double avg; //POST: Will return average
- String outavg;
- {
- avg = sum/count;
- outavg = String.valueOf(avg);
- return outavg;
- }
- }
- private static String checkMonth(int month) //PRE: Int from 1-12
- { //POST: Return a string for the month
- String[] months = {"January","Febuary","March","April","May","June","July",
- "August","September","October","November","December"};
- return months[month-1];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement