Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a class encapsulating the concept of the weather forecast, assuming that i

ID: 3923967 • 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

// 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);
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote