Menu

(Solved) : Part 1 Kotlin Assignment Part Assignment Practicing Kotlin Variables Functions Control Str Q36331239 . . .

Part 1: Kotlin Assignment.

In this part of the assignment you will be practicing withKotlin variables, functions, and control structures. Complete thispart by implementing the described functions. You can and shouldcall these functions from inside the main() method in order to testyour work. Your program should do all the following, pay attentionwhen it says you must develop a function to do something:

1. Step 1: Define a _data class_ called `City` that willrepresent the location of a city. A City should have threeproperties: a `name` (String), `latitude` (Double), and `longitude`(Double).

2. Step 2: Define a function called `listCities()` that returnsa `list` of City objects representing the following U.S. cities:{name: “Atlanta”, latitude: 33.7490, longitude: -84.3880} {name:”Bozeman”, latitude: 45.6770, longitude: -111.0429}, {name: “NewYork”, latitude: 40.7128, longitude: -74.0060} {name: “SanFrancisco”, latitude: 37.7749, longitude: -122.4194}, {name:”Tacoma”, latitude: 47.2529, longitude: -122.4443}, Call yourfunction from inside the `main()` method, saving the result to alocal variable called `cityLocations`. Print this out to check yourwork.

3. Step 3: Define a function `distanceFromSeattle()` that takesas a param a City object and return the distance in kilometers(Double) between that city and Seattle, WA (latitude: 47.6062,longitude: -122.3321). You can use the `haversine()` functionprovided below to calculate the distance in km between two pointson the globe. Call your function from inside the `main()` methodand print out the distance between Seattle and Tacoma (should beabout 40km) //The following is the haversine function you shouldincorporate to find the distances. //Calculates the distance in kmbetween two points on a globe //Implementation fromhttp://rosettacode.org/wiki/Haversine_formula fun haversine(lat1:Double, lon1: Double, lat2: Double, lon2: Double): Double { val R =6372.8 // in kilometers val l1 = Math.toRadians(lat1) val l2 =Math.toRadians(lat2) val dl = Math.toRadians(lat2 – lat1) val dr =Math.toRadians(lon2 – lon1) return 2 * R *Math.asin(Math.sqrt(Math.pow(Math.sin(dl / 2), 2.0) +Math.pow(Math.sin(dr / 2), 2.0) * Math.cos(l1) * Math.cos(l2)))}

4. Step 4: From your `main()` method, call the `map()` method ofthe `cityLocations` list, passing it your `distanceFromSeattle()`function in order to get a list of distance between the cities andSeattle. Print the resulting list.

5. Step 5: From the `main()` method, call the `filter()` methodof the `cityLocations` list, passing it an _anonymous function_used to filter the list. This function should return `true` if thegiven City is “far” from Seattle (further away than 50km). Printout the resulting list.

6. Step 6: From the `main()` method, use the `map()` and`filter() methods to get a list of the `names` of cities that arein the “West”, meaning west of the Mississippi river. For ourpurposes, the Mississippi is at longitude -89.97803 E (so anything”less than” that is to its west). Use two _lambda expressions_ todo this; utilize the implicit `it` parameter. Print out theresulting list. Hint: filter for western cities, and then map theresults to a list of names (Strings).

7. Step 7: From the `main()` method, use the `maxBy()` method ofthe `cityLocations` list to find which city is the furthest fromSeattle. Since the last (and only) parameter to the `maxBy()`method is a function, you should omit the parentheses from themethod call. Print the resulting city. Hint:https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/max-by.html

8. Step 8: From the `main()` method, use the map method toproduce a variable called `mappedCities` that is a `Map` object,where each key is the name of a city (String) and the value is theCity object. Print the resulting map, one line per key, valueobject. Then use mappedCities to print the latitude of Bozeman byusing the key value to identify the proper value. Make sure you putyour print statement in an if statement that checks to see if“Bozeman” is present in mappedCities. Here is the output of Stepstwo through 8 (I misspelled San Francisco…..on purpose): You shouldmimic your output to look as similar to this as possible……but spellSan Fran correctly **********Step Two********** City(name=Atlanta,latitude=33.749, longitude=-84.388) City(name=Bozeman,latitude=45.677, longitude=-111.0429) City(name=New York,latitude=40.7128, longitude=-74.006) City(name=San Fransico,latitude=37.7749, longitude=- 122.4194) City(name=Tacoma,latitude=47.2529, longitude=-122.4443) **********StepThree********** The distance between Seattle and Tacoma is40.19290553996769 km **********Step Four********** A new List withthe distances of all cities in list to Seattle: [3506.672723087685,887.4977864782966, 3866.6204789528556, 1093.5226297469185,40.19290553996769] **********Step Five********** List of citiesover 1000KM from Seattle (used the return list from the Filterfunction and just printed the city name) Atlanta New York SanFransico **********Step Six********** [Bozeman, San Fransico,Tacoma] **********Step Seven********** Max Distance: City(name=NewYork, latitude=40.7128, longitude=-74.006) **********StepEight********** Name Atlanta: City City(name=Atlanta,latitude=33.749, longitude=-84.388) Name Bozeman: CityCity(name=Bozeman, latitude=45.677, longitude=-111.0429) Name NewYork: City City(name=New York, latitude=40.7128, longitude=-74.006)Name San Fransico: City City(name=San Fransico, latitude=37.7749,longitude=-122.4194) Name Tacoma: City City(name=Tacoma,latitude=47.2529, longitude=-122.4443) The latitude for Bozeman is45.677

Expert Answer


Answer to Part 1 Kotlin Assignment Part Assignment Practicing Kotlin Variables Functions Control Str Q36331239 . . .

OR