Write a class encapsulating the concept of the weather forec

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.

Solution

// Weather.java

public class Weather{
   double temperature;
   String sky;
   public Weather(double temperature, String sky) {
       if(temperature < -50 && temperature > 150) temperature = 70;
       this.temperature = temperature;
       this.sky = sky;
   }
   public Weather() {
       this.temperature = 70;
       this.sky = \"sunny\";
   }
   double getTemperature() {
       return temperature;
   }
   void setTemperature(double temperature) {
       if(temperature < -50 && temperature > 150) temperature = 70;
       this.temperature = temperature;
   }
   String getSky() {
       return sky;
   }
   void setSky(String sky) {
       this.sky = sky;
   }
  
   public String toString(){
       return \"Temperature: \" + temperature + \", Sky Condition: \" + sky;
   }
   public boolean equals(Weather w){
       return this.sky.equals(w.sky) && this.temperature == w.temperature;
   }
   public double f2c() {
       return (temperature - 32) * 5.0 / 9.0;
   }
   boolean consistent(){
       if(temperature < 32 && !sky.equals(\"snowy\")) return false;
       if(temperature > 100 && !sky.equals(\"sunny\")) return false;
       return true;
   }
}

// Test.java

public class Test{
   public static void main(String[] args){
       Weather object1 = new Weather();
       object1.setTemperature(44);
       object1.setSky(\"sunny\");
       System.out.println(object1);
   }
}

Write a class encapsulating the concept of the weather forecast, assuming that it has the following attributes: the temperature and the sky conditions, which co
Write a class encapsulating the concept of the weather forecast, assuming that it has the following attributes: the temperature and the sky conditions, which co

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site