1 Replies - 529 Views - Last Post: 06 June 2014 - 02:34 PM Rate Topic: -----

#1 dubxcity  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 21-March 14

GPS Java program

Posted 06 June 2014 - 01:48 PM

I'll post the assignment what I have coded and where I am stuck
1)Description:
 Declare the following three classes: Gps, GpsCoordinates, GpsApp.

Ad GpsCoordinates:Base your class declaration on the class diagram below. Do not add or remove any public methods or constructors. All the output happes in GpsApp. There should be NO print statements in class GpsCoordinates.
Ad toString: The method toString should return a String that includes the latitude followed by the longitude. Both latitude and longitude should display 6 digits after the decimal point. (see output)

Ad Gps:Base your class declaration on the class diagram below. Do not add or remove any public methods or constructors. All the output happes in GpsApp. There should be NO print statements in class Gps.
Ad toString: The method toString should return a String that includes the name followed by the position. (see output) Make sure to include the @Override annotation 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.

Ad toString: The method toString should return a String that includes the name followed by the position. (see output) Make sure to include the @Override annotation

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.

public class GpsCoordinates {
	
	private double latitude;
	private double longitude;
	double position;
	
	
	public GpsCoordinates(double lat, double lon) {
		this.latitude=lat;
		this.longitude=lon;
	}

	/**
	 * @return the latitude
	 */
	public double getLatitude() {
		return latitude;
	}

	/**
	 * @param latitude the latitude to set
	 */
	public void setLatitude(double latitude) {
		this.latitude = latitude;
	}

	/**
	 * @return the longitude
	 */
	public double getLongitude() {
		return longitude;
	}

	/**
	 * @param longitude the longitude to set
	 */
	public void setLongitude(double longitude) {
		this.longitude = longitude;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "GpsCoordinates of SLC: [latitude=" + latitude + ", longitude="
				+ longitude + "]";
	}
		

	public class Gps{
		private String name;
		private GpsCoordinates position;
		
		public Gps(String n, GpsCoordinates pos) {
			this.name=n;
			this.position=pos;			
		}
		public GpsCoordinates updatePosition(){
			this.position = position;
			
			//double position=(position.next(2.5)-5);
			return position;
			
		}
	}
}


I am stuck on writing the updatePosition method. Any help writing this method would be great

Is This A Good Question/Topic? 0
  • +

Replies To: GPS Java program

#2 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 822
  • View blog
  • Posts: 2,529
  • Joined: 29-July 11

Re: GPS Java program

Posted 06 June 2014 - 02:34 PM

So your instructions tell you to create a random number between -2.5 and 2.5, then add that to long and lat. The most difficult part of this is generating the number. I am not really good in math, but the Random class is what you need. There is a method nextdouble() that creates a double between 0 and 1.0. So how do you get a range of -2.5 to positive 2.5? First lets figure out how to get to the positive end, 2.5. That's easy. We multiply whatever comes out of nextdouble() by 2.5. Now how do we go negative? Well, that means we want double the amount of values that multiplying our random number by 2.5 would produce, so let's go ahead and multiply the number returned from nextdouble() by 5. Now we have a range from 0 to 5.0. So now how to we get negative values? Let's try subtracting 2.5 from what nextdouble() * 5 produces. What happens? Well, if we have 5.0 returned and we subtract 2.5, we have 2.5 f nextDouble() returns .2 and we multiply that by 5.0 we get 1. Now if we subtract 2.5 from 1 we get -1.5. It seems as if this works, so our formula becomes:

double number = (new Random().nextDouble() * 5.0) - 2.5);

So unless I am wrong, this will generate numbers between -2.5 and 2.5. I will leave it to you to format the number properly and to add it to lat and long. Good luck!
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1