Objectives
- Accessing and manipulating data stored in an
ArrayList
ArrayList
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(){
}
}
DayTemperature
class represents a single day’s temperature data. This includes:day (String)
to store the day of the temperature recordminTemp (double)
to store the minimum temperature recorded for the daymaxTemp (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(){
}
TemperatureAnalysis
class manages the recorded weather data by performing calculations such as calculating averages and extreme temperatures.toString()
method in DayTemperature
getAverageTemp()
method in DayTemperature
which calculates and returns the average temperature for the day (average of max and min temperatures)static
ArrayList
named weekTemp
inside the TemperatureAnalysis
class to store instances of DayTemperature
TemperatureAnalysis
:printWeekTemp()
method prints the temperatures recorded for each day of the weekprintAverageWeeklyTemp()
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 weekprintLowestTemp()
locates and prints the day with the lowest temperature recorded for the weekprintMedianTemp()
method that calculates and prints the median temperature of the weekArrayList
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: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