Question

I need this in c++ please Write a Map class that is composed of a Location...

I need this in c++ please

Write a Map class that is composed of a Location class. Location class has three private variable string nameOfCity, double variables, latitude and longitude. It has a constructor to initialize these and also the usual getters. It has three functions: string toString() which prints :double getFlyingTime(Location l) which returns the flying time between this and location l. It uses the formula that time = distance/speed. speed = 673 miles/hour. No need to convert to minutes. : double getDistance(Location l). This method is used by getFlyingTime. The Map class has function double getFlyingTime(string l, string r); It returns -1 if the two cities are not found in its database. Map class has a vector of 4 location objects. The driver program creates the map by providing the locations to the Map. Once the Map is created, it asks the user for two cities. If the cities are found, it returns the flying distance otherwise it says cities not found. The cities have first letter capitalized and rest small.

I need help implementing the main.cpp

//Location.h

#pragma once

#include <string>

class Location

{

std::string nameOfCity;

double latitude;

double longitude;

public:

Location() = default;

Location(double lat, double longat)

{

latitude = lat;

longitude = longat;

}

std::string getNameOfCity();

double getLatitude();

double getLongitude();

std::string toString();

double getFlyingTime(Location l);

double getDistance(Location l);

};

//Location.cpp

#include "Location.h"

#include <math.h>

using namespace std;

double DegreeToRadians(double degree)

{

return (degree* 3.14 / 180); // We can use PI from math librabry as well for precise values

}

std::string Location::getNameOfCity()

{

return nameOfCity;

}

double Location::getLatitude()

{

return latitude;

}

double Location::getLongitude()

{

return longitude;

}

std::string Location::toString()

{

return "Latitude: " + to_string(latitude) + ", Longitude: " + to_string(longitude);

}

double Location::getFlyingTime(Location l)

{

return getDistance(l) / 673;

}

double Location::getDistance(Location l)

{

double lat1r = DegreeToRadians(latitude);

double lon1r = DegreeToRadians(longitude);

double lat2r = DegreeToRadians(l.getLatitude());

double lon2r = DegreeToRadians(l.getLongitude());

double u = sin((lat2r - lat1r) / 2);

double v = sin((lon2r - lon1r) / 2);

return 2.0 * 6371.0 * asin(sqrt(u * u + cos(lat1r) * cos(lat2r) * v * v)); // 6371.0 is Earth's radius

}

//Map.h

#pragma once

#include <vector>

#include "Location.h"

class Map

{

std::vector<Location> locations;

public:

void addLocation(Location l);

double getFlyingTime(std::string l, std::string r);

};

//Map.cpp

#include "Map.h"

using ::Map;

void Map::addLocation(Location l)

{

for (auto& c : locations)

{

if (c.getNameOfCity() == l.getNameOfCity())

return;

}

locations.emplace_back(l);

}

double Map::getFlyingTime(std::string l, std::string r)

