|
| 1 | +// Copyright (c) 2020 Razeware LLC |
| 2 | +// For full license & permission details, see LICENSE. |
| 3 | + |
| 4 | +void main() { |
| 5 | + listsMiniExercise1(); |
| 6 | + listsMiniExercise2(); |
| 7 | + listsMiniExercise3(); |
| 8 | + |
| 9 | + mapsMiniExercise1(); |
| 10 | + mapsMiniExercise2(); |
| 11 | + mapsMiniExercise3(); |
| 12 | + |
| 13 | + higherOrderMethodsMiniExercise1(); |
| 14 | + higherOrderMethodsMiniExercise2(); |
| 15 | +} |
| 16 | + |
| 17 | +void listsMiniExercise1() { |
| 18 | + /// Lists: Mini-exercise 1 |
| 19 | + /// |
| 20 | + /// Create an empty list of type `String`. Call it `months`. Use |
| 21 | + /// the `add` method to add the names of the twelve months. |
| 22 | +
|
| 23 | + final months = <String>[]; |
| 24 | + months.add('January'); |
| 25 | + months.add('February'); |
| 26 | + months.add('March'); |
| 27 | + months.add('April'); |
| 28 | + months.add('May'); |
| 29 | + months.add('June'); |
| 30 | + months.add('July'); |
| 31 | + months.add('August'); |
| 32 | + months.add('September'); |
| 33 | + months.add('October'); |
| 34 | + months.add('November'); |
| 35 | + months.add('December'); |
| 36 | + print(months); |
| 37 | +} |
| 38 | + |
| 39 | +void listsMiniExercise2() { |
| 40 | + /// Lists: Mini-exercise 2 |
| 41 | + /// |
| 42 | + /// Make an immutable list with the same elements as in Mini-exercise 1. |
| 43 | +
|
| 44 | + const months = [ |
| 45 | + 'January', |
| 46 | + 'February', |
| 47 | + 'March', |
| 48 | + 'April', |
| 49 | + 'May', |
| 50 | + 'June', |
| 51 | + 'July', |
| 52 | + 'August', |
| 53 | + 'September', |
| 54 | + 'October', |
| 55 | + 'November', |
| 56 | + 'December', |
| 57 | + ]; |
| 58 | + print(months); |
| 59 | +} |
| 60 | + |
| 61 | +void listsMiniExercise3() { |
| 62 | + /// Lists: Mini-exercise 3 |
| 63 | + /// |
| 64 | + /// Use collection `for` to create a new list with the month names in |
| 65 | + /// all uppercase. |
| 66 | + |
| 67 | + const months = [ |
| 68 | + 'January', |
| 69 | + 'February', |
| 70 | + 'March', |
| 71 | + 'April', |
| 72 | + 'May', |
| 73 | + 'June', |
| 74 | + 'July', |
| 75 | + 'August', |
| 76 | + 'September', |
| 77 | + 'October', |
| 78 | + 'November', |
| 79 | + 'December', |
| 80 | + ]; |
| 81 | + final bigMonths = [for (var month in months) month.toUpperCase()]; |
| 82 | + print(bigMonths); |
| 83 | +} |
| 84 | + |
| 85 | +void mapsMiniExercise1() { |
| 86 | + /// Maps: Mini-exercise 1 |
| 87 | + /// |
| 88 | + /// Create a map with the following keys: `name`, `profession`, `country` |
| 89 | + /// and `city`. For the values, add your own information. |
| 90 | + |
| 91 | + final myMap = { |
| 92 | + 'name': 'Li Ming', |
| 93 | + 'profession': 'software engineer', |
| 94 | + 'country': 'China', |
| 95 | + 'city': 'Beijing', |
| 96 | + }; |
| 97 | + print(myMap); |
| 98 | +} |
| 99 | + |
| 100 | +void mapsMiniExercise2() { |
| 101 | + /// Maps: Mini-exercise 2 |
| 102 | + /// |
| 103 | + /// You suddenly decide to move to Toronto, Canada. Programmatically update |
| 104 | + /// the values for `country` and `city`. |
| 105 | + |
| 106 | + final myMap = { |
| 107 | + 'name': 'Li Ming', |
| 108 | + 'profession': 'software engineer', |
| 109 | + 'country': 'China', |
| 110 | + 'city': 'Beijing', |
| 111 | + }; |
| 112 | + myMap['country'] = 'Canada'; |
| 113 | + myMap['city'] = 'Toronto'; |
| 114 | + print(myMap); |
| 115 | +} |
| 116 | + |
| 117 | +void mapsMiniExercise3() { |
| 118 | + /// Maps: Mini-exercise 3 |
| 119 | + /// |
| 120 | + /// Iterate over the map and print all the values. |
| 121 | + |
| 122 | + final myMap = { |
| 123 | + 'name': 'Li Ming', |
| 124 | + 'profession': 'software engineer', |
| 125 | + 'country': 'Canada', |
| 126 | + 'city': 'Toronto', |
| 127 | + }; |
| 128 | + |
| 129 | + for (var value in myMap.values) { |
| 130 | + print(value); |
| 131 | + } |
| 132 | + |
| 133 | + // Or: |
| 134 | + myMap.forEach((key, value) => print(value)); |
| 135 | +} |
| 136 | + |
| 137 | +void higherOrderMethodsMiniExercise1() { |
| 138 | + /// Higher order methods: Mini-exercise 1 |
| 139 | + /// |
| 140 | + /// Given the following exam scores: |
| 141 | + /// |
| 142 | + /// ``` |
| 143 | + /// final scores = [89, 77, 46, 93, 82, 67, 32, 88]; |
| 144 | + /// ``` |
| 145 | + /// |
| 146 | + /// Use `sort` to find the highest and lowest grades. |
| 147 | +
|
| 148 | + final scores = [89, 77, 46, 93, 82, 67, 32, 88]; |
| 149 | + scores.sort(); |
| 150 | + final lowest = scores.first; |
| 151 | + final highest = scores.last; |
| 152 | + print(lowest); |
| 153 | + print(highest); |
| 154 | +} |
| 155 | + |
| 156 | +void higherOrderMethodsMiniExercise2() { |
| 157 | + /// Higher order methods: Mini-exercise 2 |
| 158 | + /// |
| 159 | + /// Given the following exam scores: |
| 160 | + /// |
| 161 | + /// ``` |
| 162 | + /// final scores = [89, 77, 46, 93, 82, 67, 32, 88]; |
| 163 | + /// ``` |
| 164 | + /// |
| 165 | + /// Use `where` to find all the B grades, that is, all the scores |
| 166 | + /// between 80 and 90. |
| 167 | + |
| 168 | + final scores = [89, 77, 46, 93, 82, 67, 32, 88]; |
| 169 | + final bGrades = scores.where((score) => score >= 80 && score < 90); |
| 170 | + print(bGrades); |
| 171 | +} |
0 commit comments