Weather Forecast – Java

Weather Forecast – Java

Includes methods that can convert temperatures to and from Celsius, Fahrenheit, and Kelvin. Weather object variables include temperature, the condition of the sky, and the chance of rain.

Test Harness:

import java.io.FileNotFoundException;
import java.io.IOException;

public class WeatherForecast {

	public static void main(String[] args) throws IOException {
		
		Forecast albuquerque = new Forecast(); // Default constructor
		albuquerque.print(); // Testing print method
		
		Forecast orlando = new Forecast(85.0, "partly-cloudy", 80); // 3-arg constructor
		orlando.print(); 
		
		/*
		 * Testing getters and setters for each variable.
		 */
		
		orlando.setTemperature(87.0);
		System.out.println("Orlando's temperature is: " + orlando.getTemperature());
		
		orlando.setSkyCondition("clear skies");
		System.out.println("Orlando's sky condition is: " + orlando.getSkyCondition());
		
		orlando.setChanceOfRain(15);
		System.out.println("Chance of rain in Orlando is: " + orlando.getChanceOfRain() + "%");
		System.out.println();
		
		/*
		 * Testing temperature conversion methods.
		 */
		
		orlando.setTemperature(orlando.fahrenheitToCelsius(orlando.getTemperature()));
		System.out.print("Fahrenheit to celsius: ");
		System.out.println(orlando.getTemperature());
		
		orlando.setTemperature(orlando.celsiusToFahrenheit(orlando.getTemperature()));
		System.out.print("Celsius to fahrenheit: ");
		System.out.println(orlando.getTemperature());
		System.out.println();
		
		orlando.setTemperature(orlando.fahrenheitToKelvin(orlando.getTemperature()));
		System.out.print("Fahrenheit to Kelvin: ");
		System.out.println(orlando.getTemperature());
		
		orlando.setTemperature(orlando.kelvinToFahrenheit(orlando.getTemperature()));
		System.out.print("Kelvin to Fahrenheit: ");
		System.out.println(orlando.getTemperature());
		System.out.println();
		orlando.print();
		
		/*
		 * Testing remaining methods.
		 */
		
		System.out.println("Is it raining in Orlando? " + orlando.isRaining());
		
		try {
			orlando.printToFile();
			System.out.println("File output success!");
		} catch (FileNotFoundException e1) {
			System.out.println("Error! FileNotFoundException! No file was created.");
			e1.printStackTrace();
		} catch (IOException e2) {
			System.out.println("Error! IOException! No file was created.");
			e2.printStackTrace();
		} catch (Throwable e3) {
			System.out.println("Error! No file was created.");
			e3.printStackTrace();
		}
	
	}

}

Weather Object:

import java.io.FileWriter;
import java.io.IOException;

public class Forecast {
	

	private double temperature;
	private String skyCondition;
	private int    chanceOfRain;
	
	public Forecast() {	
		
		temperature  = 72.0;
		skyCondition = "clear";
		chanceOfRain = 0;
	}
	
	public Forecast(double temperature, 
					String skyCondition, 
					int    chanceOfRain) {
		
		this.temperature  =  temperature;
		this.skyCondition = skyCondition;
		this.chanceOfRain = chanceOfRain;
	}
	
	public double getTemperature() {
		return temperature;
	}
	public void setTemperature(double temperature) {
		this.temperature = temperature;
	}
	public String getSkyCondition() {
		return skyCondition;
	}
	public void setSkyCondition(String skyCondition) {
		this.skyCondition = skyCondition;
	}
	public int getChanceOfRain() {
		return chanceOfRain;
	}
	public void setChanceOfRain(int chanceOfRain) {
		this.chanceOfRain = chanceOfRain;
	}
	
	public double fahrenheitToCelsius(double fahrenheit) {
		
		double celsius;
		
		celsius = (fahrenheit - 32.0) * (5.0 / 9.0);
		return celsius;
		
	}
	
	public double celsiusToFahrenheit(double celsius) {
		
		double fahrenheit;
		
		fahrenheit = (celsius * (9.0/5.0)) + 32.0;
		return fahrenheit;
	}
	
	public double fahrenheitToKelvin(double fahrenheit) {
		
		double kelvin;
		
		kelvin = (fahrenheit + 459.67) * (5.0 / 9.0);
		return kelvin;
	}
	
	public double kelvinToFahrenheit(double kelvin) {
		
		double fahrenheit;
		
		fahrenheit = (kelvin * (9.0 / 5.0)) - 459.67;
		return fahrenheit;
	}
	
	public boolean isRaining() {
		
		if(chanceOfRain > 75) {
			return true;
		}
		return false;
	}
	
	public void print() {
		
		System.out.println("Current temperature is: " + temperature);
		System.out.println("Sky condition is: " + skyCondition);
		System.out.println("Chance of rain is: " + chanceOfRain + "%");
		System.out.println();
	}
	
	public void printToFile() throws IOException {
		
		String fileContent = "temperature = " + temperature + 
							 ", skyCondition = " + skyCondition +
							 ", chanceOfRain = " + chanceOfRain + "%";
		
		
		FileWriter fileWriter = new FileWriter("C:/Users/Derek/Desktop/ForecastOutput.txt");
		fileWriter.write(fileContent);
		fileWriter.close();
							
	}
			

}

Leave a Reply