// -------------------------------------------------------
// File I/O example for 605.201
//
// Paul McNamee 8/11/98

import java.io.*;

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; }
}

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 {
      DataOutputStream dos = new DataOutputStream(new FileOutputStream("geo.data"));
      for(int i=0; i<places.length; i++) {
        dos.writeUTF(places[i].getName());  // write a string
        dos.writeFloat(places[i].getLatitude());  // write a float
        dos.writeFloat(places[i].getLongitude());
      }
      dos.close();
    }
    catch (IOException ioe) {
      System.out.println("IOException writing places to geo.data");
    }

    try {
      DataInputStream dis = new DataInputStream(new FileInputStream("geo.data"));
      Place bahrain = new Place("Bahrain", 25.0F, 50.0F);
      for(int i=0; i<places.length; i++) {
        String str = dis.readUTF();
        float a = dis.readFloat();
        float b = dis.readFloat();

        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");
      }
      dis.close();
    }
    catch (IOException ioe) {
      System.out.println("IOException reading places from geo.data");
    }

  }
}

// paulmac@nautilus: java Places
// Read in: Baltimore (35.5,-75.5)
//         Distance from Bahrain to Baltimore is 6033.5913 nmi
// Read in: Java (-5.0,110.0)
//         Distance from Bahrain to Java is 3930.407 nmi
// Read in: Hawaii (20.0,-160.0)
//         Distance from Bahrain to Hawaii is 7585.3477 nmi
// Read in: Equator (0.0,0.0)
//         Distance from Bahrain to Equator is 3262.1748 nmi
