This is an easy solution, all i need is one method to be finished. This is what
ID: 3560094 • Letter: T
Question
This is an easy solution, all i need is one method to be finished. This is what I need the method to do
Ad updatePosition: The method updatePosition should set the field position to the current GPS position. We simulate the movement of the GPS and the reading of the current GPS position by calculating a random numbers between -2.5 and 2.5 (exclusive) and adding it to the longitude. Then it calculates another random number form the same range and adds it to the latitude.
here is the code:
package GPS;
public class GPS {
private String name;
private GPSCoordinates position;
GPS(String n, GPSCoordinates pos)
{
name = n;
updatePosition(pos);
}
public GPSCoordinates getPosition() {
return position;
}
public GPSCoordinates updatePosition(GPSCoordinates position) {
this.position = position;
return position;
}
@Override
public String toString()
{
return name + ": " + position;
}
}
package GPS;
public class GPSCoordinates {
private double latitude;
private double longitude;
GPSCoordinates(double lat, double lon)
{
setLatitude(lat);
setLongitude(lon);
}
public double getLatitude() {
return latitude;
}
public void setLatitude(double latitude) {
this.latitude = latitude;
}
public double getLongitude() {
return longitude;
}
public void setLongitude(double longitude) {
this.longitude = longitude;
}
public String toString()
{
return latitude + ", " + longitude;
}
}
Explanation / Answer
//Please find the required solution
//changes in bold letters and added main() for testing
//GPS.java
package GPS;
public class GPS {
private String name;
private GPSCoordinates position;
GPS(String n, GPSCoordinates pos)
{
name = n;
updatePosition(pos);
}
public GPSCoordinates getPosition() {
return position;
}
public GPSCoordinates updatePosition(GPSCoordinates position) {
//generating the random numbers between -2.5 to 2.5 and assign to current position
position.setLatitude(position.getLatitude()+(Math.random()*5)-2.5);
position.setLongitude(position.getLongitude()+(Math.random()*5)-2.5);
this.position = position;
return position;
}
@Override
public String toString()
{
return name + ": " + position;
}
public static void main(String[] args)
{
GPSCoordinates gpscoord=new GPSCoordinates(26.7, 32.5);//initial coordinates
GPS gp=new GPS("place", gpscoord);
System.out.println(gp);//coordinates after change(because update in constructor)
gp.updatePosition(gpscoord);
System.out.println(gp);//coordinates after change because we called update
}
}
//GPSCoordinates.java
package GPS;
public class GPSCoordinates {
private double latitude;
private double longitude;
GPSCoordinates(double lat, double lon)
{
setLatitude(lat);
setLongitude(lon);
}
public double getLatitude() {
return latitude;
}
public void setLatitude(double latitude) {
this.latitude = latitude;
}
public double getLongitude() {
return longitude;
}
public void setLongitude(double longitude) {
this.longitude = longitude;
}
public String toString()
{
return latitude + ", " + longitude;
}
}
Sample outPut:
place: 25.20241337342978, 30.808918307811894
place: 26.95065375183622, 31.203480075510278
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.