콘텐츠로 이동

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 ArrayList and a HashMap.
  • Add values to both collections with one method.
  • Retrieve values from a HashMap by key.
  • Search values and remember a best result.
  • Count values that satisfy a condition.

Requirements

  • Create a WeatherStation class with these parts:
  • private ArrayList<String> dayOrder and private HashMap<String, Integer> readings
  • a constructor that creates both collections
  • addReading(String day, int temperature) adds the day to dayOrder and stores the temperature in readings
  • printReport() prints one line per day in the order the readings were added
  • bar(int temperature) returns a bar of # characters where every # represents 5 degrees (for example, bar(22) returns "####")
  • averageTemperature() returns the average with its decimal part
  • hottestDay() returns the name of the hottest day; keep the first day when temperatures are tied
  • sunnyDays() returns the count of days with temperature 25 or higher
  • In main, create one WeatherStation object 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

  • addReading must update both collections so they stay in sync.
  • Iterate dayOrder in printReport() and look up each temperature with readings.get(day).
  • Build the bar with a loop that runs temperature / 5 times.
  • For hottestDay(), start with the first day in dayOrder as the best candidate.
  • Use > instead of >= when comparing temperatures.
  • Cast one value to double before division so the average keeps its decimal part.