1

So I'm trying to make it so that when the user creates a new PlaceInformation object, the inputted latitude and longitude are stored inside of the GeoLocation object place. Obviously, upon running the "client" code, the GeoLocation object is initialized first and is given a value of 0.0, 0.0 instead of what the user inputs. How do I make it so that the latitude and longitude parameters from the constructor store in the GeoLocation object? I tried it a different way but there were some scope issues.

public class PlaceInformation {
    private String name;
    private String tag;
    private String address;
    private double latitude;
    private double longitude;
    GeoLocation place = new GeoLocation(latitude, longitude);


    public PlaceInformation(String name, String address, String tag,
                        double latitude, double longitude) {
        this.name = name;
        this.address = address;
        this.tag = tag;
        this.latitude = latitude;
        this.longitude = longitude;
    }



    public String getName() {
        return name;
    }

    public String getAddress() {
        return address;
    }

    public String getTag() {
        return tag;
    }

    public String toString() {
        return name + ", " + address;
    }

    public double test() {
        return place.getLongitude();
    }

    public double distanceFrom(GeoLocation spot) {
        return spot.distanceFrom(place);
    }
}

3 Answers 3

4

Instantiate the object within the constructor.

    public class PlaceInformation {
        private String name;
        private String tag;
        private String address;
        private double latitude;
        private double longitude;
        GeoLocation place;


        public PlaceInformation(String name, String address, String tag,
                            double latitude, double longitude) {
            place = new GeoLocation(latitude, longitude);
            this.name = name;
            this.address = address;
            this.tag = tag;
            this.latitude = latitude;
            this.longitude = longitude;
        }

        /* Rest of class omitted */
}

Also note that you may want to make the place private.

Sign up to request clarification or add additional context in comments.

3 Comments

Ok I see, I didn't realize you could instantiate objects like that without providing the parameters. So in essence, until the user creates a PlaceInformation object, the GeoLocation object is just blank and waiting to be filled?
Exactly. Place points to a null reference until you instantiate the GeoLocation object and assign it to place. Since place is an instance variable you do not need to worry about a NullPointerException since the class must be instantiated for use. Another thing you might consider is removing the latitute and longitude fields from the PlaceInformation class, since those values are stored in place.
Yeah, I can see how those are redundant now. Thanks for the help, I really appreciate the prompt response!
0

How about initializing place field in PlaceInformation constructor:

public class PlaceInformation {
    private String name;
    private String tag;
    private String address;
    private double latitude;
    private double longitude;
    GeoLocation place = null;


    public PlaceInformation(String name, String address, String tag,
                        double latitude, double longitude) {
        this.name = name;
        this.address = address;
        this.tag = tag;
        this.latitude = latitude;
        this.longitude = longitude;
        place = new GeoLocation(latitude, longitude);
    }

Comments

0

You should declare Geolocation a private field. Then, in the constructor you create a new instance of the object. Then, the other methods will be able to "see" the place variable. Concluding, this is not "An object within an object" but a reference from one object to another.

public class PlaceInformation {
    private String name;
    private String tag;
    private String address;
    private double latitude;
    private double longitude;
    private GeoLocation place;


    public PlaceInformation(String name, String address, String tag,
                        double latitude, double longitude) {
        this.name = name;
        this.address = address;
        this.tag = tag;
        this.latitude = latitude;
        this.longitude = longitude;
        this.place = new GeoLocation(latitude, longitude);
    }



    public String getName() {
        return name;
    }

    public String getAddress() {
        return address;
    }

    public String getTag() {
        return tag;
    }

    public String toString() {
        return name + ", " + address;
    }

    public double test() {
        return place.getLongitude();
    }

    public double distanceFrom(GeoLocation spot) {
        return spot.distanceFrom(place);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.