Write a class encapsulating the concept of the weather forecast, assuming that i
ID: 3806389 • Letter: W
Question
Write a class encapsulating the concept of the weather forecast, assuming that it has the following attributes: the temperature and the sky conditions, which could be sunny, snowy, cloudy, or rainy. Include a constructor, the accessors and mutators, and 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. Include a method that converts Fahrenheit to Celsius. Celsius temperature = (Fahrenheit temperature – 32) * 5 / 9. Also include a 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.
Sample output is this:
You must create a Java project and TWO separate classes!
problems G Javadoc Declaration Console 23Explanation / Answer
// WeatherForecast.java
import java.util.*;
//Write a class encapsulating the concept of the weather forecast, assuming that it has the following attributes:
//the temperature and the sky conditions, which could be sunny, snowy, cloudy, or rainy.
class WeatherForecast
{
private double fahrenheit;
private String skyCondition;
//Include a default constructor,
public WeatherForecast()
{
//Temperature, in fahrenheit, should be between -50 and +150; the default value is 70, if needed.
fahrenheit = 70;
//The default sky condition is sunny.
skyCondition = "sunny";
}
//an overloaded constructor,
public WeatherForecast(double f,String s)
{
fahrenheit = f;
skyCondition = s;
}
//the accessors and
public String getSkyCondition()
{
return skyCondition;
}
public double getfahrenheit()
{
return fahrenheit;
}
//mutators, and methods,
public void setfahrenheit(double f)
{
fahrenheit = f;
}
public void setSkyCondition(String s)
{
skyCondition = s;
}
//toString() and
public String toString()
{
return "Sky Condition is " + skyCondition + " and temperature is " + fahrenheit;
}
//equals().
public boolean equals(WeatherForecast T)
{
return ((fahrenheit == T.fahrenheit) && (skyCondition.equalsIgnoreCase(T.skyCondition)));
}
//Include a method that converts fahrenheit to Celsius. Celsius temperature = (fahrenheit temperature 32) * 5/9
public double toCelsius()
{
return (fahrenheit-32)*5/9;
}
//. Also include a 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).
public boolean isConsistent()
{
if(fahrenheit <32 && !skyCondition.equalsIgnoreCase("snowy")) return false;
if(fahrenheit >100 && !skyCondition.equalsIgnoreCase("sunny")) return false;
return true;
}
}
// ForecastTester.java
public class ForecastTester
{
public static void main(String[] args)
{
System.out.println("Temperature cannot be less than -50 or greater than 150");
WeatherForecast t1 = new WeatherForecast();
t1.setSkyCondition("cloudy");
System.out.println("Temperature set to " + t1.getfahrenheit());
WeatherForecast t2 = new WeatherForecast(23.0,"sunny");
System.out.println("The Temperature of Weather Forecast #1 is " + t2.getfahrenheit() + "F or " + t2.toCelsius() );
System.out.println("The Sky condition of Weather Forecast #1 is " + t2.getSkyCondition() );
System.out.println("Weather Forecast #2 is Temperature " + t1.getfahrenheit() + "; sky: " + t1.getSkyCondition() );
System.out.println("Weather Forecast Temperature: " + t2.getfahrenheit());
if(t2.isConsistent()== true)
System.out.println(t2.getSkyCondition() + " is Consistent");
else
System.out.println(t2.getSkyCondition() + " is not Consistent");
System.out.println("Weather Forecast Temperature: " + t1.getfahrenheit());
if(t1.isConsistent()== true)
System.out.println(t1.getSkyCondition() + " is Consistent");
else
System.out.println(t1.getSkyCondition() + " is not Consistent");
if(t1.equals(t2) == true)
System.out.println("Original Weather Forecast #1 and #2 are same");
else
System.out.println("Original Weather Forecast #1 and #2 are different");
}
}
/*
output:
Temperature cannot be less than -50 or greater than 150
Temperature set to 70.0
The Temperature of Weather Forecast #1 is 23.0F or -5.0
The Sky condition of Weather Forecast #1 is sunny
Weather Forecast #2 is Temperature 70.0; sky: cloudy
Weather Forecast Temperature: 23.0
sunny is not Consistent
Weather Forecast Temperature: 70.0
cloudy is Consistent
Original Weather Forecast #1 and #2 are different
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.