In: Computer Science
Latitude and Longitude are the two components used to exactly identify a specific spot on the Earth. Latitude provides the North/South direction and runs from 90 degrees north at the north poll to -90 degrees south at the south poll. The equator has a Latitude of zero.
Longitude provides the East/West direction and runs from the Prime Meridian that runs through Greenwich, England and has a Longitude of zero. Longitudes then run negatively to the west and positively to the east with the maximum Longitude being 180 degrees (or -180 degrees. They are the same Longitude.)
Usually Latitude and Longitude are usually depicted in degrees, minutes and second, but they can also be represented with just real numbers, which is how your class will need to be written. (FYI Lake Worth has a 26.61353 latitude and -80.057458)
Your assignment is to create a class called locCoordinates with the following properties and methods:
class locCoordinates
{
private:
double latitude;
double longitude;
string location;
public:
locCoordinates();
bool set(double, double, string)
bool setLatitude(double);
bool setLongitude(double);
void setLocation(string);
double getLatitude();
double getLongitude();
string getLocation();
void moveUpDown (double, string);
void moveLeftRight (double, string);
void move (double, double, string);
void print();
}
And here are the specifics for this class.
The constructor should set the latitude and longitude to zero, and the location to "Prime Meridian at Equator). It should not accept any parameters (and should not call the set function).
The set function will take the two numbers entered (latitude then longitude) and ensure they are correct. If either of the numbers are not within the correct range, then the location should be set to "Error", the value of the invalid entry should be set to zero and the function return a false. If everything is good it returns a true.
setLatitude will change the current value of Latitude. It also needs to check to make sure that it is a good latitude. If not correct the function should set the Latitude to zero and return false otherwise return true. In either case, the Location should be changed to "Unknown".
setLongitude does what setLatitude does, only to the Longitude.
setLocation just updates the location string in the class.
The gets return the values in the properties.
moveUpDown will pass in a double that may be either negative or positive. This value needs to be adjusted for the number of total degrees in the latitude, and then the latitude adjusted by that amount. The location would then also be updated.
moveEastWest does just like moveUpDown, but does it for Longitude.
move takes both a change in Latitude and Longitude and applies them along with setting the new location. (If I were coding this, I would probably just call moveUpDown and moveEastWest to do the work!!)
print will print out:
Latitude: 99.99999, Longitude: 999.99999 is at location: XXXXXX
You can implement this any way you want either by creating separate class header and implementation files, or by just putting everything in one big ole file with a main routine after the class definition. In the main routine, please set up a location with a bad input and show that a false is returned, then set one up with good input. Then use the move command to adjust the coordinates by an amount above 90 (or below -90) for latitude and above 180 (or below -180) for longitude. Then use the print function to print out the results.
I need help it C++
Code:
#include <iostream>
using namespace std;
class locCoordinates // Given class
{
private:
double latitude;
double longitude;
string location;
public:
locCoordinates();
bool set(double, double, string);
bool setLatitude(double);
bool setLongitude(double);
void setLocation(string);
double getLatitude();
double getLongitude();
string getLocation();
void moveUpDown (double, string);
void moveLeftRight (double, string);
void move (double, double, string);
void print();
};
locCoordinates::locCoordinates() // setting the default
constructor
{
latitude =0;
longitude =0;
location = "Prime Meridian at Equator";
}
bool locCoordinates::set(double lat, double longi, string loc)
// function to set the latitude, longitude and location
{
location = loc;
if(lat >=-90 && lat <=90) // checking if the latitude
lies in the range
{
latitude =lat;
}
else
{
location = "Error";
latitude = 0;
if(longi >=-180 && longi <=180) // checking if the
longitude lies in the range
{
longitude =longi;
}
else
{
longitude =0;
}
return false;
}
if(longi >=-180 && longi <=180) // checking if the
longitude lies in the range
{
longitude =longi;
}
else
{
location = "Error";
longitude = 0;
if(lat >=-90 && lat <=90) // checking if the latitude
lies in the range
{
latitude =lat;
}
else
{
latitude =0;
}
return false;
}
return true;
}
bool locCoordinates::setLatitude(double lat) // function to set
the latitude
{
if(lat >=-90 && lat <=90) // checking if the latitude
lies in the range
{
latitude =lat;
location = "Unknown";
return true;
}
else
{
latitude =0;
location = "Unknown";
return false;
}
}
bool locCoordinates::setLongitude(double longi) // function to
set the longitude
{
if(longi >=-180 && longi <=180) // checking if the
longitude lies in the range
{
longitude =longi;
location = "Unknown";
return true;
}
else
{
longitude =0;
location = "Unknown";
return false;
}
}
void locCoordinates::setLocation(string loc) // function to set
the location
{
location = loc;
}
double locCoordinates::getLatitude() // function to return
latitude
{
return latitude;
}
double locCoordinates::getLongitude() // function to return
longitude
{
return longitude;
}
string locCoordinates::getLocation() // function to return
location
{
return location;
}
void locCoordinates::moveUpDown (double lat, string loc) //
function to move latitude
{
latitude += lat;
location = loc;
}
void locCoordinates::moveLeftRight (double longi, string loc) //
function to move longitude
{
longitude += longi;
location = loc;
}
void locCoordinates::move (double lat, double longi, string loc) //
function to move latitude and longitude
{
moveUpDown(lat,loc);
moveLeftRight(longi,loc);
}
void locCoordinates::print() // function to print the details of
the location
{
cout<<"Latitude : "<<latitude<<", Longitude:
"<<longitude<<" is at location:
"<<location<<endl;
}
int main()
{
locCoordinates location1;
cout<<"Details for location 1 : "<<endl;
location1.print();
cout<<"Value returned when the latitude is set to 91 :
"<<location1.setLatitude(91)<<endl;
location1.print();
locCoordinates location2;
cout<<"Details for location 2 : "<<endl;
location2.print();
location2.setLatitude(26.61353);
location2.setLongitude(-80.057458);
location2.setLocation("Lake Worth");
location2.print();
location2.move(63.38647,80.057458,"North Pole");
location2.print();
return 0;
}
Output: