KaiquanMah commited on
Commit
16b1b2e
·
verified ·
1 Parent(s): 656ebf7

for (String city : cityNTemps.keySet())

Browse files
Week 3: Objects, files and exceptions/12. Coldest city ADDED
@@ -0,0 +1,115 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Write the method
2
+
3
+ String coldestCity(HashMap<String, Double> temps)
4
+
5
+ which takes as its parameter a hashmap where the keys are city names and the values are temperatures.
6
+ Your task is to find and return the name of the city with the lowest temperature.
7
+
8
+
9
+ Example method call:
10
+
11
+ public static void main(String[] args){
12
+ HashMap<String, Double> temps = new HashMap<>();
13
+ temps.put("Turku", 0.5);
14
+ temps.put("Tampere", -5.25);
15
+ temps.put("Helsinki", -2.5);
16
+
17
+ System.out.println("Coldest city: " + coldestCity(temps));
18
+ }
19
+
20
+ Program outputs:
21
+ Coldest city: Tampere
22
+
23
+
24
+
25
+ =====================================
26
+
27
+
28
+
29
+
30
+
31
+
32
+
33
+ import java.util.Random;
34
+ import java.util.HashMap;
35
+
36
+ public class Test{
37
+ public static void main(String[] args){
38
+ final Random r = new Random();
39
+
40
+ for (int testi=1; testi<=3; testi++) {
41
+ System.out.println("Test " + testi);
42
+ String[] kaup = "Turku Helsinki Tampere Rovaniemi Oulu Kajaani Jyväskylä Pori Vaasa".split(" ");
43
+
44
+ HashMap<String, Double> lt = new HashMap<>();
45
+ double[] add = {0, 0.25, 0.5, 0.75};
46
+
47
+ for (String kaupunki : kaup) {
48
+ double l = 25 - r.nextInt(50);
49
+ //'add' array of 4 floats are used here
50
+ // randomly select 0-3 idx
51
+ // then pick up the float value from the 'add' array
52
+ // and add to the 'l' temperature
53
+ l += add[r.nextInt(4)];
54
+
55
+ lt.put(kaupunki, l);
56
+ }
57
+
58
+ System.out.println("Temperatures: ");
59
+ System.out.println("" + lt);
60
+ System.out.println("Coldest city: " + coldestCity(lt));
61
+ System.out.println("");
62
+ }
63
+ }
64
+
65
+
66
+
67
+
68
+ public static String coldestCity(HashMap<String, Double> cityNTemps) {
69
+ String coldest = "";
70
+ double coldestTemp = 999;
71
+ for (String city : cityNTemps.keySet()) {
72
+ if (cityNTemps.get(city) < coldestTemp) {
73
+ coldest = city;
74
+ coldestTemp = cityNTemps.get(city);
75
+ }
76
+ }
77
+ return coldest;
78
+ }
79
+
80
+
81
+
82
+
83
+ }
84
+
85
+
86
+
87
+
88
+
89
+
90
+
91
+
92
+ Test 1
93
+ Temperatures:
94
+ {Pori=-8.0, Jyväskylä=14.0, Kajaani=-16.0, Oulu=-1.0, Helsinki=1.0, Rovaniemi=1.0, Turku=20.5, Tampere=0.0, Vaasa=-2.75}
95
+ Coldest city: Kajaani
96
+
97
+ Test 2
98
+ Temperatures:
99
+ {Pori=-8.25, Jyväskylä=20.5, Kajaani=-21.0, Oulu=8.5, Helsinki=-4.5, Rovaniemi=-1.0, Turku=14.75, Tampere=12.75, Vaasa=1.0}
100
+ Coldest city: Kajaani
101
+
102
+ Test 3
103
+ Temperatures:
104
+ {Pori=9.75, Jyväskylä=4.5, Kajaani=-11.5, Oulu=-10.25, Helsinki=-12.5, Rovaniemi=20.0, Turku=9.5, Tampere=-8.0, Vaasa=7.75}
105
+ Coldest city: Helsinki
106
+
107
+
108
+
109
+
110
+
111
+
112
+
113
+
114
+
115
+