In: Computer Science
Write a class encapsulating the concept of the weather forecast, assuming that it has the following attributes: the temperature and the sky conditions (e.g. sunny, snowy, cloudy, rainy, etc.). Include a default constructor, an overloaded constructor, accessors and mutators, and the methods, toString() and equals(). Temperature, in Fahrenheit, should be between -50 and +150; the default value is 70, if needed. The default sky condition is sunny. In Addition, include a method that converts Fahrenheit to Celsius. Celsius temperature = (Fahrenheit temperature – 32) * 5/9. Also include a boolean method that checks whether the weather attributes are consistent (there are two cases where they are not consistent: when the temperature is below 32 and it is not snowy, and when the temperature is above 100 and it is not sunny). Write a client class to test all the methods in your class.
public class WeatherForecast
{
private double temperature; // in Fahrenheit
private String sky;
/**
* initializes temperature to 70.0 and sky to "sunny"
* calls mutator methods to validate these default values
*/
public WeatherForecast( )
{
temperature = 70;
sky = "sunny";
}
/**
* Overloaded constructor:
* Allows client to set beginning values for temperature and sky
* This constructor takes two parameters
* Calls mutator methods to validate new values
* @param newTemperature the temperature for the weather forecast
* @param newSky the sky conditions for the weather forecast
*/
public WeatherForecast( double newTemperature, String newSky )
{
temperature = newTemperature;
sky = newSky;
}
/** getTemperature method
* @return the temperature
*/
public double getTemperature( )
{
return temperature; // add code here, insted of o.o we should return tempature
}
/**
* Mutator method:
* Allows client to set value of temperature
* setTemperature sets the value of temperature
* to 70.0 if newTemperature is less than -50 or greater than 150
* @param newTemperature the new number of temperature
*/
public void setTemperature( double newTemperature )
{
// add code here // make sure falls in range of -50 to 100, need if else
if(temperature >= 0)
temperature = newTemperature;
else
{
System.err.println( "Miles driven cannot be negative." );
System.err.println( "Value not changed.");
}
}
/** getSky method
* @return the sky conditions
*/
public String getSky( )
{
return sky;
// add code here, returns the string variable that represents your sky. You are going to some error checking and a syst
// system.err make sure equal to sunny snowy cloudy rainy
}
/**
* Mutator method:<BR>
* Allows client to set value of sky
* setSky sets the value of sky
* to "sunny" if newSky is neither "sunny", "snowy",
* "cloudy", nor "rainy"
* This method is not case sensitive
* @param newSky the new sky condition
*/
// @return the temperature and the sky conditions for the weather forecast
@Override
public String toString( )
{
return( "temperature: " + temperature + "; sky: " + sky );
}
/**
* Compares two WeatherForecast objects for the same field values
* @param wf another WeatherForecast object
* @return a boolean, true if this object
* has the same field values as the parameter wf
*/
public boolean equals( WeatherForecast wf )
{
return ( Math.abs( temperature - wf.temperature ) < 0.0001
&& sky.equals( wf.sky ) );
}
/**
* fahrenheitToCelsius method
* Computes the degrees Celsius temperature
* @return a double, the temperature value in degrees Celsius
*/
public double fahrenheitToCelsius( )
{
temperature = temperature - 32;
temperature = temperature * 5;
temperature = temperature / 9;
return temperature;
// add code here complete method, do the coversioan return a double data type
}
/**
* Checks if the temperature value and the sky conditions are compatible
* @return a boolean, false if the temperature is below 32 and the sky is not snowy,
* or if the temperature is greater than 100 and the sky is not sunny, true otherwise
*/
public boolean isConsistent( )
{
return !( ( temperature < 32 && !sky.equals( "snowy" ) )
|| ( temperature > 100 && !sky.equals( "sunny" ) ) );
}
}