{

bool city1Found = false;

bool city2Found = false;

Location loc1;

Location loc2;

for (auto& c : locations)

{

if (c.getNameOfCity() == l)

{

loc1 = c;

city1Found = true;

}

if (c.getNameOfCity() == r)

{

city2Found = true;

loc2 = c;

}

}

if (city1Found && city2Found)

{

return loc1.getFlyingTime(loc2);

}

return -1;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

// Location.h

#pragma once

#include <string>

class Location

{

std::string nameOfCity;

double latitude;

double longitude;

public:

Location() = default;

// add city argument in constructor as there is no way to set the nameOfCity in the class

Location(std::string city,double lat, double longat)

{

nameOfCity = city;

latitude = lat;

longitude = longat;

}

std::string getNameOfCity();

double getLatitude();

double getLongitude();

std::string toString();

double getFlyingTime(Location l);

double getDistance(Location l);

};

//end of Location.h

// Location.cpp

#include "Location.h"

#include <math.h>

using namespace std;

double DegreeToRadians(double degree)

{

return (degree* 3.14 / 180); // We can use PI from math librabry as well for precise values

}

std::string Location::getNameOfCity()

{

return nameOfCity;

}

double Location::getLatitude()

{

return latitude;

}

double Location::getLongitude()

{

return longitude;

}

std::string Location::toString()

{

return "Latitude: " + to_string(latitude) + ", Longitude: " + to_string(longitude);

}

double Location::getFlyingTime(Location l)

{

return getDistance(l) / 673;

}

double Location::getDistance(Location l)

{

double lat1r = DegreeToRadians(latitude);

double lon1r = DegreeToRadians(longitude);

double lat2r = DegreeToRadians(l.getLatitude());

double lon2r = DegreeToRadians(l.getLongitude());

double u = sin((lat2r - lat1r) / 2);

double v = sin((lon2r - lon1r) / 2);

return 2.0 * 6371.0 * asin(sqrt(u * u + cos(lat1r) * cos(lat2r) * v * v)); // 6371.0 is Earth's radius

}

//end of Location.cpp

// Map.h

#pragma once

#include <vector>

#include "Location.h"

class Map

{

std::vector<Location> locations;

public:

void addLocation(Location l);

double getFlyingTime(std::string l, std::string r);

};

//end of Map.h

//Map.cpp

#include "Map.h"

using ::Map;

void Map::addLocation(Location l)

{

for (auto& c : locations)

{

if (c.getNameOfCity() == l.getNameOfCity())

return;

}

locations.emplace_back(l);

}

double Map::getFlyingTime(std::string l, std::string r)

{

bool city1Found = false;

bool city2Found = false;

Location loc1;

Location loc2;

for (auto& c : locations)

{

if (c.getNameOfCity() == l)

{

loc1 = c;

city1Found = true;

}

if (c.getNameOfCity() == r)

{

city2Found = true;

loc2 = c;

}

}

if (city1Found && city2Found)

{

return loc1.getFlyingTime(loc2);

}

return -1;

}

//end of Map.cpp

// main.cpp : C++ program to get the flying time between 2 cities input by the user

#include "Map.h"

#include <iostream>

#include <string>

#include <ctype.h>

using namespace std;

int main()

{

               Map map; // create a Map object

               // add 4 locations to Map

               Location l1("Las vegas",36.1699,115.1398);

               Location l2("Mumbai",19.0760,72.8777);

               Location l3("Dallas",32.7767,96.7970);

               Location l4("New york",40.7128,74.0060);

               map.addLocation(l1);

               map.addLocation(l2);

               map.addLocation(l3);

               map.addLocation(l4);

               string city1, city2;

               // input the 2 cities

               cout<<" Enter the name of first city (first letter capitalized and rest small ) : ";

               getline(cin,city1);

               cout<<" Enter the name of second city (first letter capitalized and rest small ) : ";

               getline(cin,city2);

               // format the city names such that first letter is capitalized and rest are small

               string mod_city1="";

               mod_city1 += toupper(city1.at(0));

               for(size_t i=1;i<city1.length()-1;i++)

                              mod_city1 += tolower(city1.at(i));

               string mod_city2="";

               mod_city2 += toupper(city2.at(0));

               for(size_t i=1;i<city2.length()-1;i++)

                              mod_city2 += tolower(city2.at(i));

               // get the flying time

               double flying_time=map.getFlyingTime(mod_city1,mod_city2);

               if( flying_time == -1) // check if city(s) found in database

                              cout<<" City(s) not found in the database"<<endl;

               else

                              cout<<" Flying time between "<<mod_city1<<" and "<<mod_city2<<" : "<<flying_time<<" hours"<<endl;

               return 0;

}

//end of program

Output:

Enter the name of first city (first letter capitalized and rest small) : new york Enter the name of second city (first letter capitalized and rest small: dallas Flying time between New york and Dallas: 3.27648 hours

Add a comment
Know the answer?
Add Answer to:
I need this in c++ please Write a Map class that is composed of a Location...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ Programming Hi! Sorry for all the material attached. I simply need help in writing the...

    C++ Programming Hi! Sorry for all the material attached. I simply need help in writing the Facility.cpp file and the other files are included in case they're needed for understanding. I was able to complete the mayday.cpp file but am stuck on Facility. The following link contains a tar file with the files provided by the professor. Thank you so much in advanced! http://web.cs.ucdavis.edu/~fgygi/ecs40/homework/hw4/ Closer.h: #ifndef CLOSER_H #define CLOSER_H #include <string> #include "gcdistance.h" struct Closer { const double latitude, longitude;...

  • The following C++  code contains an incomplete program that should be able to calculate the distance between...

    The following C++  code contains an incomplete program that should be able to calculate the distance between a series of geocoded locations. Two parts of the program need to be completed: 1. The portion of the main() function that calculates the distances between each of the hard-coded 5 locations, and stores it in a two-dimensional array declared as distances. 2. A portion of the calculateDistanceBetweenLocations(l1, l2) function; omitted code spaces are designed with a // TODO: comment. The code is properly...

  • Extend the code from Lab6.A template is given to you with CircleHeader.h, Circle.cpp and CircleMain.cpp Use...

    Extend the code from Lab6.A template is given to you with CircleHeader.h, Circle.cpp and CircleMain.cpp Use the same Circle UML as below and make extensions as marked and as needed. Given below is a UML for the Painting Class. You will need to write PaintingHeader.h and Painting.cpp. The Painting Class UML contains a very basic skeleton. You will need to flesh out this UML and add more instance variables and methods as needed. You do NOT need to write a...

  • I am stuck on this question. Write the class, GeoLocation.java. You can refer to Name.java used...

    I am stuck on this question. Write the class, GeoLocation.java. You can refer to Name.java used in the lecture to complete the below exercises. Do the following: 1. Create two instance variables, lat and lng, both of which should be doubles. 2. Write the default constructor. 3. Write the non-default constructor. 4. Write 2 accessor methods, one for each instance variable. 5. Write 2 mutator methods, one for each instance variable. 6. Write a method that will return the location...

  • Please Implement ParkingLot.cpp below based off the completed Automobile Class and ClaimCheck class down below. Automobile.hpp...

    Please Implement ParkingLot.cpp below based off the completed Automobile Class and ClaimCheck class down below. Automobile.hpp #pragma once #include <iostream> #include <string> class Automobile { friend bool operator==( const Automobile& lhs, const Automobile& rhs ); friend std::ostream & operator<<( std::ostream& stream, const Automobile& vehicle ); private: std::string color_; std::string brand_; std::string model_; std::string plateNumber_; public: Automobile( const std::string & color, const std::string & brand, const std::string & model, const std::string & plateNumber ); }; bool operator!=( const Automobile& lhs, const...

  • This is for my c++ class and I would really appreciate the help, Thank you! Complete...

    This is for my c++ class and I would really appreciate the help, Thank you! Complete the definitions of the functions for the ConcessionStand class in the ConcessionStand.cpp file. The class definition and function prototypes are in the provided ConcessionStand.h header file. A testing program is in the provided main.cpp file. You don’t need to change anything in ConcessionStand.h or main.cpp, unless you want to play with different options in the main.cpp program. ___________________ Main.cpp ____________________ #include "ConcessionStand.h" #include <iostream>...

  • This is a simple C++ class using inheritance, I have trouble getting the program to work....

    This is a simple C++ class using inheritance, I have trouble getting the program to work. I would also like to add ENUM function to the class TeachingAssistant(Derived class) which is a subclass of Student(Derived Class) which is also a subclass of CourseMember(Base Class). The TeachingAssistant class uses an enum (a user-defined data type) to keep track of the specific role the TA has: enum ta_role {LAB_ASSISTANT, LECTURE_ASSISTANT, BOTH}; You may assume for initialization purposes that the default role is...

  • c++. I'm working on a hangman game. It has to utilize a class. It should show...

    c++. I'm working on a hangman game. It has to utilize a class. It should show the number of attempts that have been made, the length of the word represented by dots, and then the alphabet. There also needs to be input validation. If the word was horse, the very first attempt at the user guessing should display " 1 ..... choose: abcdefghijklmnopqrstuvwxyz ". As the user guesses letters, the number of attempts should go up, the letter should be...

  • Design a class named Triangle that extends GeometricObject class. The class contains: Three double data fields...

    Design a class named Triangle that extends GeometricObject class. The class contains: Three double data fields named side1, side2, and side3 with default values 1.0 to denote three sides of the triangle. A no-arg constructor that creates a default triangle with color = "blue", filled = true. A constructor that creates a triangle with the specified side1, side2, side3 and color = "blue", filled = true. The accessor functions for all three data fields, named getSide1(), getSide2(), getSide3(). A function...

  • In C++ please.. I only need the game.cpp. thanks. Game. Create a project titled Lab8_Game. Use...

    In C++ please.. I only need the game.cpp. thanks. Game. Create a project titled Lab8_Game. Use battleship.h, battleship.cpp that mentioned below; add game.cpp that contains main() , invokes the game functions declared in battleship.h and implements the Battleship game as described in the introduction. ——————————————- //battleship.h #pragma once // structure definitions and function prototypes // for the battleship assignment // 3/20/2019 #include #include #ifndef BATTLESHIP_H_ #define BATTLESHIP_H_ // // data structures definitions // const int fleetSize = 6; // number...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT