One of the things that computers are best at is math, a task that most humans are happy to pass off to someone else (or something else, in this case). For your first Java program, you will use the computer to determine a depressing fact about the financial condition of the United States. Your program will be called BigDebt. The program figures out how much the national debt increases in an average minute. In order to determine this amount, the computer will be told how much the debt increases in an average day.
The national debt is the amount the United States government has borrowed to compensate for budget deficits over the years. It was approaching $5.25 trillion at last count, which equals $19,700 in indebtedness for each resident of the United States. The following table shows the debt since 1960:
1960:$290.2 billion1965:$320.9 billion1970:$389.2 billion1975:$576.6 billion1980:$930.2 billion1985:$1.946 trillion 1990:$3.365 trillion1995:$4.989 trillionEd Hall maintains a Web page with the current national debt, updated continuously. It's available at the following location:
Beginning the Program
Using 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 book 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: }
Make sure to capitalize everything exactly as shown, and use your Tab key or space bar to insert the blank spaces in front of some lines. When you're done, save the file with the file name BigDebt.java. Figure 2.1 shows the full text of Listing 2.1 entered using the Zeus for Windows word processor and saved as BigDebt.java.
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. <../art/02/02tja01.jpg> Entering BigDebt.java using the Zeus for Windows word- processing program.
The class Statement
The 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 Hour 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 Does
The 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 Marks
In 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 Variable
The 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 books, 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.
The int variable type can store values from -2.1 billion to 2.1 billion in Java programs. There are other variable types for different types of numbers and other types of information.
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 debt
As 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 debt
Insert 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 Product
Your 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.
إرسال تعليق