Test-Driving a Java Application

In this section, you will run and interact with your first Java application. You will begin by running an ATM application that simulates the transactions that take place when using an ATM machine (e.g., withdrawing money, making deposits and checking account balances). You will learn how to build this application in the optional, object-oriented case study included in Chapters 18 and 10. Figure 1.10 at the end of this section suggests many additional interesting applications that you may also want to test-drive after completing the ATM test-drive.

In the following steps, you will run the application and perform various transactions. The elements and functionality you see in this application are typical of what you will learn to program in this book. [Note: We use fonts to distinguish between features you see on a screen (e.g., the Command Prompt) and elements that are not directly related to a screen. Our convention is to emphasize screen features like titles and menus (e.g., the File menu) in a semibold sans-serif Helvetica font and to emphasize non-screen elements, such as file names or input (e.g., ProgramName.java) in a sans-serif Lucida font. As you have already noticed, the defining occurrence of each term is set in blue, heavy bold. In the figures in this section, we highlight in yellow the user input required by each step and point out significant parts of the application with lines and text. To make these features more visible, we have modified the background color of the Command Prompt windows.]

1.

Checking your setup. Read the For Students and Instructors: Important Information Before You Begin section to confirm that you have set up Java properly on your computer and that you have copied the book's examples to your hard drive.

 

2.

Locating the completed application. Open a Command Prompt window. For readers using Windows 95, 98 or 2000, this can be done by selecting Start > Programs > Accessories > Command Prompt. For Windows XP users, select Start > All Programs > Accessories > Command Prompt. Change to your completed ATM application directory by typing cd C:examplesch01ATM and then pressing Enter (Fig. 1.2). The command cd is used to change directories.

 

Figure 1.2. Opening a Windows XP Command Prompt and changing directories.

 

3.

Running the ATM application. Now that you are in the directory that contains the ATM application, type the command java ATMCaseStudy (Fig. 1.3) and press Enter. Remember from the preceding section that the java command, followed by the name of the application's .class file (in this case, ATMCaseStudy), executes the application. It is not necessary to specify the .class extension when using the java command. [Note: Java commands are case sensitive. It is important to type the name of this application 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.]

 

Figure 1.3. Using the java command to execute the ATM application.

 
   

4.

Entering an account number. When the application first executes, it displays a "Welcome!" greeting and prompts you for an account number. Type 12345 at the "Please enter your account number:" prompt (Fig. 1.4) and press Enter.

 

 

Figure 1.4. Prompting the user for an account number.

 

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 Identification Number) and press Enter. The ATM main menu containing a list of options will be displayed (Fig. 1.5).

 

Figure 1.5. Entering a valid PIN number and displaying the ATM application's main menu.

 

6.

Viewing the account balance. Select option 1, "View my balance", from the ATM menu. The application then displays two numbersthe Available balance ($1000.00) and the Total balance ($1,200.00). The available 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 deposits, are not immediately available for the user to withdraw, so the available balance 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 (Fig. 1.6).

 

Figure 1.6. ATM application displaying user account balance information.

 
 

7.

Withdrawing money from the account. Select option 2, "Withdraw cash", from the application menu. You are then presented (Fig. 1.7) with a list of dollar amounts (e.g., 20, 40, 60, 100 and 200). You are also given the option to cancel the transaction and return to the main menu. Withdraw $100 by selecting option 4. The application displays "Please take your cash now." and returns to the main menu. [Note: Unfortunately, this application only simulates the behavior of a real ATM and thus does not actually dispense money.]

 

Figure 1.7. Withdrawing money from the account and returning to the main menu.

 

8.

Confirming that the account information has been updated. From the main menu, select option 1 again to view your current account balance. Note that both the available balance and the total balance have been updated to reflect your withdrawal transaction (Fig. 1.8).

 

Figure 1.8. Checking new balance.

 
   

9.

Ending the transaction. To end your current ATM session, select option 4, "Exit" from the main menu. The ATM will exit the system and display a goodbye message to the user. The application will then return to its original prompt asking for the next user's account number (Fig. 1.9).

 

Figure 1.9. Ending an ATM transaction session.

(This item is displayed on page 20 in the print version)

 
 

10.

Exiting the ATM application and closing the Command Prompt window. Most applications provide an option to exit and return to the Command Prompt directory from which the application was run. A real ATM does not provide a user with the option to turn off the ATM machine. Rather, when a user has completed all desired transactions and chooses the menu option to exit, the ATM resets itself and displays a prompt for the next user's account number. As Fig. 1.9 illustrates, the ATM application here behaves similarly. Choosing the menu option to exit ends only the current user's ATM session, not the entire ATM application. To actually exit the ATM application, click the close (x) button in the upper-right corner of the Command Prompt window. Closing the window causes the running application to terminate.

 

Additional Applications Found in Java How to Program, 6/e

We encourage you to practice running some of the Java applications featured in this textbook. Figure 1.10 lists a few of the hundreds of applications found in the examples and exercises in this text. Many of these programs simulate real-world applications and introduce the powerful and fun features of Java programming. Please feel free to run any or all of the programs listed to see some of the different types of applications you will learn how to build as you study the programming concepts in this textbook. The examples folder for Chapter 1 contains all the files required to run each application. You can do so by typing the commands listed in Fig. 1.10 in a Command Prompt window.

Figure 1.10. Examples of additional Java applications found in Java How to Program, 6/e.

Application Name

Chapter Location

Commands to Run

Tic-Tac-Toe

Chapters 8 and 24

cd C:examplesch01Tic-Tac-Toe java TicTacToeTest

Guessing Game

Chapter 11

cd C:examplesch01GuessGame java GuessGame

Logo Animator

Chapter 21

cd C:examplesch01LogoAnimator java LogoAnimator

Bouncing Ball

Chapter 23

cd C:examplesch01BouncingBall java BouncingBall

Категории