Ed Hall maintains a Web page with the current national debt, updated continuously. It's available at the following location: http://www.brillig.com/debt_clock/ Beginning the ProgramUsing your word processor, begin your Java programming career by entering each line from Listing 2.1. Don't enter the line number and colon at the beginning of each line--these are used in this guide so that specific line numbers can be referred to. Listing 2.1. The BigDebt program.
1: class BigDebt {
2: public static void main (String[] arguments) {
3: // My first Java program goes here
4: }
5: }
You have created the bare-bones form of a Java program. You will create several programs that start off exactly like this one, except for the word BigDebt on Line 1. This word represents the name of your program and changes with each program that you write. Line 3 also should make sense--it's a sentence in actual English. The rest is completely new, however, and each part is introduced in the following sections. Figure 2.1. Entering BigDebt.java using the Zeus for Windows word- processing program. The class StatementThe first line of the program is the following: class BigDebt {
Translated into English, this line means, "Computer, give my Java program the name BigDebt." As you might recall from Chapter 1, each instruction that you give a computer is called a statement. The class statement is the way you give your computer program a name. It also is used to determine other things about the program, as you will see later. The significance of the term class is that Java programs also are called classes. In this example, the program name BigDebt matches the file name you gave your document, BigDebt.java. Java programs must have a name that matches the first part of their file names, and these names always must be capitalized in the same way. If the name doesn't match, you will get an error when you try to compile the program. What the main Statement DoesThe next line of the program is the following: public static void main (String[] arguments) {
This line tells the computer, "The main part of the program begins here." Java programs are organized into different sections, so there needs to be a way to identify the part of a program that will be handled first. All of the programs that you will write during the next several hours use main as the starting point. Those Squiggly Bracket MarksIn the BigDebt.java program, every line except Line 3 contains a squiggly bracket of some kind--either an { or an }. These brackets are a way to group parts of your program (in the same way that parentheses are used in this sentence to group words). Everything between the opening bracket, {, and the closing bracket, }, is part of the same group. These groupings are called blocks. In Listing 2.1, the opening bracket on Line 1 is associated with the closing bracket on Line 5, which makes your entire program a block. You will always use brackets in this way to show the beginning and end of your programs. Blocks can be located inside other blocks (just as parentheses are used here (and a second set is used here)). The BigDebt.java program has brackets on Line 2 and Line 4 that establish another block. This block begins with the main statement. Everything inside the main statement block is a command for the computer to handle when the program is run. The following statement is the only thing located inside the block: // My first Java program goes here This line is a placeholder. The // at the beginning of the line tells the computer to ignore this line--it is put in the program solely for the benefit of humans who are looking at the program's text. Lines that serve this purpose are called comments. Right now, you have written a complete Java program. It can be compiled, but if you run it, nothing will happen. The reason for this is that you have not told the computer to do any-thing yet. The main statement block contains only a line of comments, which is ignored. If the BigDebt.java program is going to provide sobering details about the United States Treasury, you will have to add some commands inside the opening and closing brackets of the main statement block. Storing Information in the debt VariableThe national debt is increasing at a present rate of $59 million per day. To put this number into perspective, overpaid sports athletes could donate their salaries to the United States Treasury and barely make a dent in it. Chicago White Sox slugger Albert Belle's five-year, $50 million deal stops the debt from increasing for about 20 hours. Aside from the publishers of computer guides, most of us don't make the same kind of money as pro athletes. If we want to slow down the growing debt ourselves, a place to start is by breaking it down into minutes. Your Java program will figure this out for you. The first step is to tell the computer what you were just told: The national debt goes up $59 million per day. Load the BigDebt.java file into your word processor if it's not still loaded, and replace Line 3 with the following: int debt = 59000000; This statement tells the computer to store the value 59,000,000 into a variable called debt. Variables are special storage places where a computer program can store information. The value of variables can be changed. Variables can be used to hold several different types of information, such as integers, floating-point numbers, lines of text, and characters of text. In a Java program, you must tell the computer what type of information a variable will hold. In this program, debt is an integer. Putting int in the statement int debt = 59000000; sets up the variable to hold integer values.
When you entered this statement into the computer program, a semi-colon should have been included at the end of the line. Semi-colons are used at the end of each command in your Java programs. They're like periods at the end of a sentence; the computer uses them to determine when one command ends and the next command begins. Changing the Information Stored in debtAs it stands, the program you have written does one thing: It uses the debt variable to hold the value 59,000,000--a day's worth of growing debt. However, you want to determine the amount of debt per minute, not per day. To determine this amount, you need to tell the computer to change the value that has been stored in the debt variable. There are 1,440 minutes in each day, so tell the computer to divide the value in debt by 1,440. Insert a blank line after the int debt = 59000000; statement. In the blank line, enter the following: debt = debt / 1440; If you haven't been able to suppress all memories of new math, this statement probably looks like an algebra problem to you. It gives the computer the following assignment: "Set the debt variable equal to its current value divided by 1,440." You now have a program that does what you wanted it to do. It determines the amount the national debt grows in an average minute. However, if you ran the program at this point, it wouldn't display anything. The two commands you have given the computer in the BigDebt program occur behind the scenes. To show the computer's result, you have to display the contents of the debt variable. Displaying the Contents of debtInsert another blank line in the BigDebt program after the debt = debt / 1440; statement. Use that space to enter the following statement: System.out.println("A minute's worth of debt is $" + debt);
This statement tells the computer to display the text A minute's worth of debt is $ followed by the value stored in the debt variable. The System.out.println command means "display a line on the system output device." In this case, the system output device is your computer monitor. Everything within the parentheses is displayed. Saving the Finished ProductYour program should now resemble Listing 2.2. Make any corrections that are needed and save the file as BigDebt.java. Keep in mind that all Java programs are created as text files and are saved with the .java file extension. Listing 2.2. 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: }
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 FileBefore 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.
Fixing ErrorsIf 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:
Figure 2.2. 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 ProgramThe 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.
Workshop: Modifying the ProgramThe 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.
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: }
javac BigDebt.java When you run the program, you should get the following output: A second's worth of debt is $682 SummaryDuring 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:
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. Q&A
debt = debt / 86400; // divide debt by the number of seconds
javac BigDebt
javac BigDebt.java
QuizTest your knowledge of the material covered in this chapter by answering the following questions. Questions
Answers
ActivitiesIf you'd like to explore the topics covered in this hour a little more fully, try the following activities:
|
|||||||||||||||||||
| © 2004 Soft Lookup Corp. Privacy Statement | |||||||||||||||||||