// PlanetsWithEnum.java (c) 2005 Kari Laitinen // http://www.naturalprogramming.com // 2005-05-26 File created. // 2005-05-27 Last modification. // This program works like Planets.java in javafiles3. // In this version Planet is not a traditional class but an enum type. import java.util.* ; enum Planet { Mercury( 0.387, 0.241, 58.815, 2433, 0.05, 0 ), Venus ( 0.723, 0.615, 224.588, 6053, 0.82, 0 ), Earth ( 1.000, 1.000, 1.000, 6379, 1.00, 1 ), Mars ( 1.523, 1.881, 1.029, 3386, 0.11, 2 ), Jupiter( 5.203, 11.861, 0.411, 71370, 317.93, 12 ), Saturn ( 9.541, 29.457, 0.428, 60369, 95.07, 10 ), Uranus ( 19.190, 84.001, 0.450, 24045, 14.52, 5 ), Neptune( 30.086, 164.784, 0.657, 22716, 17.18, 2 ), Pluto ( 39.507, 248.35, 6.410, 5700, 0.18, 0 ) ; // semicolon!!! 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 ; // The constructor of an enum may not be public. Planet( 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 ) { 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 toString() ; } public void print_planet_data() { // Below the name of the planet is referred to either with // the toString() method or the name(). System.out.print( "\n " + toString() + " orbits the sun in average distance of " + ( mean_distance_from_sun * 149500000 ) + " kilometers." + "\n It takes " + circulation_time_around_sun + " years for " + toString() + " to go around the sun once. \n The radius of " + name() + " is " + equatorial_radius_in_kilometers + " kilometers.\n" ) ; } } class PlanetsWithEnum { // At the end of this file there is an alternative implementation of // the main() method. public static void main( String[] not_in_use ) { Scanner keyboard = new Scanner( System.in ) ; 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() ; // The values() method, which is generated by the compiler, // returns an array containing all the constants of the enum type. Planet[] planet_table = Planet.values() ; int planet_index = 0 ; boolean table_search_ready = false ; while ( table_search_ready == false ) { if ( planet_table[ planet_index ].get_planet_name().toLowerCase() .contains( planet_name_from_user.toLowerCase() ) ) { planet_table[ planet_index ].print_planet_data() ; table_search_ready = true ; } else { planet_index ++ ; if ( planet_index >= planet_table.length ) { System.out.print( "\n Sorry, could not find information on \"" + planet_name_from_user + "\".\n" ) ; table_search_ready = true ; } } } } } /*** // The main() method can be written simply in the following way // if we require that the user of the program gives an accurate // name of a planet. class PlanetsWithEnum { public static void main( String[] not_in_use ) { Scanner keyboard = new Scanner( System.in ) ; System.out.print( "\n This program can give you information about the" + "\n planets in our solar system. " + "\n Give an accurate planet name: "); String planet_name_from_user = keyboard.nextLine() ; // It seems that for every enum type a valueOf() method // is generated automatically. The method returns a reference // to that constant whose name is specified by the string that // is supplied as a parameter. try { Planet planet_of_interest = Planet.valueOf( planet_name_from_user ) ; planet_of_interest.print_planet_data() ; } catch ( Exception caught_exception ) { System.out.print( "\n Sorry, could not find information on \"" + planet_name_from_user + "\".\n" ) ; } } } *****/