Java Program Analysis

The finished version of the BigDebt program.

 

1: class BigDebt {

2:     public static void main (String[] arguments) {

3:         int debt = 59000000;

4:         debt = debt / 1440;

5:         System.out.println("A minute's worth of debt is $" + debt);

6:     }

7: }


When the computer runs this program, it will run each of the statements in the
main statement block on lines 3 through 5. Listing 2.3 shows what the program would look like if it were written in the English language instead of Java.

Listing 2.3. A line-by-line breakdown of the BigDebt program.

 

1: The BigDebt program begins here:

2:     The main part of the program begins here:

3:         Store the value 59000000 in an integer variable called debt

4:         Set debt equal to its current value divided by 1440

5:         Display "A minute's worth of debt is $" and the new value of debt

6:     The main part of the program ends here.

7: The BigDebt program ends here.

Compiling the Program into a Class File

Before you can try out the program, it must be compiled. The term compile might be unfamiliar to you now, but you will become quite familiar with it in the coming hours. When you compile a program, you take the instructions you have given the computer and convert them into a form the computer can better understand. You also make the program run as efficiently as possible. Java programs must be compiled before you can run them. With the Java Developer's Kit, programs are compiled with the javac tool.

To compile the BigDebt program, go to the directory on your system where the BigDebt.java file is located, and type the following command:

javac BigDebt.java

When the program compiles successfully, a new file called BigDebt.class is created in the same directory as BigDebt.java. (If you have any error messages, refer to the following section, "Fixing Errors.") The .class extension was chosen because all Java programs also are called classes. A Java program can be made up of several classes that work together, but in a simple program such as BigDebt only one class is needed.

Do you have a relative, spouse, or other loved one who only says something when things go wrong? (Me neither.) The javac tool only speaks up when there's an error to complain about. If you compile a program successfully without any errors, nothing happens in response.

Fixing Errors

If errors exist in your program when you compile it, the javac tool displays a message explaining each error and the lines they occurred on. Figure 2.2 shows an attempt to compile a program that has errors, and the error messages that are displayed as a result.

Error messages displayed by the javac tool include the following information:

·         The name of the Java program

·         The number of the line where the error was found

·         The type of error

·         The line where the error was found

Figure 2.2. <../art/02/02tja02.jpg> Compiling a version of the BigDebt program that has errors.

As you learned during the past hour, errors in programs are called bugs. Finding those errors and squashing them is called debugging. The following is an example of an error message from Figure 2.2:

BigDebt.java:4: Invalid type expression.

                debt = debt / 1440

In this example, the 4 that follows the file name BigDebt.java indicates that the error is on Line 4. The actual error message, Invalid type expression in this case, can often be con-fusing to new programmers. In some cases, the message can be confusing to any programmer. When the error message doesn't make sense to you, take a look at the line where the error occurred.

For instance, can you determine what's wrong with the following statement?

debt = debt / 1440

The problem is that there's no semi-colon at the end of the statement, which is required in Java programs.

If you get error messages when compiling the BigDebt program, double-check that your program matches Listing 2.2, and correct any differences you find. Make sure that everything is capitalized correctly, and all punctuation (such as {, }, and ;) is included. Often, a close look at the statement included with the error message is enough to reveal the error, or errors, that need to be fixed.

Running the Program

The Java Developer's Kit provides a Java interpreter so that you can try the program you have created. The interpreter makes the computer follow the instructions you gave it when you wrote the program. To see whether the BigDebt program does what you want, go to the directory that contains the BigDebt.class file, and type the following:

java BigDebt

When the program runs, it should state the following:

A minute's worth of debt is $40972

The computer has provided the answer you wanted! With this information, you now know that if you want to donate your salary to slow one minute's growth of the national debt, you need to make more than $40 grand per year. You also have to avoid paying any taxes.

 

Caution: Neither the author nor Sams.net Publishing makes any guarantees express or implied that the Internal Revenue Service will accept the excuse that you spent all your money on the national debt.

 

Workshop: Modifying the Program

The BigDebt program calculated the amount the national debt increases in a minute. If you'd like to make a dent in the debt but can't spare $40,000, you might want to see how much a second's worth of debt would cost you.

Load the file BigDebt.java into your word processor again. You need to change the following statement:

debt = debt / 1440;

This line divided the value in the debt variable by 1,440 because there are 1,440 minutes in a day. Change the line in the BigDebt program so that it divides the debt variable by 86,400, the amount of seconds in a day.

 

Caution: When you change the line of code, don't include a comma in the number (as in 86,400).If you do, you will get an error message when you compile the program. Commas may make it easier for people to read the numbers in your code, but the compiler doesn't appreciate the gesture.

 

You also need to change the following line:

System.out.println("A minute's worth of debt is $" + debt);

Now that you're calculating a second's worth of debt, you need to replace the word minute with second. Make the change and save the file BigDebt.java. Your version of BigDebt.java should match Listing 2.4.

Listing 2.4. The modified version of the BigDebt program.

 

1: class BigDebt {

2:     public static void main (String arguments[]) {

3:         int debt = 59000000;

4:        debt = debt / 86400;

5:         System.out.println("A second's worth of debt is $" + debt);

6:     }

7: }


Compile the file with the same command that you used previously:

javac BigDebt.java

When you run the program, you should get the following output:

A second's worth of debt is $682

Summary

During this hour, you got your first chance to create a Java program. You learned that to create a Java program you need to complete these three basic steps:

1. Write the program with a word processor.

2.
Compile the program.

3.
Tell the interpreter to run the program.

Along the way, you were introduced to some basic computer programming concepts such as compilers, interpreters, blocks, statements, and variables. These things will become more clear to you in successive hours. As long as you got the program to work during this hour, you're ready to proceed.

Post a Comment

أحدث أقدم