Skip to content

Activity 3: Testing Methods

Instructions

Example Code

Temperature class (don't look until after the activity)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/**
 * Two utility methods related to temperature.
 *
 * @author CS159 Faculty
 * @version 01/24/2024
 */
public class Temperature {

    /**
     * Reports whether temperature is Low, Medium, or High.
     *
     * @param temp the temperature to categorize
     * @return "Low", "Medium", or "High"
     */
    public static String report(int temp) {
        if (temp >= 80) {
            return "High";
        }
        if (temp >= 65) {
            return "Medium";
        }
        return "Low";
    }

    /**
     * Computes the average of three temperatures.
     *
     * @param val1 first temperature value
     * @param val2 second temperature value
     * @param val3 third temperature value
     * @return the average temperature
     */
    public static double average(double val1, double val2, double val3) {
        return (val1 + val2 + val3) / 3.0;
    }

}