C++
CSCI2312 – Object Oriented Programming Section 003
Homework 4 Fall 2022 Page 1 of 6
Homework #4
1. Goals The goal of this homework is to exercise the concepts of Object-Oriented Programming in C++ language.
2. Problem Description You will adapt the application you implemented for homework #3 to follow the Object-Oriented Programming Paradigm. You will implement the classes and methods described in the following UML Class Diagram.
2.1. Implementation Details
2.1.1. Provided Database Files
• The data files to use are essentially the same ones used in homework 3, tough a fresh copy will be available.
2.1.2. Source Files
• Place your main program in the main.cpp file.
• Implement each class (City and Distance) using both a .h and .cpp files (a total of four files).
2.1.3. Main Program
• The main program, depicted by the “class” main in the UML diagram, but not an actual class, will have an array of City objects (up to 30 cities).
• See section 3 for more details on the main program functionality.
CSCI2312 – Object Oriented Programming Section 003
Homework 4 Fall 2022 Page 2 of 6
Function Name loadCities
Description Loads the city information and creates the objects to store the distance information
Returns Does not return data.
Parameters None. Comments Declared and implemented in main.cpp reads both cities.txt and distances.txt files
and creates the necessary objects to store cities and their distances. Hint: Read the cities from the city.txt file and for each one create an object of the type City. Read the distances, and for each target city, get the City object corresponding to the source city and call the setDistance method on the object with the target city name and the distance to that city. Should be easier to read the first city and the process all the distances for that city, then move forward to the second city and so on. This is recommended but you can process each file independently. The number of cities would be defined as a constant in your main program. Use that constant in the function. Hint 2: use upper-case strings.
Function Name toUppercase
Description Converts a string to uppercase
Returns Returns a new string corresponding to the string argument into uppercase
Parameters 1) a string which should not be modified
Comments Declared and implemented in main.cpp
2.1.4. Distance Class
The Distance class stores the distance to a particular city. The source is not stored as a piece of data in the Distance object, but it is rather determined by which object owns the Distance object. For example, considering a Distance object with Denver as cityName, stored in the listDistances in Los Angeles City object, then the distance value (in miles) will be the distance from Los Angeles to Denver. The default constructor should initialize the city’s name to an empty string and the distance to zero. Getters are implemented for both the city name and distance. In addition, a setter for distance is provided.
2.1.5. City Class
The City class stores the information on distances from that city to the others. It stores the city name and a list of distances to other cities implemented as a static array of up to 30 distances. A counter needs to be kept to properly index the Distance objects in the array (hint see section 5.3 in our text). The default constructor initializes the city name to the empty string and the counter to 0. A custom constructor takes the city name used to initialize the object. A copy constructor is also provided to copy the city’s name and implement a deep copy of the Distance object. A getter and a setter are provided for the name of the city.
CSCI2312 – Object Oriented Programming Section 003
Homework 4 Fall 2022 Page 3 of 6
City Class Methods
Function Name setDistance
Description Sets the distance to another city. This method should create a Distance object and adds it to the array.
Returns Does not return data. Parameters 1) the target city name
2) the distance to that target city
Comments
Function Name addDetour
Description Adds a detour between two cities by adding additional miles to the trip.
Returns Does not return data. Parameters 1) the target city name
2) the distances in miles to increment the trip.
Comments Needs to search for a Distance object with the given city’s name. When found, computes the new distance and sets it to the object.
Function Name distanceTo
Description Computes the distance to another city.
Returns Returns a integer with the distance value Parameters 1) the target city name (string)
Comments
Function Name tripCost
Description Computes the estimated cost in dollars for a trip.
Returns Returns a double precision floating point number with the cost value Parameters 1) the target city name (string)
2) the miles per galon (mpg) as a double 3) the gas cost per gallon (double)
Comments 𝑑𝑖𝑠𝑡𝑎𝑛𝑐𝑒 / 𝑀𝑃𝐺 × 𝐺𝑎𝑙𝑙𝑜𝑛𝑃𝑟𝑖𝑐𝑒
Function Name getAverageDistance
Description Computes the average distance of a trip from the city.
Returns Returns a double precision floating point number with the average
Parameters None.
Comments The source city should NOT be considered as a target when computing the average (i.e., do not count Denver to Denver trip). Use a for loop and recall the continue statement.
Function Name getClosestCity
Description Computes the closest city and returns its name.
Returns Returns the city name
Parameters None
CSCI2312 – Object Oriented Programming Section 003
Homework 4 Fall 2022 Page 4 of 6
Comments The source city should not be considered as a target when computing the average (i.e., do not count Denver to Denver).
Function Name getFarthestCity
Description Computes the farthest city and returns its name
Returns Returns the city name.
Parameters None Comments The source city should not be considered as a target when computing the average (i.e., do
not count Denver to Denver).
Function Name getSecondClosestCities
Description Returns the name of the second to closest city.
Returns Returns the city name.
Parameters None Comments The source city should not be considered as a target when computing the average (i.e., do
not count Denver to Denver).
Function Name getSecondFarthestCities
Description Returns the name of the second to fartherst city.
Returns Returns the city name.
Parameters None
Comments The source city should not be considered as a target when computing the average (i.e., do not count Denver to Denver).
CSCI2312 – Object Oriented Programming Section 003
Homework 4 Fall 2022 Page 5 of 6
3. Program Adapt the program you wrote for homework 3 to now use the object structure described above. The basic application will present the following menu: ---------------------------------
Main Menu
------------------------------
1) Load Cities and Distances
2) Add Weather Detour
3) Distance Between Cities
4) Distance and Trip Cost
5) Average Trip Distance
6) Closest City
7) Farthest City
8) Closest Two Cities
9) Farthest Two Cities
99) EXIT
------------------------------
• Option (1): loads cities and distances, will load the database from the provided files. This will be the first step in the program. When other options (2 through 9) are selected before loading the database, your program should display a warning message to the user guiding him/her to load the database first.
• Option (2): Adding a weather detour will ask for a source city, a destination city, and a detour distance which will be added to the travel distance between the source and destination. Your program should also ask if the detour is one way (source to target) or both ways (source to target and target to source) as well.
• Option (3): asks the user for two city names and will display the distance, expressed in miles between the two cities. E.g., “The current distance between DENVER and LOS ANGELES is 1410 miles.” (this includes a 75mile detour)
• Option (4): asks for two city names, the average miles per gallon (mpg) performance of the car, and the average cost of the gas, to provide the information for the trip. E.g., “The current distance between DENVER and LOS ANGELES is 1410 miles. The trip would cost $145.41 in a car that performs 32mpg and a gas price of $3.30/gallon.”
• Option (5): asks for a city name and displays the average distance for a trip starting at that city. E.g., “From DENVER the average trip distance is 1850.14 miles”.
• Option (6): asks for a city name and displays the closest city to the given one. E.g., “The closest city to DENVER is PHOENIX, 942 miles away.”
• Option (7): asks for a city name and displays the farthest city to the given one. E.g., “The farthest city to DENVER is BOSTON, 2838 miles away.”
• Option (8): asks for a city name and displays the two closest cities to the given one. E.g., “The two closest cities to DENVER are PHOENIX, 942 miles away, and DALLAS, 1064 miles away.”
• Option (9): asks for a city name and displays the farthest city to the given one. E.g., “The two farthest cities to DENVER are BOSTON, 2838 miles away, and MIAMI, 2773 miles away.”
• Option (99): asks the user for confirmation and terminates the program. o The program should keep running until the user selects to terminate the program.
• All decimal numbers should be displayed with a precision of 2 decimal places.
CSCI2312 – Object Oriented Programming Section 003
Homework 4 Fall 2022 Page 6 of 6
4. Development Tips Use stubs to facilitate your development. This is, declare your classes and all methods and attributes, then for the implementation either leave the methods empty or just return a default “dummy” value. Once you do this for both City and Distance classes, your code should compile. Then you can implement one method at a time, and you can modify the main program along. If you prefer, you can write a separate main program that allows you to test a class and each of its methods separately from the main program described above.
5. Testing - Test your program. - Make sure that the changes for instance by adding a detour are reflected in the following option.
o Example: ▪ Get the distance between Denver and Los Angeles ▪ Add a detour of 100miles from Denver to Los Angeles ▪ Get the distance again and compare.
- Test inputs, e.g., type a city name that is not included. Your program should show an error, but your program needs to keep running.
6. Deliverables / Submission 1. Code should follow the discussed guidelines regarding naming conventions, coding style, and comments.
a. Comment your code. No need to comment on every single line of code but add comments explaining what you are doing.
2. Develop your code in CLion. You will submit the entire CLion Project.
3. Create a makefile to compile and run your code in CSE. Capture a screenshot of the compilation output and program running (see 4.c below).
4. You need to submit a total of three files to Canvas Assignment page: a. Hwk4.zip
i. A compressed file that includes all CLion files for the homework. ii. Should contain at least the main.cpp, City.h, City.cpp, Distance.h,
Distance.cpp, cities.txt, and distances.txt. iii. Include the Readme.md file in the CLion Project.
b. makefile i. The makefile you used to compile the program in CSE Grid.
c. hwk4.png or hwk4.jpg i. a screenshot of your program running on CSE Grid.
ii. Include the result of the option (3) between Denver and Boston.
5. Submit all your files in one single submission, otherwise, you will be overwriting your previous submission.
- 1. Goals
- 2. Problem Description
- 2.1. Implementation Details
- 2.1.1. Provided Database Files
- 2.1.2. Source Files
- 2.1.3. Main Program
- 2.1.4. Distance Class
- 2.1.5. City Class
- City Class Methods
- 3. Program
- 4. Development Tips
- 5. Testing
- 6. Deliverables / Submission