Mini Project Set 5: Weather Station Report¶
Name: ________
Time: 40 minutes
Write a Java program that stores daily high temperatures in a HashMap and prints a weather report. The WeatherStation class keeps the days in order with an ArrayList.
Learning Goals¶
- Create a class that owns two collections: an
ArrayListand aHashMap. - Add values to both collections with one method.
- Retrieve values from a
HashMapby key. - Search values and remember a best result.
- Count values that satisfy a condition.
Requirements¶
- Create a
WeatherStationclass with these parts: - private
ArrayList<String> dayOrderand privateHashMap<String, Integer> readings - a constructor that creates both collections
addReading(String day, int temperature)adds the day todayOrderand stores the temperature inreadingsprintReport()prints one line per day in the order the readings were addedbar(int temperature)returns a bar of#characters where every#represents 5 degrees (for example,bar(22)returns"####")averageTemperature()returns the average with its decimal parthottestDay()returns the name of the hottest day; keep the first day when temperatures are tiedsunnyDays()returns the count of days with temperature 25 or higher- In
main, create oneWeatherStationobject and add exactly these readings:
station.addReading("Mon", 22);
station.addReading("Tue", 26);
station.addReading("Wed", 19);
station.addReading("Thu", 26);
station.addReading("Fri", 24);
station.addReading("Sat", 31);
station.addReading("Sun", 28);
- Print each day in this format:
Mon: 22C ####
- Then print the summary. The average must have two decimal places.
- Use
readings.get(day)whenever you need a temperature from the map. Do not create an extra array or another map.
Expected Output¶
Mon: 22C ####
Tue: 26C #####
Wed: 19C ###
Thu: 26C #####
Fri: 24C ####
Sat: 31C ######
Sun: 28C #####
Average temperature: 25.14C
Hottest day: Sat
Sunny days: 4
Starter Code¶
import java.util.ArrayList;
import java.util.HashMap;
class WeatherStation {
// TODO: Add the two private collections and the constructor.
public void addReading(String day, int temperature) {
// TODO
}
public void printReport() {
// TODO
}
public String bar(int temperature) {
// TODO
return "";
}
public double averageTemperature() {
// TODO
return 0.0;
}
public String hottestDay() {
// TODO
return "";
}
public int sunnyDays() {
// TODO
return 0;
}
}
public class Main {
public static void main(String[] args) {
WeatherStation station = new WeatherStation();
station.addReading("Mon", 22);
station.addReading("Tue", 26);
station.addReading("Wed", 19);
station.addReading("Thu", 26);
station.addReading("Fri", 24);
station.addReading("Sat", 31);
station.addReading("Sun", 28);
station.printReport();
System.out.printf("Average temperature: %.2fC%n", station.averageTemperature());
System.out.println("Hottest day: " + station.hottestDay());
System.out.println("Sunny days: " + station.sunnyDays());
}
}
Before You Submit¶
addReadingmust update both collections so they stay in sync.- Iterate
dayOrderinprintReport()and look up each temperature withreadings.get(day). - Build the bar with a loop that runs
temperature / 5times. - For
hottestDay(), start with the first day indayOrderas the best candidate. - Use
>instead of>=when comparing temperatures. - Cast one value to
doublebefore division so the average keeps its decimal part.