Skip to main content

Section 5.4 Stretch Level

Subsection 5.4.1 Overview

This Java program analyzes the temperatures recorded in Winnipeg, Manitoba from April 1, 2024 to April 10, 2024 and it contains methods that perform various temperature-related calculations.
  class DayTemperature{

    String day;
    double minTemp;
    double maxTemp;

    public DayTemperature(String day, double maxTemp, double minTemp){
        this.day = day;
        this.minTemp = minTemp;
        this.maxTemp = maxTemp;
    }

    public String toString(){

    }

    public double getAverageTemp(){

    }
}
The DayTemperature class represents a single day’s temperature data. This includes:
  • day (String) to store the day of the temperature record
    • e.g., "April 1, 2024"
  • minTemp (double) to store the minimum temperature recorded for the day
  • maxTemp (double)to store the maximum temperature recorded for the day
  public class TemperatureAnalysis{
    //TO-DO: Initialize static ArrayList titled weekTemp that holds instances of DayTemperature

    public static void main(String[] args){
        printWeekTemp();
        printAverageWeeklyTemp();
        printHighestTemp();
        printLowestTemp();
    }

    public static void printWeekTemp(){
      
    }

    public static void printMedianTemp(){

    }

    public static void printAverageWeeklyTemp(){
    
    }

    public static void printHighestTemp(){
  
    }

    public static void printLowestTemp(){

    }
The TemperatureAnalysis class manages the recorded weather data by performing calculations such as calculating averages and extreme temperatures.

Subsection 5.4.2 Instructions

  1. Implement the toString() method in DayTemperature
    • Expected Output: April 1, 2003 -- Min: -7.8°C, Max: 6.2°C
  2. Implement the getAverageTemp() method in DayTemperature which calculates and returns the average temperature for the day (average of max and min temperatures)
  3. Define a static ArrayList named weekTemp inside the TemperatureAnalysis class to store instances of DayTemperature
  4. Implement the following methods inside TemperatureAnalysis:
    • printWeekTemp()method prints the temperatures recorded for each day of the week
    • printAverageWeeklyTemp() calculates and prints the average temperature for the entire week by summing up the daily averages (Hint: use getAverageTemp()) and dividing by the total number of days.
    • printHighestTemp() locates and prints the day with the highest temperature recorded for the week
    • printLowestTemp() locates and prints the day with the lowest temperature recorded for the week
  5. Bonus: Implement a printMedianTemp() method that calculates and prints the median temperature of the week
    • Hint: Create a new ArrayList that stores Double in the method to store all individual temperatures (both min and max from each day). This approach simplifies finding the median as you can just sort this list and then pick the middle value. Remember to handle both even and odd numbers of elements differently:
      • For even numbers, average the two middle values
      • For odd numbers, take the middle value directly
The expected output is:
Daily Temperatures for the Week:
April 1, 2024 -- Min: -7.8°C, Max: 6.2°C
April 2, 2024 -- Min: -2.0°C, Max: 2.0°C
April 3, 2024 -- Min: -4.3°C, Max: 7.9°C
April 4, 2024 -- Min: -3.3°C, Max: 10.1°C
April 5, 2024 -- Min: -2.1°C, Max: 12.5°C
April 6, 2024 -- Min: 3.0°C, Max: 14.1°C
April 7, 2024 -- Min: 1.6°C, Max: 15.6°C
April 8, 2024 -- Min: 0.4°C, Max: 11.0°C
April 9, 2024 -- Min: -2.1°C, Max: 13.8°C
April 10, 2024 -- Min: -0.2°C, Max: 16.2°C
Average Temperature: 4.630000000000001°C
The median temperature is: 2.5°C
The day with the highest temperature is: April 10, 2024 -- Min: -0.2°C, Max: 16.2°C
The day with the lowest temperature is: April 1, 2024 -- Min: -7.8°C, Max: 6.2°C
(Source for weather data: https://climate.weather.gc.ca/climate_data/daily_data_e.html?StationID=27174)
You have attempted of activities on this page.