• Tidak ada hasil yang ditemukan

WHO DOES WHAT, AND HOW?

Dalam dokumen Beginning Programming with Java (Halaman 194-200)

To go from Listing 6-1 to Listing 6-2 , I added an import declaration and some stuff about new Scanner(System.in) . You can find out more about these things by reading the “Getting numbers, words, and other things ” section in Chapter 5 . (You can find out even more about input and output by visiting Chapter 13 .) And more examples (more

keyboard.next Something methods) are in Chapters 7 and 8 .

Methods and assignments

Note how I use keyboard.nextDouble in Listing 6-2 . The call to

method keyboard.nextDouble is part of an assignment statement. If you look in Chapter 5 at the section on how the EchoLine program works, you see that the computer can substitute something in place of a method call. The computer does this in Listing 6-2 . When you type 5.75 on the keyboard, the computer turns

amount = keyboard.nextDouble();

into

amount = 5.75;

(The computer doesn’t really rewrite the code in Listing 6-2 . This

amount = 5.75 line simply illustrates the effect of the computer’s

action.) In the second assignment statement in Listing 6-2 , the computer adds 25.00 to the 5.75 that’s stored in amount .

Some method calls have this substitution effect, and others (like

System.out.println ) don’t. To find out more about this topic, see Chapter 19 .

program runs, how does the user know to type something on the keyboard? If the user and the programmer are the same person, and the program is fairly simple, knowing what to type is no big deal. For example, when you start running the code in Listing 5-1 , you have this book in front of you, and the book says “The computer is waiting for you to type something… . You type one line of text… .” So you type the text and press Enter. Everything is fine.

But very few programs come with their own books. In many instances, when a program starts running, the user has to stare at the screen to figure out what to do next. The code in Listing 6-2 works in this stare-at-the-screen scenario. In Listing 6-2 , the first call to print puts an

informative message ( What’s the price of a flash drive? ) on the user’s screen. A message of this kind is called a prompt .

When you start writing programs, you can easily confuse the roles of the prompt and the user’s input. Remember: No preordained relationship exists between a prompt and the subsequent input. To create a prompt, you call print or println . Then, to read the user’s input, you call

nextLine , nextDouble , or one of the Scanner class’s other nextSomething methods. These

print and next calls belong in two separate statements. Java has no commonly used, single statement that does both the prompting and the “ next -ing.”

As the programmer, your job is to combine the prompting and the next -ing. You can combine prompting and next-ing in all kinds of ways. Some ways are helpful to the user, and some ways aren’t, as described in this list:

If you don’t have a call to print or println , the user sees no prompt. A blinking cursor sits quietly and waits for the user to type something. The user has to guess what kind of input to type.

Occasionally that’s okay, but usually it isn’t.

If you call print or println but you don’t call a keyboard.next Something method, the computer doesn’t wait for the user to type anything. The program races to execute whatever statement comes immediately after the print or println .

If your prompt displays a misleading message, you misled the user. Java has no built-in feature that checks the appropriateness of a prompt. That’s not surprising. Most computer languages have no prompt-checking feature.

Be careful with your prompts. Be nice to your user. Remember that you were once a humble computer user, too.

Variations on a Theme

In Listing 6-1 , it takes two lines to give the amount variable its first value:

double amount;

amount = 5.95;

You can do the same thing with just one line:

double amount = 5.95;

When you do this, you don’t say that you’re “assigning” a value to the

amount variable. The line double amount=5.95 isn’t called an

“assignment statement.” Instead, this line is called a declaration with an initialization. You’re initializing the amount variable. You can do all sorts of things with initializations, even arithmetic:

double gasBill = 174.59;

double elecBill = 84.21;

double H2OBill = 22.88;

double total = gasBill + elecBill + H2OBill;

Moving variables from place to place

It helps to remember the difference between initializations and assignments. For one thing, you can drag a declaration with its initialization outside of a method:

//This is okay:

class SnitSoft {

static double amount = 5.95;

public static void main(String args[]) {

amount = amount + 25.00;

System.out.print("We will bill $");

System.out.print(amount);

System.out.println(" to your credit card.");

}

}

You can’t do the same thing with assignment statements (see the following code and Figure 6-8 ):

//This does not compile:

class BadSnitSoftCode { static double amount;

amount = 5.95; //Misplaced statement

public static void main(String args[]) {

amount = amount + 25.00;

System.out.print("We will bill $");

System.out.print(amount);

System.out.println(" to your credit card.");

}

}

FIGURE 6-8: A failed attempt to compile BadSnitSoftCode .

You can’t drag statements outside of methods. (Even though a variable declaration ends with a semicolon, a variable declaration isn’t

considered to be a statement. Go figure!)

The advantage of putting a declaration outside of a method is illustrated in Chapter 17 . While you wait impatiently to reach that chapter, notice how I added the word static to each declaration that I pulled out of the

main method. I had to do this because the main method’s header has the word static in it. Not all methods are static. In fact, most methods aren’t static. But whenever you pull a declaration out of a static

method, you have to add the word static at the beginning of the

declaration. All the mystery surrounding the word static is resolved in Chapter 18 .

Combining variable declarations

The code in Listing 6-1 has only one variable (as if variables are in short supply). You can get the same effect with several variables:

class SnitSoftNew {

public static void main(String args[]) { double flashDrivePrice;

double shippingAndHandling;

double total;

flashDrivePrice = 5.95;

shippingAndHandling = 25.00;

total

= flashDrivePrice + shippingAndHandling;

System.out.print("We will bill $");

System.out.print(total);

System.out.println(" to your credit card.");

}

}

This new code gives you the same output as the code in Listing 6-1 . (Refer to Figure 6-1 .)

Dalam dokumen Beginning Programming with Java (Halaman 194-200)