Problem 2: City Data Abstraction (200pts)
Say we have an abstract data type for cities. A city has a name, a latitude coordinate, and a longitude coordinate.
Our ADT has one constructor:
make_city(name, lat, lon): Creates a city object with the given name, latitude, and longitude.We also have the following selectors in order to get the information for each city:
get_name(city): Returns the city's nameget_lat(city): Returns the city's latitudeget_lon(city): Returns the city's longitudeHere is how we would use the constructor and selectors to create cities and extract their information:
假设我们有一个表示城市(city)的抽象数据类型(ADT)。一个城市有一个名称(name)、一个纬度坐标(latitude)和一个经度坐标(longitude)。
我们的 ADT 有一个构造函数:
make_city(name, lat, lon):使用给定的名称、纬度和经度创建一个城市对象。
我们还有以下选择函数,用于获取每个城市的信息:
get_name(city):返回城市的名称get_lat(city):返回城市的纬度get_lon(city):返回城市的经度
以下是我们如何使用构造函数和选择函数来创建城市并提取它们的信息:
>>> nanjing = make_city('Nanjing', 31, 118)
>>> get_name(nanjing)
'Nanjing'
>>> get_lat(nanjing)
31
>>> beijing = make_city('Beijing', 39, 116)
>>> get_lon(beijing)
116
All of the selector and constructor functions can be found in the lab file, if you are curious to see how they are implemented. However, the point of data abstraction is that we do not need to know how an abstract data type is implemented, but rather just how we can interact with and use the data type.
如果您好奇这些选择函数和构造函数是如何实现的,可以在实验文件中找到它们。然而,数据抽象的要点在于我们不需要知道一个抽象数据类型是如何实现的,而只需要知道我们如何与之交互并使用该数据类型。