Write a class encapsulating the concept of the weather forecast, assuming that i
ID: 3810221 • 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 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
Explanation / Answer
/* OUTPUT
Sky Condition is sunny and temperature is 70.0
Sky Condition is cloudy and temperature is 35.0
is T1 equal to T2 ? Not Equal
Celsius equivalent of 70.0 is 21.11111111111111
is T1 is consistent Consistent
*/
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 temperature
{
private double Fahrenheit;
private String sky_condition;
//Include a default constructor,
public temperature()
{
//Temperature, in Fahrenheit, should be between -50 and +150; the default value is 70, if needed.
Fahrenheit = 70;
//The default sky condition is sunny.
sky_condition = "sunny";
}
//an overloaded constructor,
public temperature(double f,String s)
{
Fahrenheit = f;
sky_condition = s;
}
//the accessors and
public String getSky_condition()
{
return sky_condition;
}
public double getFahrenheit()
{
return Fahrenheit;
}
//mutators, and methods,
public void setFahrenheit(double f)
{
Fahrenheit = f;
}
public void setSky_condition(String s)
{
sky_condition = s;
}
//toString() and
public String toString()
{
return "Sky Condition is " + sky_condition + " and temperature is " + Fahrenheit;
}
//equals().
public boolean equals(temperature T)
{
return ((Fahrenheit == T.Fahrenheit) && (sky_condition.equalsIgnoreCase(T.sky_condition)));
}
//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 is_consistent()
{
if(Fahrenheit <32 && !sky_condition.equalsIgnoreCase("snowy")) return false;
if(Fahrenheit >100 && !sky_condition.equalsIgnoreCase("sunny")) return false;
return true;
}
}
//Write a client class to test all the methods in your class.
public class tester
{
public static void main(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
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.