/* * TemperatureAdvisor.java * * Project 1 Part A sample solution */ /* import the Random class so we can use it in our program. */ import java.util.Random; /** * A class for converting temperature values between Fahrenheit, Celsius, * and Kelvin degree scales. Stores the last temperature value given as the * current temperature, and makes preductions about what tomorrow's temperature * will be. * * @author Orion Bawdon */ public class TemperatureAdvisor { /** The current temperature, in degrees Celsius. */ private double currentTemp; /** A random number generator used for weather forecasting. */ private Random rand; /** * Creates a new TemperatureAdvisor. The current temperature is set to a * reasonable default value and a random number generator object is created. */ public TemperatureAdvisor() { currentTemp = 15.0; rand = new Random(); } /* * Each of the convert methods does two things: * 1. Sets the current temperature to a certain value. Remember, we are * storing the temperature in Celsius, so that means that to do this we must * always convert the temperature to Celsius, no matter what scales we are * converting to and from. * 2. Returns the temperature in the desired scale. To do this we take * advantage of the fact that we are making other methods to return the * current temperature in each of the three temperature scales. Writing your * program this way reduces the amount of coding you need to do and also makes * it easier to debug if something goes wrong. * * Each method also is preceded by a Javadoc comment. The purpose of these * comments is to communicate, on a high level, what each method does. Many * of these comments are very similar, but it is still considered good * programming practice to have a comment before each method specifying * exactly what the method does. Don't worry too much about the format of * these comments for now, but you should make it a point to include them * on every method you implement, if you haven't been doing so already. */ /** * Converts a temperature from degrees Fahrenheit to degrees Celsius and * sets the current temperature to that value. * * @param degreesF The temperature to be converted, in degrees Fahrenheit. * @return the given temperature in degrees Celsius. */ public double convertFToC(double degreesF) { currentTemp = (5.0 / 9.0) * (degreesF - 32.0); return getCurrentTempC(); } /** * Converts a temperature from degrees Celsius to degrees Fahrenheit and * sets the current temperature to that value. * * @param degreesC The temperature to be converted, in degrees Celsius. * @return the given temperature in degrees Fahrenheit. */ public double convertCToF(double degreesC) { currentTemp = degreesC; return getCurrentTempF(); } /** * Converts a temperature from degrees Fahrenheit to degrees Kelvin and * sets the current temperature to that value. * * @param degreesF The temperature to be converted, in degrees Fahrenheit. * @return the given temperature in degrees Kelvin. */ public double convertFToK(double degreesF) { currentTemp = (5.0 / 9.0) * (degreesF - 32.0); return getCurrentTempK(); } /** * Converts a temperature from degrees Kelvin to degrees Fahrenheit and * sets the current temperature to that value. * * @param degreesF The temperature to be converted, in degrees Kelvin. * @return the given temperature in degrees Fahrenheit. */ public double convertKToF(double degreesK) { currentTemp = degreesK - 273.15; return getCurrentTempF(); } /** * Converts a temperature from degrees Celsius to degrees Kelvin and * sets the current temperature to that value. * * @param degreesF The temperature to be converted, in degrees Celsius. * @return the given temperature in degrees Kelvin. */ public double convertCToK(double degreesC) { currentTemp = degreesC; return getCurrentTempK(); } /** * Converts a temperature from degrees Kelvin to degrees Celsius and * sets the current temperature to that value. * * @param degreesF The temperature to be converted, in degrees Kelvin. * @return the given temperature in degrees Celsius. */ public double convertKToC(double degreesK) { currentTemp = degreesK - 273.15; return getCurrentTempC(); } /** * Gets the current temperature in degrees Fahrenheit. * * @return the current temperature in degrees Fahrenheit. */ public double getCurrentTempF() { return currentTemp * (9.0 / 5.0) + 32.0; } /** * Gets the current temperature in degrees Celsius. * * @return the current temperature in degrees Celsius. */ public double getCurrentTempC() { return currentTemp; } /** * Gets the current temperature in degrees Kelvin. * * @return the current temperature in degrees Kelvin. */ public double getCurrentTempK() { return currentTemp + 273.15; } /** * Predicts tomorrow's temperature by randomly generating a value within * 5 degrees C of the current temperature. * * @return tomorrow's temperature in degrees Fahrenheit. */ public double getTomorrowsTempF() { double tomorrowsTemp = currentTemp + (rand.nextDouble() * 10.0) - 5.0; return tomorrowsTemp * (9.0 / 5.0) + 32.0; } /** * Predicts tomorrow's temperature by randomly generating a value within * 5 degrees C of the current temperature. * * @return tomorrow's temperature in degrees Celsius. */ public double getTomorrowsTempC() { double tomorrowsTemp = currentTemp + (rand.nextDouble() * 10.0) - 5.0; return tomorrowsTemp; } /** * Predicts tomorrow's temperature by randomly generating a value within * 5 degrees C of the current temperature. * * @return tomorrow's temperature in degrees Kelvin. */ public double getTomorrowsTempK() { double tomorrowsTemp = currentTemp + (rand.nextDouble() * 10.0) - 5.0; return tomorrowsTemp + 273.15; } }