Reputation: 11
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);
}
}
Upvotes: 1
Views: 1192
Reputation: 692
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);
}
}
Upvotes: 0
Reputation: 1739
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);
}
Upvotes: 0
Reputation: 94499
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.
Upvotes: 4