JAVA code only Write a class encapsulating the concept of the weather forecast,
ID: 3810054 • Letter: J
Question
JAVA code only
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 default constructor, an overloaded 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.
Here is the outcome
s Javadoc Declaration G ConsoleExplanation / Answer
//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//
import java.util.*;
public class Forecast{
// Classes would be Temperature and skyconditions
class temperature
{
private double Fahrenheit;
private String skycondition;
//Include a default constructor,
public temperature()
{
//Temperature should be in between -50 and +150
Fahrenheit = 70;
//The default skycondition is sunny.
skycondition = "sunny";
}
//an overloaded constructor
public temperature(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(temperature T)
{
return ((Fahrenheit == T.Fahrenheit) && (sky_condition.equalsIgnoreCase(T.skycondition)));
}
//Method to convert farenhite to celcius c
celsius temperature = (Fahrenheit temperature – 32) * 5/9
public double toCelsius()
{
return (Fahrenheit-32)*5/9;
}
//Method wll check consistency of weather
// (There might be two cases of not consistency : when the temperature 32 and it is not snowy, and when the
// temperature>100 and it is not sunny).
public boolean is_consistent()
{
if(Fahrenheit <32 && !skycondition.equalsIgnoreCase("snowy")) return false;
if(Fahrenheit >100 && !skycondition.equalsIgnoreCase("sunny")) return false;
return true;
}
//client class to test all methods
class tester
{
public tester(String[] args)
{
//testing default constructor;
temperature T1 = new temperature();
System.out.println(T1);
//testing overloaded constructor;
temperature T2 = new temperature(35,"cloudy");
System.out.println(T2);
//testing equals method
System.out.println("is T1 equal to T2 ? " + (T1.equals(T2)?" Equal":" Not Equal"));
System.out.println("Celsius equivalent of " + T1.getFahrenheit() + " is " + T1.toCelsius());
System.out.println("is T1 is consistent " + (T1.is_consistent()?" Consistent":" Not Consistent"));
}
}}}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.