AP CSA Practice Free Response Question 1
A Weather
class is used to represent the weather for a particular day. The Weather
class has the day's temperature (int temperature
) and whether it is sunny (boolean sunny
). The class has a getDescription()
method that returns a description of the weather as a String
. You will write the getDescription()
method.
public class Weather {
private int temperature;
private boolean sunny;
/** Constructs a Weather object with the given temperature and sunny status */
public Weather(int temp, boolean isSunny) {
temperature = temp;
sunny = isSunny;
}
/** Returns a weather description based on the temperature and sunny status as described in part a. */
public String getWeatherDescription() {
String description = " ";
//add your if statement and assign description to the appropriate return value.
return description;
}
}
Part a.
Write the method getWeatherDescription
that returns a String
describing the weather based on the temperature and sunny status, as follows:
- if it is sunny and the temperature is greater than 80 degrees, return
Hot and sunny
.
- if it is sunny and the temperature is between 60 and 80 degrees inclusively, return
Pleasant and sunny
.
- if it is sunny and the temperature is less than 60 degrees, return
Cool and sunny
.
- if it is not sunny and the temperature is greater than 80 degrees, return
Hot but cloudy
.
- if it is sunny and the temperature is between 60 and 80 degrees inclusively, return
Pleasant but cloudy
.
- if it is sunny and the temperature is less than 60 degrees, return
Cool and cloudy
.
Write Your Response
- Write code to complete the
getWeatherDescription()
method in the Java Playground below.
- Run the code and verify the statements print
true
for all cases.
- If any output is
false
, adjust your code and try again.