// PlanetsMap.java (c) Kari Laitinen // http://www.naturalprogramming.com // 2015-01-02 File created. // 2015-01-02 Last modification. // This program works in the same way as Planets.java. // In this version there is no conventional array for storing // Planet objects. Instead, the Planet objects are stored into // a data structure called HashMap. // You will see that it is very easy to search for an object in // a HashMap. import java.util.* ; class Planet { String planet_name ; double mean_distance_from_sun ; double circulation_time_around_sun ; double rotation_time_around_own_axis ; long equatorial_radius_in_kilometers ; double mass_compared_to_earth ; int number_of_moons ; public Planet( String given_planet_name, double given_mean_distance_from_sun, double given_circulation_time_around_sun, double given_rotation_time_around_own_axis, long given_equatorial_radius_in_kilometers, double given_mass_compared_to_earth, int given_number_of_moons ) { planet_name = given_planet_name ; mean_distance_from_sun = given_mean_distance_from_sun ; circulation_time_around_sun = given_circulation_time_around_sun ; rotation_time_around_own_axis = given_rotation_time_around_own_axis ; equatorial_radius_in_kilometers = given_equatorial_radius_in_kilometers ; mass_compared_to_earth = given_mass_compared_to_earth ; number_of_moons = given_number_of_moons ; } public String get_planet_name() { return planet_name ; } public void print_planet_data() { System.out.print( "\n " + planet_name + " orbits the sun in average distance of " + ( mean_distance_from_sun * 149500000 ) + " kilometers." + "\n It takes " + circulation_time_around_sun + " years for " + planet_name + " to go around the sun once. \n The radius of " + planet_name + " is " + equatorial_radius_in_kilometers + " kilometers.\n" ) ; } } public class PlanetsMap { public static void main( String[] not_in_use ) { Scanner keyboard = new Scanner( System.in ) ; // First we create a HashMap. HashMap planet_table = new HashMap() ; // Then we put Key-Object pairs to the table. Each key is a String, and // the objects associated with the keys are Planet objects. planet_table.put( "mercury", new Planet( "Mercury", 0.387, 0.241, 58.815, 2433, 0.05, 0 ) ) ; planet_table.put( "venus", new Planet( "Venus", 0.723, 0.615, 224.588, 6053, 0.82, 0 ) ) ; planet_table.put( "earth", new Planet( "Earth", 1.000, 1.000, 1.000, 6379, 1.00, 1 ) ) ; planet_table.put( "mars", new Planet( "Mars", 1.523, 1.881, 1.029, 3386, 0.11, 2 ) ) ; planet_table.put( "jupiter", new Planet( "Jupiter", 5.203, 11.861, 0.411, 71370, 317.93, 12 ) ) ; planet_table.put( "saturn", new Planet( "Saturn", 9.541, 29.457, 0.428, 60369, 95.07, 10 ) ) ; planet_table.put( "uranus", new Planet( "Uranus", 19.190, 84.001, 0.450, 24045, 14.52, 5 ) ) ; planet_table.put( "neptune", new Planet( "Neptune", 30.086, 164.784, 0.657, 22716, 17.18, 2 ) ) ; planet_table.put( "pluto", new Planet( "Pluto", 39.507, 248.35, 6.410, 5700, 0.18, 0 ) ) ; System.out.print( "\n This program can give you information about the" + "\n planets in our solar system. Give a planet name: "); String planet_name_from_user = keyboard.nextLine().toLowerCase() ; // With the method containsKey() we ask if there is a Planet object // connected to the the String object pointed by // planet_name_from_user. if ( planet_table.containsKey( planet_name_from_user ) ) { planet_table.get( planet_name_from_user).print_planet_data() ; } else { System.out.print( "\n Sorry, could not find information on \"" + planet_name_from_user + "\".\n" ) ; } } } /* NOTES: The following pages might be useful to read: http://docs.oracle.com/javase/7/docs/api/java/util/Map.html http://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html http://docs.oracle.com/javase/7/docs/technotes/guides/collections/index.html */