Skip Top Navigation Bar

AP Computer Science A Free Response Practice Using the if Statements

Weather

This single part question is worth 3 points. This is a formative free response questions that is great early practice.

AP CSA Alignment

Unit Alignment Learning Objectives Skills
Unit 2: Selection and Iteration 2.3.A Develop code to respresent branching logical processes by using selection statements and determine the result of these processes. 2.A Write program code to implement an algorithm.
Unit 2: Selection and Iteration 2.9.A Develop code for standard and original algorithms (without data structures) and determine the result of these algorithms. 2.A Write program code to implement an algorithm.

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.