1. Trang chủ
  2. » Công Nghệ Thông Tin

Beginning Programming with Java for Dummies 2nd phần 4 potx

41 339 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Nội dung

You should notice a couple of things about Listing 7-2. First, you can read an int value with the nextInt method. Second, you can issue successive calls to Scanner methods. In Listing 7-2, I call nextInt twice. All I have to do is separate the numbers I type by blank spaces. In Figure 7-2, I put one blank space between my 80 and my 6, but more blank spaces would work as well. This blank space rule applies to many of the Scanner methods. For example, here’s some code that reads three numeric values: gumballs = myScanner.nextInt(); costOfGumballs = myScanner.nextDouble(); kids = myScanner.nextInt(); Figure 7-3 shows valid input for these three method calls. What you read is what you get When you’re writing your own code, you should never take anything for granted. Suppose you accidentally reverse the order of the gumballs and kids assignment statements in Listing 7-2: Figure 7-3: Three numbers for three Scanner method calls. Figure 7-2: Next thing you know, I’ll have seventy kids and a thousand gumballs. 106 Part II: Writing Your Own Java Programs 12_588745 ch07.qxd 3/16/05 9:19 PM Page 106 //This code is misleading: System.out.print(“How many gumballs? How many kids? “); kids = myScanner.nextInt(); gumballs = myScanner.nextInt(); Then, the line How many gumballs? How many kids? is very misleading. Because the kids assignment statement comes before the gumballs assign- ment statement, the first number you type becomes the value of kids, and the second number you type becomes the value of gumballs. It doesn’t matter that your program displays the message How many gumballs? How many kids? . What matters is the order of the assignment statements in the program. If the kids assignment statement accidentally comes first, you can get a strange answer, like the zero answer in Figure 7-4. That’s how int division works. It just cuts off any remainder. Divide a small number (like 6) by a big number (like 80), and you get 0. Figure 7-4: How to make six kids very unhappy. 107 Chapter 7: Numbers and Types 12_588745 ch07.qxd 3/16/05 9:19 PM Page 107 Creating New Values by Applying Operators What could be more comforting than your old friend, the plus sign? It was the first thing you learned about in elementary school math. Almost everybody knows how to add two and two. In fact, in English usage, adding two and two is a metaphor for something that’s easy to do. Whenever you see a plus sign, one of your brain cells says, “Thank goodness, it could be something much more complicated.” So Java has a plus sign. You can use the plus sign to add two numbers: int apples, oranges, fruit; apples = 5; oranges = 16; fruit = apples + oranges; Of course, the old minus sign is available too: apples = fruit - oranges; Use an asterisk for multiplication, and a forward slash for division: double rate, pay, withholding; int hours; rate = 6.25; hours = 35; pay = rate * hours; withholding = pay / 3.0; When you divide an int value by another int value, you get an int value. The computer doesn’t round. Instead, the computer chops off any remainder. If you put System.out.println(11 / 4) in your program, the computer prints 2, not 2.75. If you need a decimal answer, make either (or both) of the numbers you’re dividing double values. For example, if you put System.out. println(11.0 / 4) in your program, the computer divides a double value, 11.0, by an int value, 4. Because at least one of the two values is double, the computer prints 2.75. Finding a remainder There’s a useful arithmetic operator called the remainder operator. The symbol for the remainder operator is the percent sign ( %). When you put System. out.println(11 % 4) in your program, the computer prints 3. It does this because 4 goes into 11 who-cares-how-many times, with a remainder of 3. 108 Part II: Writing Your Own Java Programs 12_588745 ch07.qxd 3/16/05 9:19 PM Page 108 The remainder operator turns out to be fairly useful. After all, a remainder is the amount you have left over after you divide two numbers. What if you’re making change for $1.38? After dividing 138 by 25, you have 13 cents left over, as shown in Figure 7-5. The code in Listing 7-3 makes use of this remainder idea. Listing 7-3: Making Change import java.util.Scanner; class MakeChange { public static void main(String args[]) { Scanner myScanner = new Scanner(System.in); int quarters, dimes, nickels, cents; int whatsLeft, total; System.out.print(“How many cents do you have? “); total = myScanner.nextInt(); quarters = total / 25; whatsLeft = total % 25; (continued) 138 cents 138/25 is 5 138%25 is 13 Figure 7-5: Hey, bud! Got change for 138 sticks? 109 Chapter 7: Numbers and Types 12_588745 ch07.qxd 3/16/05 9:19 PM Page 109 Listing 7-3 (continued) dimes = whatsLeft / 10; whatsLeft = whatsLeft % 10; nickels = whatsLeft / 5; whatsLeft = whatsLeft % 5; cents = whatsLeft; System.out.println(); System.out.println(“From “ + total + “ cents you get”); System.out.println(quarters + “ quarters”); System.out.println(dimes + “ dimes”); System.out.println(nickels + “ nickels”); System.out.println(cents + “ cents”); } } A run of the code in Listing 7-3 is shown in Figure 7-6. You start with a total of 138 cents. The statement quarters = total / 25; divides 138 by 25, giving 5. That means you can make 5 quarters from 138 cents. Next, the statement whatsLeft = total % 25; divides 138 by 25 again, and puts only the remainder, 13, into whatsLeft. Now you’re ready for the next step, which is to take as many dimes as you can out of 13 cents. You keep going like this until you’ve divided away all the nickels. At that point, the value of whatsLeft is just 3 (meaning 3 cents). When two or more variables have similar types, you can create the variables with combined declarations. For example, Listing 7-3 has two combined declarations — one for the variables quarters, dimes, nickels, and cents (all of type int); another for the variables whatsLeft and total (both of Figure 7-6: Change for $1.38. 110 Part II: Writing Your Own Java Programs 12_588745 ch07.qxd 3/16/05 9:19 PM Page 110 type int). But to create variables of different types, you need separate decla- rations. For example, to create an int variable named total and a double variable named amount, you need one declaration int total; and another declaration double amount;. Listing 7-3 has a call to System.out.println() with nothing in the paren- theses. When the computer executes this statement, the cursor jumps to a new line on the screen. (I often use this statement to put a blank line in a program’s output.) The increment and decrement operators Java has some neat little operators that make life easier (for the computer’s processor, for your brain, and for your fingers). Altogether there are four such operators — two increment operators and two decrement operators. The increment operators add one, and the decrement operators subtract one. To see how they work, you need some examples. Using preincrement The first example is in Figure 7-7. Figure 7-7: Using pre- increment. 111 Chapter 7: Numbers and Types 12_588745 ch07.qxd 3/16/05 9:19 PM Page 111 A run of the program in Figure 7-7 is shown in Figure 7-8. In this horribly uneventful run, the count of gumballs gets displayed three times. 112 Part II: Writing Your Own Java Programs If thine int offends thee, cast it out The run in Figure 7-6 seems artificial. Why would you start with 138 cents? Why not use the more familiar $1.38? The reason is that the number 1.38 isn’t a whole number, and without whole numbers, the remainder operator isn’t very useful. For example, the value of 1.38 % 0.25 is 0.1299999999999999. All those nines are tough to work with. So if you want to input 1.38, then the program should take your 1.38 and turn it into 138 cents. The question is, how can you get your program do this? My first idea is to multiply 1.38 by 100: double amount; int total; System.out.print(“How much money do you have? “); amount = myScanner.nextDouble(); total = amount * 100; //This doesn’t quite work. In everyday arithmetic, multiplying by 100 does the trick. But computers are fussy. With a com- puter, you have to be very careful when you mix int values and double values. (See the first figure in this sidebar.) 12_588745 ch07.qxd 3/16/05 9:19 PM Page 112 The double plus sign goes under two different names, depending on where you put it. When you put the ++ before a variable, the ++ is called the prein- crement operator. In the word preincrement, the pre stands for before. In this setting, the word before has two different meanings: ߜ You’re putting ++ before the variable. ߜ The computer adds 1 to the variable’s value before the variable gets used in any other part of the statement. Figure 7-9 has a slow-motion instant replay of the preincrement operator’s action. In Figure 7-9, the computer encounters the System.out.println (++gumballs) statement. First, the computer adds 1 to gumballs (raising the value of gumballs to 29). Then the computer executes System.out. println , using the new value of gumballs (29). Figure 7-8: A run of the preincre- ment code (the code in Figure 7-7). 113 Chapter 7: Numbers and Types To cram a double value into an int variable, you need something called casting. When you cast a value, you essentially say. “I’m aware that I’m trying to squish a double value into an int variable. It’s a tight fit, but I want to do it anyway.” To do casting, you put the name of a type in parentheses, as follows: total = (int) (amount * 100); //This works! This casting notation turns the double value 138.00 into the int value 138, and everybody’s happy. (See the second figure in this sidebar.) 12_588745 ch07.qxd 3/16/05 9:19 PM Page 113 114 Part II: Writing Your Own Java Programs With System.out.println(++gumballs), the computer adds 1 to gum- balls before printing the new value of gumballs on the screen. Using postincrement An alternative to preincrement is postincrement. With postincrement, the post stands for after. The word after has two different meanings: ߜ You put ++ after the variable. ߜ The computer adds 1 to the variable’s value after the variable gets used in any other part of the statement. Figure 7-10 has a close-up view of the postincrement operator’s action. In Fig- ure 7-10, the computer encounters the System.out.println(gumballs++) statement. First, the computer executes System.out.println, using the old value of gumballs (28). Then the computer adds 1 to gumballs (raising the value of gumballs to 29). Figure 7-10: The postin- crement operator in action. Figure 7-9: The prein- crement operator in action. 12_588745 ch07.qxd 3/16/05 9:19 PM Page 114 Look at the bold line of code in Figure 7-11. The computer prints the old value of gumballs (28) on the screen. Only after printing this old value does the computer add 1 to gumballs (raising the gumballs value from 28 to 29). 115 Chapter 7: Numbers and Types Statements and expressions Any part of a computer program that has a value is called an expression. If you write gumballs = 30; then 30 is an expression (an expression whose value is the quantity 30). If you write amount = 5.95 + 25.00; then 5.95 + 25.00 is an expression (because 5.95 + 25.00 has the value 30.95). If you write gumballsPerKid = gumballs / kids; then gumballs / kids is an expression. (The value of the expression gumballs / kids depends on whatever values the variables gumballs and kids have when the statement with the expression in it is executed.) This brings us to the subject of the pre- and postincrement and decrement operators. There are two ways to think about these operators: the way everyone understands it, and the right way. The way I explain it in most of this section (in terms of time, with before and after) is the way everyone understands the concept. Unfortunately, the way everyone understands the concept isn’t really the right way. When you see ++ or , you can think in terms of time sequence. But occasionally some programmer uses ++ or in a convoluted way, and the notions of before and after break down. So if you’re ever in a tight spot, you should think about these operators in terms of statements and expressions. First, remember that a statement tells the com- puter to do something, and an expression has a value. (Statements are described in Chapter 4, and expressions are described earlier in this sidebar.) Which category does gumballs++ belong to? The surprising answer is both. The Java code gumballs++ is both a statement and an expression. Suppose that, before executing the code System.out.println(gumballs++), the value of gumballs is 28: ߜ As a statement, gumballs++ tells the com- puter to add 1 to gumballs. ߜ As an expression, the value of gumballs++ is 28, not 29. So even though gumballs gets 1 added to it, the code System.out.println(gumballs++) really means System.out.println(28). (See the figure in this sidebar.) Now, almost everything you just read about gumballs++ is true about ++gumballs. The only difference is, as an expression, ++gum- balls behaves in a more intuitive way. Suppose that, before executing the code System.out. println(++gumballs) , the value of gum- balls is 28: ߜ As a statement, ++gumballs tells the com- puter to add 1 to gumballs. ߜ As an expression, the value of ++gumballs is 29. So with System.out.println(++gum- balls) , the variable gumballs gets 1 added to it, and the code System.out.println (++gumballs) really means System.out. println(29) . 12_588745 ch07.qxd 3/16/05 9:19 PM Page 115 [...]... Name Java s Primitive Numeric Types Range of Values Whole Number Types Byte –128 to 127 Short –32768 to 32767 Int –2 147 483 648 to 2 147 483 647 Long –92233720368 547 75808 to 92233720368 547 75807 Decimal Number Types Float –3 .4 1038 to 3 .4 1038 Double –1.8×10308 to 1.8×10308 Table 7-1 lists six of Java s primitive types (also known as simple types) Java has only eight primitive types, so only two of Java s... represent those possibilities, I need very little fanfare How about 0 for “false” and 1 for “true?” They ask, “Do you accept our offer to write Beginning Programming with Java For Dummies, 2nd Edition?” “1,” I reply 122 Part II: Writing Your Own Java Programs Too bad I didn’t think of that a few months ago Anyway, this chapter deals with letters, truth, falsehood, and other such things Characters In... reads, “Have a Java 5.0 compiler? True.” The other sign reads, “Have a Java 5.0 compiler? False.” You evaluate the compiler situation and march on, veering right or left depending on your software situation A diagram of this story is shown in Figure 9-1 * This excerpt is reprinted with permission from Wiley Publishing, Inc If you can’t find a copy of Beginning Programming with Java For Dummies, 2nd Edition... problem Hey, admit it This sounds like fun! Chapter 9 Forks in the Road In This Chapter ᮣ Writing statements that choose between alternatives ᮣ Putting statements inside one another ᮣ Writing several kinds of decision making statements H ere’s an excerpt from Beginning Programming with Java For Dummies, 2nd Edition, Chapter 2: If your computer already has a Java 5.0 compiler, you can skip the next section’s... could be simpler? Now imagine sending a one-word fax The word is “true,” which is understood to mean, “true, I accept your offer to write Beginning Programming with Java For Dummies, 2nd Edition.” A fax with this message sends a picture of the four letters t-r-u-e, with fuzzy lines where dirt gets on the paper and little white dots where the cartridge runs short on toner But really, what’s the essence... Internally, the Java Virtual Machine doesn’t store boolean values with the letters t-r-u-e or f-a-l-s-e Instead, the JVM stores codes, like 0 for false and 1 for true When the computer displays a boolean value (as in System.out.println(eachKidGetsTen)), the Java virtual machine converts a code like 0 into the five-letter word false Comparing numbers; comparing characters In Listing 8 -4, I compare a... can’t just leave you with the incomplete story in Table 7-1 Table 8-2 Type Name Java s Primitive Non-numeric Types Range of Values Character Type char Thousands of characters, glyphs, and symbols Logical Type boolean Only true or false If you dissect parts of the Java virtual machine, you find that Java considers char to be a numeric type That’s because Java represents characters with something called... Figure 8 -4, the second smallLetter assignment puts 3 into smallLetter When the computer executes this second assignment statement, the old value R is gone 125 126 Part II: Writing Your Own Java Programs Figure 8 -4: Varying the value of small Letter Is that okay? Can you afford to forget the value that smallLetter once had? Yes, in Listing 8-2, it’s okay After you’ve assigned a value to bigLetter with the... who created Java s Scanner class didn’t create a next method for reading a single character So to input a single character, I paste two Java API methods together I use the findInLine and charAt methods What’s behind all this findInLine(“.”) charAt(0) nonsense? Without wallowing in too much detail, here’s how the findInLine(“.”).charAt(0) technique works: Java s findInLine method looks for things in... the character with single quote marks I digress A while ago, I wondered what would happen if I called the Character.to UpperCase method and fed the method a character that isn’t lowercase to begin with I yanked out the Java API documentation, but I found no useful information The documentation said that toUpperCase “converts the character argument to uppercase using case mapping information from . 32767 Int –2 147 483 648 to 2 147 483 647 Long –92233720368 547 75808 to 92233720368 547 75807 Decimal Number Types Float –3 .4 10 38 to 3 .4 10 38 Double –1.8×10 308 to 1.8×10 308 Table 7-1 lists six of Java s. about 0 for “false” and 1 for “true?” They ask, “Do you accept our offer to write Beginning Programming with Java For Dummies, 2nd Edition?” “1,” I reply. 13_588 745 ch08.qxd 3/16/05 9: 34 PM Page. “true, I accept your offer to write Beginning Programming with Java For Dummies, 2nd Edition.” A fax with this message sends a picture of the four letters t-r-u-e, with fuzzy lines where dirt gets

Ngày đăng: 12/08/2014, 10:21