// PlacesOnEarth.java (c) 2005 Kari Laitinen // http://www.naturalprogramming.com // 2005-05-04 File created. // 2005-05-05 Last modification. // This program demonstrates the use of some Math methods. // Class PlaceOnEarth presented in this program is a lightweight // version of the Place_on_earth class which is presented in // the programs of my C++ book. import java.util.* ; class PlaceOnEarth { String place_name ; String country_name ; int degrees_of_latitude ; int minutes_of_latitude ; int degrees_of_longitude ; int minutes_of_longitude ; public PlaceOnEarth( String given_place_name, String given_country_name, int given_degrees_latitude, int given_minutes_latitude, int given_degrees_longitude, int given_minutes_longitude ) { place_name = given_place_name ; country_name = given_country_name ; degrees_of_latitude = given_degrees_latitude ; minutes_of_latitude = given_minutes_latitude ; degrees_of_longitude = given_degrees_longitude ; minutes_of_longitude = given_minutes_longitude ; } public String get_place_name() { return place_name ; } public String get_country_name() { return country_name ; } public long get_distance_to( PlaceOnEarth another_place ) { long distance_to_caller = calculate_distance_on_earth_surface( degrees_of_latitude, minutes_of_latitude, degrees_of_longitude, minutes_of_longitude, another_place.degrees_of_latitude, another_place.minutes_of_latitude, another_place.degrees_of_longitude, another_place.minutes_of_longitude ) ; return distance_to_caller ; } static final double EARTH_RADIUS_IN_KILOMETERS = 6370 ; public static long calculate_distance_on_earth_surface( double point_1_latitude, double point_1_longitude, double point_2_latitude, double point_2_longitude ) { // All latitudes and longitudes must be expressed // in radians when calling this method. double cosine_of_angle_between_points = Math.cos( point_1_latitude ) * Math.cos( point_2_latitude ) * Math.cos( point_1_longitude - point_2_longitude ) + Math.sin( point_1_latitude ) * Math.sin( point_2_latitude ) ; double angle_between_points = Math.acos( cosine_of_angle_between_points ) ; long distance_to_caller = (long) ( angle_between_points * EARTH_RADIUS_IN_KILOMETERS ) ; return distance_to_caller ; } // The following function takes arguments as degrees and minutes, // converts them to radians, and then calls the function above. public static long calculate_distance_on_earth_surface( int point_1_latitude_degrees, int point_1_latitude_minutes, int point_1_longitude_degrees, int point_1_longitude_minutes, int point_2_latitude_degrees, int point_2_latitude_minutes, int point_2_longitude_degrees, int point_2_longitude_minutes ) { return calculate_distance_on_earth_surface( Math.toRadians( (double) point_1_latitude_degrees + (double) point_1_latitude_minutes / 60.0 ), Math.toRadians( (double) point_1_longitude_degrees + (double) point_1_longitude_minutes / 60.0 ), Math.toRadians( (double) point_2_latitude_degrees + (double) point_2_latitude_minutes / 60.0 ), Math.toRadians( (double) point_2_longitude_degrees + (double) point_2_longitude_minutes / 60.0 ) ) ; } } class PlacesOnEarth { public static void main( String[] not_in_use ) { Scanner keyboard = new Scanner( System.in ) ; PlaceOnEarth new_york_in_usa = new PlaceOnEarth( "New York", "New York USA", 40, 45, -74, 00 ) ; PlaceOnEarth tokyo_in_japan = new PlaceOnEarth( "Tokyo", "Japan", 35, 45, 139, 45 ) ; System.out.print( "\n Distance is " + new_york_in_usa.get_distance_to( tokyo_in_japan ) + "\n\n" ) ; } }