// Olympics.kt Copyright (c) Kari Laitinen // http://www.naturalprogramming.com // 2020-10-27 File created. /* This program demonstrates the use of an array of objects, or, more precisely, an array that contains references to objects. The program first introduces a class named Olympics. An Olympics object can contain the data of olympic games. By using the Olympics class, an array (Olympics table) is defined in the 'main' program. The array is used to search for data of olympic games. The end of data in the Olympics table is detected in a somewhat rude way. It will be your duty to improve this program so that the end of data will be detected in a more 'official' manner, i.e., there is a standard mechanism to detect how many objects are contained in an array. */ class Olympics( val olympic_year : Int, val olympic_city : String, val olympic_country : String ) { fun print_olympics_data() { print( String.format( "\n In %d, Olympic Games were held in %s, %s.\n\n", olympic_year, olympic_city, olympic_country ) ) } } fun main() { // First we create an array that contains Olympics objects. // Later on the program will go through this array searching // for given year. val olympics_table = arrayOf( Olympics( 1896, "Athens", "Greece" ), Olympics( 1900, "Paris", "France" ), Olympics( 1904, "St. Louis", "U.S.A." ), Olympics( 1906, "Athens", "Greece" ), Olympics( 1908, "London", "Great Britain"), Olympics( 1912, "Stockholm","Sweden" ), Olympics( 1920, "Antwerp", "Belgium" ), Olympics( 1924, "Paris", "France" ), Olympics( 1928, "Amsterdam","Netherlands"), Olympics( 1932, "Los Angeles", "U.S.A."), Olympics( 1936, "Berlin", "Germany" ), Olympics( 1948, "London", "Great Britain"), Olympics( 1952, "Helsinki","Finland" ), Olympics( 1956, "Melbourne","Australia" ), Olympics( 1960, "Rome", "Italy" ), Olympics( 1964, "Tokyo", "Japan" ), Olympics( 1968, "Mexico City","Mexico" ), Olympics( 1972, "Munich", "West Germany"), Olympics( 1976, "Montreal", "Canada" ), Olympics( 1980, "Moscow", "Soviet Union"), Olympics( 1984, "Los Angeles","U.S.A."), Olympics( 1988, "Seoul", "South Korea"), Olympics( 1992, "Barcelona","Spain" ), Olympics( 1996, "Atlanta", "U.S.A." ), Olympics( 2000, "Sydney", "Australia" ), Olympics( 2004, "Athens", "Greece" ), Olympics( 2008, "Beijing", "China" ), Olympics( 2012, "London", "Great Britain"), Olympics( 2016, "Rio de Janeiro", "Brazil" ), Olympics( 9999, "end of", "data" ) ) print( "\n This program can tell where the Olympic " + "\n Games were held in a given year. Give " + "\n a year by using four digits: " ) val given_year = readLine()!!.toInt() var olympics_index = 0 var table_search_ready = false while ( table_search_ready == false ) { if ( olympics_table[ olympics_index ].olympic_year == given_year ) { olympics_table[ olympics_index ].print_olympics_data() table_search_ready = true } else if ( olympics_table[ olympics_index ].olympic_year == 9999 ) { print( "\n Sorry, no Olympic Games were held in " + given_year + ".\n" ) table_search_ready = true } else { olympics_index += 1 } } print( "\n" ) }