Hi, I really need help to build the following program and have no idea where to
ID: 3540157 • Letter: H
Question
Hi, I really need help to build the following program and have no idea where to start....Help!
Write a program to allow the user to calculate the distance between cities on a map.
The distances are calculated in map unitson the 2D map.
Each city is given a set of coordinates relative to some point on the map. We'll call this origin: (0,0).
But in order to make the system useful, we'll have to add a few other features:
Each city has a name in addition to its coordinates. (You must be able to store cities with spaces in their names: Los Angeles, New York, etc.)
The user will select what action to take from a menu:
Make sure you allow them to choose their options by both the number and the capitalized letter(s) of the choice.
They cannot calculate a distance until at least two cities have been entered.
When they have more than two cities, show them the list of cities and have them pick the two cities between which to calculate the distance. Don't let them choose the same city for both ends of this mythical 'trip'.
When they have exactly two cities, assume these are the end-points and just print the distance between them.
Hint: You should have at least 2 classes %u2014 possibly 3 (see the options below). One might be Point%u2014 like the one from lecture/the examples. Another might be a Cityclasslike:
Don't forget to separate all the details of city handling from the details of list management.
Hint: Each class should be in its own library. Other libraries may also be used for collections of useful functions.
Explanation / Answer
public double GetDistanceBetweenPoints(double lat1, double long1, double lat2, double long2) { double distance = 0; double dLat = (lat2 - lat1) / 180* Math.PI; double dLong = (long2 - long1) / 180 * Math.PI; double a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) + Math.Cos(lat2) * Math.Sin(dLong/2) * Math.Sin(dLong/2); double c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a)); //Calculate radius of earth // For this you can assume any of the two points. double radiusE = 6378135; // Equatorial radius, in metres double radiusP = 6356750; // Polar Radius //Numerator part of function double nr = Math.Pow(radiusE * radiusP * Math.Cos(lat1 / 180 * Math.PI), 2); //Denominator part of the function double dr = Math.Pow(radiusE * Math.Cos(lat1 / 180 * Math.PI), 2) + Math.Pow(radiusP * Math.Sin(lat1 / 180 * Math.PI), 2); double radius = Math.Sqrt(nr / dr); //Calculate distance in meters. distance = radius * c; return distance; // distance in meters }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.