Lab Notes: 2D Array for City Distances
Overview of the Lab
This lab focuses on working with a 2D array to calculate the distance between major American cities using a programming class.
Key Components
Class Variable: The
CityLayis a class variable, meaning there is only one instance of it regardless of how many objects of the class are created.2D Array Structure: The distance data is stored in an 8x8 2D array, where each row and column corresponds to one of the eight cities.
Main Program Functionality
Initial Display: The program starts by displaying an initial message that includes a list of legal cities.
User Input for City Codes: The
getCityCodemethod prompts the user to enter a city name and returns its corresponding city code. If the name is unrecognized, the user is prompted to enter another.Calculating Distances: Once the user selects two cities, the program retrieves the distance by locating the corresponding indices in the distance array.
Example Calculation
Example input: Selecting New York as the origin and Atlanta as the destination. The program would return that the distance between New York and Atlanta is 850 miles.
Conclusion
The lecture concludes with running through an example to demonstrate the array and the inputs required for distance calculations.