Web hosting india - Chapter 5 Control Structures: Part 2 209 the

Chapter 5 Control Structures: Part 2 209 the currency values should be displayed starting with a dollar sign ($), use a decimal point to separate dollars and cents and use a comma to delineate thousands (e.g., $1,234.56). Class Locale provides constants that can be used to customize this program to represent currency values for other countries, so that currency formats are displayed properly for each locale (i.e., each country s local-currency format). Class NumberFormat (imported at line 5) is located in package java.text, and class Locale (imported at line 6) is located in package java.util. Line 25 declares JTextArea reference outputTextArea and initializes it with a new object of class JTextArea (from package javax.swing). A JTextArea is a GUI component that can display many lines of text. The message dialog that displays the JTextArea determines the width and height of the JTextArea, based on the String it contains. We introduce this GUI component now because we will see many examples throughout the text in which the program outputs contain too many lines to display on the screen. This GUI component allows us to scroll through the lines of text so we can see all the program output. The methods for placing text in a JTextArea include setText and append. Line 28 uses JTextArea method setText to place a Stringin the JTextArea to which outputTextArea refers. Initially, a JTextArea contains an empty String (i.e., a String with no characters in it). The preceding statement replaces the empty String with one containing the column heads for our two columns of output Year and AmountonDeposit. The column heads are separated with a tab character (escape sequence t). Also, the string contains the newline character (escape sequence n), indicating that any additional text appended to the JTextAreashould begin on the next line. The for structure (lines 31 40) executes its body 10 times, varying control variable yearfrom 1 to 10 in increments of 1. (Note that year represents n in the statement of the problem.) Java does not include an exponentiation operator. Instead, we use static method pow of class Math for this purpose. Math.pow(x,y) calculates the value of x raised to the yth power. Method pow takes two arguments of type doubleand returns a double value. Line 34 performs the calculation from the statement of the problem, a = p (1 + r) n where a is amount, p is principal, r is rate and n is year. Lines 37 38 append more text to the end of the outputTextArea. The text includes the current value of year, a tab character (to position to the second column), the result of the method call moneyFormat.format( amount ) which formats the amount as U. S. currency and a newline character (to position the cursor in the JTextArea at the beginning of the next line). Lines 43 44 display the results in a message dialog. Until now, the message displayed has always been a String. In this example, the second argument is outputText- Area a GUI component. An interesting feature of class JOptionPane is that the message it displays with showMessageDialogcan be a Stringor a GUI component, such as a JTextArea. In this example, the message dialog sizes itself to accommodate the JTextArea. We use this technique several times early in this chapter to display large text- based outputs. Later in this chapter, we demonstrate how to add a scrolling capability to the JTextArea, so the user can view a program s output that is too large to display in full on the screen. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/2/01

Leave a Reply