Mini Project Set 5 Answer: Weather Station Report
Complete Program
import java.util.ArrayList;
import java.util.HashMap;
class WeatherStation {
private ArrayList<String> dayOrder;
private HashMap<String, Integer> readings;
public WeatherStation() {
dayOrder = new ArrayList<>();
readings = new HashMap<>();
}
public void addReading(String day, int temperature) {
dayOrder.add(day);
readings.put(day, temperature);
}
public void printReport() {
for (String day : dayOrder) {
int temperature = readings.get(day);
System.out.println(day + ": " + temperature + "C " + bar(temperature));
}
}
public String bar(int temperature) {
String result = "";
for (int i = 0; i < temperature / 5; i++) {
result += "#";
}
return result;
}
public double averageTemperature() {
int total = 0;
for (String day : dayOrder) {
total += readings.get(day);
}
return (double) total / dayOrder.size();
}
public String hottestDay() {
String bestDay = dayOrder.get(0);
for (String day : dayOrder) {
if (readings.get(day) > readings.get(bestDay)) {
bestDay = day;
}
}
return bestDay;
}
public int sunnyDays() {
int count = 0;
for (String day : dayOrder) {
if (readings.get(day) >= 25) {
count++;
}
}
return count;
}
}
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());
}
}
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
Key Points
dayOrder (an ArrayList) keeps the reading order, while readings (a HashMap) gives fast lookup by day name.
addReading updates both collections at once, so they always stay in sync.
- The
bar method reuses integer division: temperature / 5 is the number of # characters.
hottestDay() starts with the first day and uses > so tied temperatures keep the first day.
- A
double cast is needed before calculating the average.