Recently several cities in north Carolina experienced the joy of a snowfall. The
ID: 3662743 • Letter: R
Question
Recently several cities in north Carolina experienced the joy of a snowfall. The following information was recorded by WERC TV
Your task is to write a java program to compute the total amount of snowfall and the average snowfall for the cities listed above. Your program should display each city and snowfall amount and the average amount of snowfall, found and display the highest as well as the lowest also.
City Snowfall Amount Boone 2 feet Ashville 6.5 feet Winston-Salem 5.5 inches Raleigh 4 inches Fayetteville .7 inchesExplanation / Answer
// Update your package name here or comment out
package org.myUniversity.exercise;
import java.util.HashMap;
import java.util.Map;
public class Chegg {
// Concept looking for would may have helped with data structure selection
public static void main(String[] args) {
// HashMap doesn't handle primitives(int), just objects(Integer - Wrapper Class)
// DECLARATIONS
// City and Snowfall
Map<String, Float> map = new HashMap<String, Float>();
float totalSnowfall = 0;
float avgSnowfall=0;
// Data Initilization : All snowfall values are converted to inches for uniformity
map.put("Boone", 24f);
map.put("Ashville", 78f);
map.put("Winston", 5.5f);
map.put("Raleigh", 4f);
map.put("Fayetteville", 0.7f);
// Initialize with first City snowfall
float minSnowfall=map.get("Boone");
float maxSnowfall=map.get("Boone");
// Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.
for (Map.Entry<String, Float> entry : map.entrySet()) {
System.out.println("City = " + entry.getKey() + ", Snowfall = " + entry.getValue()+" inches");
totalSnowfall = totalSnowfall + entry.getValue();
// If present city snow fall is less than min
if (minSnowfall > entry.getValue())
minSnowfall= entry.getValue();
// If present city snow fall is greater than max
if (maxSnowfall < entry.getValue())
maxSnowfall= entry.getValue();
}
// System.out.println("Total citis: " +map.size());
avgSnowfall = totalSnowfall/map.size();
System.out.println(" Total Snowfall = " + totalSnowfall + "inches Average Snowfall = " + avgSnowfall +" inches ");
System.out.println("Min Snowfall = " + minSnowfall + " inches Max Snowfall = " + maxSnowfall +" inches ");
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.