Question

I'm trying to Create a class that holds a location,Including the following methods:

Setters (mutators) for each of the data fields For the location name, you should use the trim() method to remove any leading or trailing blank spaces.

Getters (accessors) for each of the data fields

Constructor: You should have only one constructor that takes the place name, the latitude, and the longitude. You may expect that the data will be valid, although the strings may need to be trimmed.

public class Location {
    public Location(String aCity, double aLatitude, double aLongitude)
    {
        String city = aCity;
        double latitude = aLatitude;
        double longitude = aLongitude;
    }
    void setLocation(String theCity)
    {
        city = theCity.trim();
    }
    void setLatitude(double lat)
    {
        latitude = lat;
    }
    void setLongitude(double long1)
    {
        longitude = long1;
    }
    public String getLocation()
    {
        return city;
    }
    public double getLatitude()
    {
        return latitude;
    }
    public double getLongitude()
    {
    return longitude;
    }
}

I'm not sure where I'm going wrong here, I'm getting errors on both citys, both latitudes, and both longitudes. This is my first time writing a class, so please dumb everything down for me. Thanks for your time.

Was it helpful?

Solution

You are almost there. You have:

public Location(String aCity, double aLatitude, double aLongitude)
{
    String city = aCity;
    double latitude = aLatitude;
    double longitude = aLongitude;
}

You are declaring local variables in the constructor. You actually want to be declaring fields:

public class Location {

    private String city;
    private double latitude;
    private double longitude;

    public Location(String aCity, double aLatitude, double aLongitude)
    {
        city = aCity;
        latitude = aLatitude;
        longitude = aLongitude;
    }

    ...

}

Check out the official tutorial on declaring member variables. It is concise and well-written and will give you a better idea of what is going on.

OTHER TIPS

Here is what you're doing wrong:

In your code, you declare the variables in the constructor. That will make them only visible by the constructor. Instead of looking like this:

public class Location {
    public Location(String aCity, double aLatitude, double aLongitude)
    {
        String city = aCity;
        double latitude = aLatitude;
        double longitude = aLongitude;
    }
    void setLocation(String theCity)
    {
        city = theCity.trim();
    }
    void setLatitude(double lat)
    {
        latitude = lat;
    }
    void setLongitude(double long1)
    {
        longitude = long1;
    }
    public String getLocation()
    {
        return city;
    }
    public double getLatitude()
    {
        return latitude;
    }
    public double getLongitude()
    {
    return longitude;
    }
}

Your code should look like this:

public class Location {

    String city = null;
    double latitude;
    double longitude;

    public Location(String aCity, double aLatitude, double aLongitude)
    {
        city = aCity;
        latitude = aLatitude;
        longitude = aLongitude;
    }
    void setLocation(String theCity)
    {
        city = theCity.trim();
    }
    void setLatitude(double lat)
    {
        latitude = lat;
    }
    void setLongitude(double long1)
    {
        longitude = long1;
    }
    public String getLocation()
    {
        return city;
    }
    public double getLatitude()
    {
        return latitude;
    }
    public double getLongitude()
    {
        return longitude;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top