January 1st, 2008
Chapter 5: Controlling Program Flow with Decision-Making Statements 121 To break or not to break In every Java programmer s life, a time comes when he or she forgets to use breakstatements. At first, the resulting output is confusing, but then the programmer remembers fall-through. The term fall-through describes what happens when you end a case without a breakstatement. What happens is that execution of the code falls right through to the next case in line. Execution keeps falling through until you eventually reach a breakstatement or the end of the entire switchstatement. Usually, when you re using a switchstatement, you don t want fall-through, so you pepper breakstatements throughout the switchstatements. But, occasionally, fall-through is just the thing you need. Take, for instance, the Al s All Wet song. (The classy lyrics are shown in the sidebar bearing the song s name.) Each verse of Al s All Wet adds new lines in addition to the lines from previous verses. This situation (accumulating lines from one verse to another) cries out for a switchstatement with fall-through. Listing 5-7 demonstrates the idea. Listing 5-7: A switch Statement with Fall-Through import static java.lang.System.out; import java.util.Scanner; class FallingForYou { public static void main(String args[]) { Scanner myScanner = new Scanner(System.in); out.print( Which verse? ); int verse = myScanner.nextInt(); switch (verse) { case 3: out.print( Last refrain, ); out.println( last refrain, ); case 2: out.print( He s a pain, ); out.println( he s a pain, ); case 1: out.print( Has no brain, ); out.println( has no brain, ); } out.println( In the rain, in the rain. ); out.println( Ohhhhhhhh… ); out.println(); } }
We will hook you up with a JSP web hosting at the great rate, check our jsp web hosting, and join us today!!!
Posted in Tomcat EJB Blog | No Comments »
December 31st, 2007
120 Part II: Writing Your Own Java Programs Al s All Wet Sung to the tune of Gentille Alouette : In the rain, in the rain. Ohhhhhhhh. . . . Al s all wet. Oh, why is Al all wet? Oh, Al s all wet cause he s standing in the Al s all wet. Oh, why is Al all wet? Oh, rain. Al s all wet cause he s standing in the Why is Al out in the rain? rain. That s because he has no brain. Why is Al out in the rain? Has no brain, has no brain, Cause this is the last refrain. In the rain, in the rain. Last refrain, last refrain, Ohhhhhhhh. . . . He s a pain, he s a pain, Has no brain, has no brain, Al s all wet. Oh, why is Al all wet? Oh, In the rain, in the rain. Al s all wet cause he s standing in the Ohhhhhhhh. . . . rain. Why is Al out in the rain? Al s all wet. Oh, why is Al all wet? Oh, That s because he is a pain. Al s all wet cause he s standing in the He s a pain, he s a pain, rain. Has no brain, has no brain, Harriet Ritter and Barry Burd Figure 5-9: The big fork in the code of Listing 5-6. Which verseis this? Has no brain Ohhhhhhhh…. 1 Is a pain 2 Last refrain 3Try again other
Hosting services offered by our company comes with free domain name if you pay at yearly basis. Find out more at New Orleans Web Hosting services.
Posted in Tomcat EJB Blog | No Comments »
December 30th, 2007
Chapter 5: Controlling Program Flow with Decision-Making Statements 119 Figure 5-8 shows two runs of the program in Listing 5-6. (The overall idea behind the program is illustrated in Figure 5-9.) First, the user types a number, like the number 2. Then, execution of the program reaches the top of the switchstatement. The computer checks the value of the versevariable. When the computer determines that the versevariable s value is 2, the computer checks each case of the switchstatement. The value 2doesn t match the topmost case, so the computer proceeds on to the middle of the three cases. The value posted for the middle case (the number 2) matches the value of the versevariable, so the computer executes the statements that come immediately after case 2. These two statements are out.println( That s because he is a pain. ); break; The first of the two statements displays the line That s because he is a pain.on the screen. The second statement is called a break statement. (What a surprise!) When the computer encounters a breakstatement, the computer jumps out of whatever switchstatement it s in. So, in Listing 5-6, the computer skips right past the case that would display Cause this is the last refrain.In fact, the computer jumps out of the entire switch statement and goes straight to the statement just after the end of the switch statement. The computer displays Ohhhhhhhh. . . .because that s what the statement after the switchstatement tells the computer to do. Figure 5-8: Running the code of Listing 5-6. If the pesky user asks for verse 6, the computer responds by dropping past cases 1, 2, and 3. Instead, the computer does the default. In the default, the computer displays No such verse. Please try again, and then breaks out of the switchstatement. After the computer is out of the switchstatement, the computer displays Ohhhhhhhh. . . . You don t really need to put a breakat the very end of a switchstatement. In Listing 5-6, the last break(the breakthat s part of the default) is just for the sake of overall tidiness.
We provides quality the CPanel Web Hosting. All our web hosting plans regular, business and expert are competitively priced and unsurpassed in reliability, uptime, and quality of service.
Posted in Tomcat EJB Blog | No Comments »
December 29th, 2007
118 Part II: Writing Your Own Java Programs if (verse == 1) { out.println( That s because he has no brain. ); } if (verse == 2) { out.println( That s because he is a pain. ); } if (verse == 3) { out.println( Cause this is the last refrain. ); } But that approach seems wasteful. Why not create a statement that checks the value of versejust once and then takes an action based on the value that it finds? Fortunately, just such a statement exists. It s called a switchstatement. Listing 5-6 has an example of a switchstatement. Listing 5-6: A switch Statement import static java.lang.System.out; import java.util.Scanner; class JustSwitchIt { public static void main(String args[]) { Scanner myScanner = new Scanner(System.in); out.print( Which verse? ); int verse = myScanner.nextInt(); switch (verse) { case 1: out.println( That s because he has no brain. ); break; case 2: out.println( That s because he is a pain. ); break; case 3: out.println( Cause this is the last refrain. ); break; default: out.println( No such verse. Please try again. ); break; } out.println( Ohhhhhhhh. . . . ); } }
We will hook you up with a JSP web hosting at the great rate, check our jsp web hosting, and join us today!!!
Posted in Tomcat EJB Blog | No Comments »
December 28th, 2007
Chapter 5: Controlling Program Flow with Decision-Making Statements 117 Does username equal “bburd”? no yes Unknown Does password user equal “swordfish”? no yes Figure 5-7: Don t try eating with this fork. Your basic switch statement Now, it s time to explore situations in which you have a decision with many branches. Take, for instance, the popular campfire song Al s All Wet. (For a review of the lyrics, see the sidebar.) You re eager to write code that prints this song s lyrics. Fortunately, you don t have to type all the words over and over again. Instead, you can take advantage of the repetition in the lyrics. A complete program to display the Al s All Wet lyrics won t come until Chapter 6. In the meantime, assume that you have a variable named verse. The value of verseis 1, 2, 3, or 4, depending on which verse of Al s All Wet you re trying to print. You could have a big, clumsy bunch of ifstatements that checks each possible versenumber. Incorrect You’re in password
We are dedicated to offering you a reliable, fast, and scalable unlimited web hosting home for your business, and personal web sites.Go and see our shared web hosting services.
Posted in Tomcat EJB Blog | No Comments »
December 27th, 2007
116 Part II: Writing Your Own Java Programs Listing 5-5 (continued) out.println( You re in. ); } else { out.println( Incorrect password ); } } else { out.println( Unknown user ); } } } Figure 5-6 shows several runs of the code in Listing 5-5. The main idea is that to log on, you have to pass two tests. (In other words, two conditions must be true.) The first condition tests for a valid username; the second condition tests for the correct password. If you pass the first test (the username test), you march right into another ifstatement that performs a second test (the password test). If you fail the first test, you never make it to the second test. The overall plan is shown in Figure 5-7. Figure 5-6: Authenticating a user. The code in Listing 5-5 does a good job with nested ifstatements, but it does a terrible job with real-world user authentication. First of all, never show a password in plain view (without asterisks to masquerade the password). Second, don t handle passwords without encrypting them. Third, don t tell the malicious user which of the two words (the username or the password) was entered incorrectly. Fourth . . . well I could go on and on. The code in Listing 5-5 just isn t meant to illustrate good username/password practices. Choosing among Many Alternatives (Java switch Statements) I m the first to admit that I hate making decisions. If things go wrong, I would rather have the problem be someone else s fault. Writing the previous sections (on making decisions with Java s ifstatement) knocked the stuffing right out of me. That s why my mind boggles as I begin this section on choosing among many alternatives. What a relief it is to have that confession out of the way!
Do you have a godaddy domain name? If you do, you have found the right web hosting provider for you.Our Godaddy Web Hosting packages are the best match to well known, affordable godaddy domain names.
Posted in Tomcat EJB Blog | No Comments »
December 26th, 2007
Chapter 5: Controlling Program Flow with Decision-Making Statements 115 conclude that the whole expression is false. The fact is that, in Java, any && operator is evaluated before any ||operator. So the expression really asks if 2 < 5 || (something-or-other). Because 2 < 5 is true, the whole expression is true. To change the expression s value from trueto false, you can put the expression s first two comparisons in parentheses, like this: (2 < 5 || 100 < 6) && 27 < 1 Java s ||operator is inclusive. This means that you get a truevalue whenever the thing on the left side is true, the thing on the right side is true, or both things are true. For instance, the expression 2 < 10 || 20 < 30is true. In Java, you can t combine comparisons the way you do in ordinary English. In English, you may say, We ll have between three and ten people at the dinner table. But in Java, you get an error message if you write 3 <= people <= 10. To do this comparison, you need something like 3 <= people && people <= 10. Building a Nest Have you seen those cute Russian Matryoshka nesting dolls? Open up one, and another one is inside. Open up the second, and a third one is inside it. You can do the same thing with Java s ifstatements. (Talk about fun!) Listing 5-5 shows you how. Listing 5-5: Nested if Statements import static java.lang.System.out; import java.util.Scanner; class Authenticator2 { public static void main(String args[]) { Scanner myScanner = new Scanner(System.in); out.print( Username: ); String username = myScanner.next(); if (username.equals( bburd )) { out.print( Password: ); String password = myScanner.next(); if (password.equals( swordfish )) { (continued)
Please check our cheap domain web hosting services, our secret is fast and stable servers, dedicated 24/7 customer support, and many useful FREE bonuses.
Posted in Tomcat EJB Blog | No Comments »
December 25th, 2007
114 Part II: Writing Your Own Java Programs Listing 5-4: Checking Username and Password import static java.lang.System.out; import java.util.Scanner; class Authenticator { public static void main(String args[]) { Scanner myScanner = new Scanner(System.in); out.print( Username: ); String username = myScanner.next(); out.print( Password: ); String password = myScanner.next(); if ( (username.equals( bburd ) && password.equals( swordfish )) || (username.equals( hritter ) && password.equals( preakston )) ) { out.println( You re in. ); } else { out.println( You re suspicious. ); } } } Figure 5-5: Using logical operators. Keep an eye on those parentheses! When you re combining comparisons with logical operators, it s better to waste typing effort and add unneeded parentheses than to goof up your result by using too few parentheses. Take, for example, the expression 2 < 5 || 100 < 6 && 27 < 1 By misreading this expression, you may come to the conclusion that the expression is false. That is, you could wrongly read the expression as meaning (something-or-other) && 27 < 1. Because 27 < 1 is false, you would
For the first time, E-commerce websites allow small and large companies to actually compete on a level playing field.We highly recommend you to visit ecommerce website hosting.
Posted in Tomcat EJB Blog | 2 Comments »
December 24th, 2007
Chapter 5: Controlling Program Flow with Decision-Making Statements 113 Importing everything in one fell swoop The first line of Listing 5-3 illustrates a lazy way of importing both System. outand System.in. To import everything that Systemhas to offer, you use the asterisk wildcard character (*). In fact, importing java.lang.System.* is like having about 30 separate importdeclarations, including System.in, System.out, System.err, System.nanoTime, and many other System things. The use of an asterisk in an importdeclaration is generally considered bad programming practice, so I don t do it often in this book s examples. But for larger programs programs that use dozens of names from the Java API the lazy asterisk trick is handy. You can t toss an asterisk anywhere you want inside an importdeclaration. For example, you can t import everything starting with java by writing import java.*. You can substitute an asterisk only for the name of a class or for the name of something static that s tucked away inside a class. For more information about asterisks in import declarations, see Chapter 9. For information about static things, see Chapter 10. Java s logical operators Mr. Spock would be pleased. Java has all the operators that you need for mixing and matching logical tests. The operators are shown in Table 5-2. Table 5-2 Logical Operators Operator Symbol Meaning Example && and 5 < x && x < 10 || or x < 5 || 10 < x ! not !password.equals( swordfish ) You can use these operators to form all kinds of elaborate conditions. Listing 5-4 has an example. Some runs of the program of Listing 5-4 are shown in Figure 5-5. When the username is bburd and the password is swordfish or when the username is hritter and the password is preakston, the user gets a nice message. Otherwise, the user is a bum who gets the nasty message that he or she deserves.
Would you like to be a member of headache free web site owner’s community. Now you can. We focus in IX Web Hosting, go go go!
Posted in Tomcat EJB Blog | No Comments »
December 23rd, 2007
112 Part II: Writing Your Own Java Programs Figure 5-4: The result of using == and using Java s equals method. In Listing 5-3, the call myScanner.next()grabs whatever word the user types on the computer keyboard. The code shoves this word into the variable named password. Then the program s ifstatements use two different techniques to compare passwordwith swordfish . The more appropriate of the two techniques uses Java s equalsmethod. The equalsmethod looks funny because when you call it you put a dot after one string and put the other string in parentheses. But that s the way you have to do it. In calling Java s equalsmethod, it doesn t matter which string gets the dot and which gets the parentheses. For instance, in Listing 5-3, you could have written if ( swordfish .equals(password)) The method would work just as well. A call to Java s equalsmethod looks imbalanced, but it s not. There s a reason behind the apparent imbalance between the dot and the parentheses. The idea is that you have two objects: the passwordobject and the swordfish object. Each of these two objects is of type String. (However, password is a variable of type String, and swordfish is a Stringliteral.) When you write password.equals( swordfish ), you re calling an equals method that belongs to the password object. As you call that method, you re feeding swordfish to the method as the method s parameter (pun intended). You can read more about methods belonging to objects in Chapter 7. When comparing strings with one another, use the equals method not the double equal sign.
We are the cheapest Catholic web hosting provider, check Catholic Web Hosting services and make sure of alone.
Posted in Tomcat EJB Blog | No Comments »