Java’s Nimbus Look-and-Feel
1.10 Test-Driving a Java Application
the faster machine-language code executes. Thus Java programs actually go throughtwo compilation phases—one in which source code is translated into bytecodes (for portability across JVMs on different computer platforms) and a second in which, during execution, the bytecodes are translated into machine language for the actual computer on which the program executes.
Problems That May Occur at Execution Time
Programs might not work on the first try. Each of the preceding phases can fail because of various errors that we’ll discuss throughout this book. For example, an executing program might try to divide by zero (an illegal operation for whole-number arithmetic in Java).
This would cause the Java program to display an error message. If this occurred, you’d have to return to the edit phase, make the necessary corrections and proceed through the remaining phases again to determine that the corrections fixed the problem(s). [Note:
Most programs in Java input or output data. When we say that a program displays a mes- sage, we normally mean that it displays that message on your computer’s screen. Messages and other data may be output to other devices, such as disks and hardcopy printers, or even to a network for transmission to other computers.]
1.10 Test-Driving a Java Application 23
nificant parts of the application. To make these features more visible, we’ve changed the background color of theCommand Promptwindows to white and the foreground color to black.] This is a simple text-only version. Later in the book, you’ll learn the techniques to rework this using GUI (graphical user interface) techniques.
1. Checking your setup.Read the Before You Begin section of the book to confirm that you’ve set up Java properly on your computer and that you’ve copied the book’s examples to your hard drive.
2. Locating the completed application.Open aCommand Promptwindow. This can be done by selecting Start > All Programs > Accessories > Command Prompt. Change to the ATM application directory by typingcd C:\examples\ch01\ATM, then pressEnter(Fig. 1.11). The commandcdis used to change directories.
3. Running the ATM application.Type the commandjava ATMCaseStudyand press Enter(Fig. 1.12). Recall that thejavacommand, followed by the name of the ap- plication’s.classfile (in this case,ATMCaseStudy), executes the application. Spec- ifying the .classextension when using thejavacommand results in an error.
[Note:Java commands are case sensitive. It’s important to type the name of this ap- plication with a capital A, T and M in “ATM,” a capital C in “Case” and a capital S in “Study.” Otherwise, the application will not execute.] If you receive the error message, “Exception in thread "main" java.lang.NoClassDefFoundError:
ATMCaseStudy," your system has aCLASSPATHproblem. Please refer to the Before You Begin section of the book for instructions to help you fix this problem.
4. Entering an account number.When the application first executes, it displays a
"Welcome!"greeting and prompts you for an account number. Type12345at the
"Please enter your account number:"prompt (Fig. 1.13) and pressEnter.
Fig. 1.11 | Opening aCommand Promptand changing directories.
Fig. 1.12 | Using thejavacommand to execute the ATM application.
File location of the ATM application Using thecdcommand to
change directories
5. Entering a PIN.Once a valid account number is entered, the application displays the prompt"Enter your PIN:". Type"54321"as your valid PIN (Personal Iden- tification Number) and pressEnter. The ATM main menu containing a list of options will be displayed (Fig. 1.14). We’ll show how you can enter a PIN pri- vately using aJPasswordFieldin Chapter 14.
6. Viewing the account balance. Select option 1, "View my balance", from the ATM menu (Fig. 1.15). The application then displays two numbers—the
Available balance ($1000.00) and theTotal balance($1200.00). The avail- able balance is the maximum amount of money in your account which is available for withdrawal at a given time. In some cases, certain funds, such as recent depos- its, are not immediately available for the user to withdraw, so the available bal- ance may be less than the total balance, as it is here. After the account-balance information is shown, the application’s main menu is displayed again.
7. Withdrawing money from the account.Select option2,"Withdraw cash", from the application menu. You’re then presented (Fig. 1.16) with a list of dollar amounts (e.g.,20,40,60,100and200). You’re also given the option to cancel the transaction and return to the main menu. Withdraw $100 by selecting option4. The application displays "Please take your cash now."and returns to the main menu. [Note:Unfortunately, this application onlysimulatesthe behavior of a real ATM and thus does not actually dispense money.]
Fig. 1.13 | Prompting the user for an account number.
Fig. 1.14 | Entering a valid PIN number and displaying the ATM application’s main menu.
Enter account number prompt ATM welcome message
ATM main menu Enter valid PIN
1.10 Test-Driving a Java Application 25
8. Confirming that the account information has been updated. From the main menu, select option 1again to view your current account balance (Fig. 1.17).
Both the available balance and the total balance have been updated to reflect your withdrawal transaction.
9. Ending the transaction.To end your current ATM session, select option4, "Exit", from the main menu (Fig. 1.18). The ATM will exit the system and display a good- bye message to the user. The application will then return to its original prompt, ask- ing for the next user’s account number.
10. Exiting the ATM and closing the Command Promptwindow. Most applications provide an option to exit and return to theCommand Promptdirectory from which the application was run. A real ATM does not provide a user with the option to turn off the ATM. Rather, when a user has completed all desired transactions and chosen Fig. 1.15 | ATM application displaying user account-balance information.
Fig. 1.16 | Withdrawing money from the account and returning to the main menu.
Account-balance information
ATM withdrawal menu
the menu option to exit, the ATM resets itself and displays a prompt for the next user’s account number. As Fig. 1.18 illustrates, the ATM application here behaves similarly. Choosing the menu option to exit ends only the current user’s ATM ses- sion, not the entire ATM application. To actually exit the ATM application, click the close (x) button in the upper-right corner of theCommand Promptwindow.
Closing the window causes the running application to terminate.