T-Th 9:05 |
CS 1110: Introduction to Computing Using Java Spring 2012 |
|||||||||||||||||||||||||||||||||||
Main
About: Announcements Staff Consultants Times & Places Calendar Materials: Texts DrJava VideoNote Terminology Handouts: Lectures Assignments Labs Assessment: Grading Exams Quizzes Resources: CMS Piazza (link) Piazza (about) AEWs FAQ Java API Style Guide Academic Integrity |
Formatting LocalesThere is a PDF version of these instructions, if you would prefer to have that instead. The goal of this lab is to introduce you to the concept of writing a listener to a GUI application. In addition, you will get some experience with "localization", how to customise an application to different cultures. This matters when you are writing to a larger user base. People in the US sometimes think that they dominate the world, but they are in the minority when it comes to many issues, even formatting numbers and percentages. For example, here is how people in three parts of the world format decimal numbers, currencies, and percentages:
Requirements For This LabThere are two files necesssary for this lab, and they are all available from the online version of these instructions at the course web page. You should create a new directory on your hard drive and download the following five files into this directory: Despite the importance of these files, there is no code to turn in for this lab. Everything that you need to do corresponds to some output for you to write down on this sheet. When you are done, you should show your instructor the contents of the sheet. If you do not finish the lab, then you need to finish it by the beginning of lab next week and show it to your instructor at that time. Starting the Application
As we have discussed several times in class, Adding a Listener
Currently, the application does nothing when you press the "Ready!" button; that is
because it does not have a listener. The class
Once you have done this, recompile and hit the "Ready!" button. The number will be
printed in three ways in the String fields -- number format, currency format, and percentage
format -- all for the chosen locales. Write what you see below.
Testing the Locales
Take a look at the method
Execute the program and write down the number 5000.365 in the formats for four different locales.
First, English (U.S.), then English (United Kingdom), then Chinese (Taiwan), then one of your
choice. Be sure to hit the ready button after selecting a locale. You may get a question-mark
instead of a currency symbol for some locales; that is because your computer software may not
know how to display that particular symbol.
Understanding Locales
A locale consists (essentially) of a language and a country. Class
To display the default locale of your computer in
Note that method Now when you run the program, the displayable form of the default locale for your computer is placed in String field 3, showing you the language and country, each in two letters. For example, if the default locale for your computer was French (Canada), field 3 would contain: Default locale: fr_CANow add code to print the displayable form of the locale that you selected in the GUI by adding the following statement to method actionPerformed , just before the
return statement:
Note that method
Once you have done this, write the display form of three different locales below. Show what was
in the locale pulldown box as well as the language and country codes.
Decimal Number Format
Now suppose you want to format some decimal numbers using the format of
You then call a method to format the number that is in
When done, you can put the result in String field 5:
Place these three statements just before the return statement in method
Place the following numbers in
Currency Format and Percent FormatMo< Above, you usedNumberFormat.getInstance(getLocale()); to get
an instance of a class that would help you format numbers in the decimal format
of the selected locale. To get an instance of a class to format numbers in a
currency, use the statement:
Similary, for a percentage, use
Try them and repeat the exercise from the previous step.
Getting the Available Locales
Not all possible locales are implemented by Java, but over 100 are. Take a look at the declaration
of instance variable
It might be useful to have a listing of what the decimal, currency, or percent format of a
particular number would look like for all locales at once. Method
The output will appear in the Java console, because of the use of the
|