[ Team LiB ] Previous Section Next Section

8.7 Exercises

Exercise 8-1. Several internationalization-related classes, such as NumberFormat and DateFormat, have static methods named getAvailableLocales( ) that return an array of the Locale objects they support. You can look up the name of the country of a given Locale object with the getDisplayCountry( ) method. Note that this method has two variants. One takes no arguments and displays the country name as appropriate in the default locale. The other version of getDisplayCountry( ) expects a Locale argument and displays the country name in the language of the specified locale.

Write a program that displays the country names for all locales returned by NumberFormat.getAvailableLocales( ). Using the static locale constants defined by the Locale class, display each country name in English, French, German, and Italian.

Exercise 8-2. Modify the Portfolio class of Example 8-3 to remove all hardcoded display strings. Instead, use the ResourceBundle and MessageFormat classes as demonstrated in Examples Example 8-4 and Example 8-5.

Exercise 8-3. Write a multicity digital clock program that displays the current date and time in the cities Washington, London, Paris, Bonn, Beijing, and Tokyo. Display the dates and times using the customary formats for those cities. You'll want to read about the java.util.TimeZone class and the DateFormat.setTimeZone( ) method. Consult a map or search the Internet to determine the time zones for each of the cities. Write the program as an AWT or Swing application or as an applet after you have read Chapters Chapter 11 and Chapter 16. You may want to base the program on the Clock applet from Example 16-2.

Exercise 8-4. Example 8-4 shows how you can use a ResourceBundle to internationalize the text that appears within menus in your application. One feature of Swing that discourages internationalization is that the JButton, JMenu, and JMenuItem constructors, among others, are passed the labels they are to display. This makes it very tempting for programmers to hardcode these labels into their programs. Create internationalized subclasses of these components, named IButton, IMenu, and IMenuItem, that instead take resource names as their constructor arguments. Each class should look up a resource bundle named "Labels" and use this bundle to look up the button or menu label that corresponds to the resource name passed to the constructor. If the bundle does not exist, or if a given resource is not defined in it, the IButton, IMenu, and IMenuItem classes should default to using the resource names as their labels. Write a simple test program (and some test property files) that demonstrate these new classes under two or three different locales. You'll probably want to read Chapter 11 before working on this exercise.

    [ Team LiB ] Previous Section Next Section