Hi All,
This below is a rewrite that I did of a program that I discovered at
Google
Topic: Latitude and Longitude calculations in Java? contibutor:
#6 by Paul McName
http://groups.google.com/group/comp.lang.java.programmer/browse_frm/thread/6e0e9
702e3e0b9db?q=places+location+longitude&hl=en&
I never saw the notation "35.5F" and never saw " lat1 *= DEG2RAD;"
Yet it with the changes that I wrote it does run.
Is this authentic java?
Bob
import java.io.*;
import java.awt.*;
public class Places {
public static void main (String [] args) {
Place [ ] places = new Place [4];
places[0] = new Place("Baltimore", 35.5F, -75.5F);
places[1] = new Place("Java", -5.0F, 110.0F);
places[2] = new Place("Hawaii", 20F, -160.0F);
places[3] = new Place("Equator", 0F, 0F) ;
// Write Data
try {
FileOutputStream fos= new FileOutputStream("geo.txt");
BufferedWriter bow = new BufferedWriter(new
OutputStreamWriter(fos));
for(int i=0; i<places.length; i++) {
bow.write(places[i].getName()+"\n"); // write a string
bow.write(Float.toString(places[i].getLatitude())+"\n"); //
write a float
bow.write(Float.toString(places[i].getLongitude())+"\n");
}
bow.close();
}
catch (IOException ioe) {
System.out.println("IOException writing places to geo.txt");
}
try {
Place bahrain = new Place("Bahrain", 25.0F, 50.0F);
FileInputStream fis= new FileInputStream("geo.txt");
BufferedReader bor = new BufferedReader(new
InputStreamReader(fis));
String str,stra,strb ="";
//float a,b;
for(int i=0; i<places.length; i++) {
str = bor.readLine(); // write a string
stra = bor.readLine();
strb = bor.readLine();
float a = Float.valueOf(stra);
float b = Float.valueOf(strb);
System.out.println("Read in: " + str + " (" + a + "," + b +
")");
System.out.println("\tDistance from " + bahrain.getName() + "
to " + str + " is " +
Location.Distance(bahrain, new Place(str, a,
b)) + " nmi");
}
bor.close();
}
catch (IOException ioe) {
System.out.println("IOException reading places from geo.txt");
}
}
}
class Location {
private float latitude ;
private float longitude ;
// Default (no-argument constructor)
public Location ( ) {
latitude = longitude = 0.0F;
}
// Two-argument constructor
public Location(float lat, float lon) {
latitude = lat;
longitude = lon;
}
public float getLatitude() { return latitude ; }
public float getLongitude() { return longitude ;
}
public void setLatitude (float lat) {
latitude = lat;
}
public void setLongitude (float lon) {
longitude = lon;
}
static float Distance(Location l1, Location l2) {
float lat1 = l1.getLatitude();
float lat2 = l2.getLatitude();
float lon1 = l1.getLongitude();
float lon2 = l2.getLongitude();
float val;
final float DEG2RAD = 0.01745F ;
if ((lat1 == lat2) && (lon1 == lon2)) {
val = 0.0F;
} else {
lat1 *= DEG2RAD;
lat2 *= DEG2RAD;
lon1 *= DEG2RAD;
lon2 *= DEG2RAD;
val = (float) (60.0 * ((Math.acos((Math.sin(lat1) *
Math.sin(lat2)) +
(Math.cos(lat1) * Math.cos(lat2) *
Math.cos(lon2 - lon1))))
/ DEG2RAD));
}
return (val); // distance in nautilus miles
}
}
class Place extends Location {
String name;
public Place(String where, float lat, float lon) {
super(lat, lon);
name = where;
}
public String getName() { return name; }
}
IchBin - 02 Dec 2005 07:03 GMT
> Hi All,
>
[quoted text clipped - 6 lines]
> I never saw the notation "35.5F" and never saw " lat1 *= DEG2RAD;"
> Yet it with the changes that I wrote it does run.
[cut code]
lat1 *= DEG2RAD; is short for lat1 =lat1*DEG2RAD;
"35.5F" is saying 35.5 is a Float

Signature
Thanks in Advance...
IchBin, Pocono Lake, Pa, USA
http://weconsultants.servebeer.com/JHackerAppManager
__________________________________________________________________________
'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor, Regular Guy (1952-)
Oliver Wong - 02 Dec 2005 19:46 GMT
>> I never saw the notation "35.5F" and never saw " lat1 *= DEG2RAD;"
>> Yet it with the changes that I wrote it does run.
>
> "35.5F" is saying 35.5 is a Float
As opposed to a double. If you just put "35.5", Java will treat it as a
double. Similarly, "35" will be treated as a integer, but if you want it to
be a long, write "35L".
- Oliver
Rhino - 02 Dec 2005 17:38 GMT
> Hi All,
>
[quoted text clipped - 8 lines]
>
> Is this authentic java?
Try compiling it and executing it. Does it work? If yes, it is authentic
Java.....
Rhino
Roedy Green - 02 Dec 2005 18:23 GMT
>I never saw the notation "35.5F" and never saw " lat1 *= DEG2RAD;"
see http://mindprod.com/jgloss/precedence.html for the various ?=
operators.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
bherbst65@hotmail.com - 02 Dec 2005 23:05 GMT
Thanks to all of you for your help
Bob