Write a java program Weatherjava with 3 private instance var
Write a java program Weather.java with 3 private instance varaibles
1 city
2 temperature
3 sky conditions
The program must have two constructors
no arg constructor(city= \"none\", temp=70, skyconditions = \"sunny\')
an overloaed constructor Assign the city instance variable to the corresponding city parameter but, sky and temp should use setSky and setTemp setter methods.
default is sunny
Methods
Setter and Getter methods for each instance variable
toString method
convert method Fahrenheit to Celsius (C=(F-32)+5.0/9) should be double
method checkConditons temp <32 F should be sunny, cloudy or, snowy and temp>100F should be sunny
Method should have void return and no parameters
Use Driver WeatherDriver.java
import java.util.*;
public class Weather Driver{
public staic void main (String[]args){
Scanner scan = new Scanner (System.in);
//Instance weather objects
Weather w1= new Weather(\"Denver\", 30, \"snowy\");
Weather w2= new Weather (\"Tucson\",70,\"sunny\");
//Prompt for w3\'s = city name, temperature and sky conditions
//Output all 3 weather objects using toString() method in Weather
System.out.println(w1);
System.out.println(w2);
}
}
Sample output
What is the city?
London
What is the sky condition (sunny,cloudy,rainy or, snowy)?
cloudy
What is the temperature?
55
Denver
Sky condition: snowy
Temperature: 30.0 degrees F (-1.1 degrees C)
Tucson
Sky Condition: sunny
Temperature: 70.0 degrees F (21.1 degrees C)
Londdon
Sky Condition cloudy
Temperature 55.0 degrees F (12.8 degrees C)
Solution
/*
* The class Weather that represents the object
* of the weather class that contains city, temperature
* and sky conditions.
* */
//Weather.java
public class Weather {
//instance variables of class
private String city;
private double temp;
private String skyconditions;
//No-argument constructor
public Weather()
{
city=\"none\";
temp=70;
skyconditions=\"sunny\";
}
//parameterized construcotr
public Weather(String city,
double temp,
String skyconditions) {
setCity(city);
setTemp(temp);
setSky(skyconditions);
}
//Setter methods
public void setCity(String city){
this.city=city;
}
public void setTemp(double temp){
this.temp=temp;
}
public void setSky
(String skyconditions){
this.skyconditions=skyconditions;
}
//Getter methods
public String getCity(){
return city;
}
public double getTemp(){
return temp;
}
public String getSky(){
return skyconditions;
}
public double Fahrenheit2Celsius()
{
return ((temp-32.0)*5.0/9.0);
}
//Returns the string representation of the weather object
@Override
public String toString() {
return String.format(\"%s\ Sky condition:%s\" +
\"\ Temperature:%5.1f degrees F (%5.1f degrees C)\",
city,skyconditions,temp,Fahrenheit2Celsius());
}
}
------------------------------------------------------------------------------------------------------
//Test java program
//WeatherDriver.java
import java.util.*;
public class WeatherDriver{
public static void main (String[]args){
Scanner scan = new Scanner (System.in);
//variable declaration
String city;
double temp;
String skyconditions;
//Instance weather objects
Weather w1= new Weather(\"Denver\", 30, \"snowy\");
Weather w2= new Weather (\"Tucson\",70,\"sunny\");
//Create an instance of the Weather
Weather w3=new Weather();
//Prompt city name, temperature and sky conditions
System.out.println(\"What is the city?\");
city=scan.nextLine();
System.out.println
(\"What is the sky condition (sunny,cloudy,rainy or, snowy)?\");
skyconditions=scan.nextLine();
System.out.println(\"What is the temperature?\");
temp=Double.parseDouble(scan.nextLine());
//call setter methods
w3.setCity(city);
w3.setSky(skyconditions);
w3.setTemp(temp);
//Output all 3 weather objects using toString() method in Weather
System.out.println(w1);
System.out.println(w2);
System.out.println(w3);
}
}
------------------------------------------------------------------------------------------------------
Sample Output:
What is the city?
London
What is the sky condition (sunny,cloudy,rainy or, snowy)?
cloudy
What is the temperature?
55
Denver
Sky condition:snowy
Temperature: 30.0 degrees F ( -1.1 degrees C)
Tucson
Sky condition:sunny
Temperature: 70.0 degrees F ( 21.1 degrees C)
London
Sky condition:cloudy
Temperature: 55.0 degrees F ( 12.8 degrees C)



