How to Run a Canned Java Program - dummies

By Barry Burd

The best way to get to know Java is to “do Java,” by writing, testing, and running your own Java programs. These steps prepare you by describing how to run and test a program. Rather than write your own program, you run a sample program. The program calculates the monthly payments on a home mortgage loan, as shown in the figure.


Here’s how to run the mortgage program:

  1. Install Java and install and configure Eclipse.

  2. Download the sample code from the Java Programming for Android Developers for Dummies webpage.

  3. Launch Eclipse.

    The Workspace Launcher dialog box in Eclipse appears, as shown in the figure.


    A workspace is a folder on the computer’s hard drive. Eclipse stores Java programs in one or more workspace folders. Along with these Java programs, each workspace folder contains some Eclipse settings.

    These settings store information such as the version of Java that you’re using, the colors you prefer for words in the editor, the size of the editor area when you drag the area’s edges, and other preferences. You can have several workspaces with different programs and different settings in each workspace.

    By default, the Workspace Launcher offers to open whatever workspace you opened the last time you ran Eclipse.

  4. In the Workspace Launcher dialog box, click OK.

    The big Eclipse workbench stares at you from the computer screen, as shown in the figure.


    In this figure, the leftmost part of the workbench is the Eclipse Package Explorer, which contains numbers such as 03-01, 04-01, and so on. Each number is the name of an Eclipse project, which is, formally, a collection of files and folders inside a workspace.

    Intuitively, a project is a basic work unit. For example, a self-contained collection of Java program files to manage a CD collection (along with the files containing the data) may constitute a single Eclipse project.

    Eclipse project names can include letters, digits, blank spaces, and other characters.

    When you launch Eclipse, you may see different elements than the ones shown in the figure. You may see the Eclipse Welcome screen with only a few icons in an otherwise barren window. You may also see a workbench like the one shown in the figure, but with no list of numbers (03-01, 04-01, and so on) in the Package Explorer.

    If so, you may have modified the workspace name in the Eclipse Workspace Launcher dialog box. In any case, make sure that you see numbers like 03-01 and 04-01 in the Package Explorer. Seeing these numbers ensures that Eclipse is ready to run the sample programs.

  5. In the Package Explorer, click the 03-Mortgage branch.

    As a result, the 03-Mortgage project appears highlighted.

    To see a sneak preview of the Java program you’re running in Project 03-Mortgage, expand the 03-Mortgage branch in the Package Explorer. Inside the 03-Mortgage branch, you find the src branch, which in turn contains a (default package) branch. Inside the (default package) branch, you find the branch.

    This branch represents the sample Java program. Double-clicking the branch makes the code appear in the Eclipse editor, as shown in the figure.


  6. Choose Run→Run As→Java Application from the main menu, as shown in the figure.


    When you choose Run As→Java Application, the computer runs the project’s code. The program displays the Mortgage Payment Calculator window on the screen, as shown in the figure.


  7. Type numbers into the fields in the Mortgage Payment Calculator window.

    When you type a principal amount in Step 6, don’t include the country’s currency symbol and don’t group the digits. (U.S. residents: Omit dollar signs and commas.) For the percentage rate, omit the % symbol. For the number of years, don’t use a decimal point. If you break any of these rules, the Java code can’t read your number.