Page 145 - Introduction to Programming with Java: A Problem Solving Approach
P. 145

                Practice Problem
Now let’s put what you’ve learned into practice by using the if statement within a complete program. Suppose you’re asked to write a sentence-tester program that checks whether a user-entered line ends with a period. Your program should print an error message if the last character in the line is not
a period. In writing the program, use a sample session as a guide. Note that the italicized
Mahatma Gandhi quote is a user-entered input value. Sample session:
Enter a sentence:
Permanent good can never be the outcome of violence.
Another sample session:
Enter a sentence:
Permanent good can never be the outcome of
Invalid entry – your sentence is not complete!
As your first step in implementing a solution, use pseudocode to generate an informal outline of the basic logic:
print “Enter a sentence: ”
input sentence
if sentence’s last character is not equal to ‘.’
4.4 &&
Logical Operator 111
    print “Invalid entry – your sentence is not complete!”
Apago PDF Enhancer
Note the simple “if” form of the if statement. That’s appropriate because there’s a need to do something (print an invalid entry message) or nothing. Why nothing? Because the problem description does not say to print anything for user entries that are legitimate sentences. In other words, the program should skip what’s in the if statement if you finish the sentence properly. Now we suggest that you try writing the Java code to implement this algorithm. You’ll need to use a couple of the String methods described near the end of Chapter 3. When you’re ready, look at the SentenceTester solution in Figure 4.4.
How does the SentenceTester program determine whether the last character is a period? Suppose the user enters “Hello.” In that case, what value would be assigned to the lastCharPosition variable? String’s length method returns the number of characters in a string. The number of characters in “Hello.” is six. Since the first position is zero, lastCharPosition would get assigned a value of (6 􏰀 1) or 5. Why do we want lastCharPosition? We need to see if the last character in the user-entered value is a period. To do so, we use lastCharPosition as the argument in a charAt method call. String’s charAt method returns the character at a specified index position within a string. The index position of the period in “Hello.” is 5, and the if condition checks whether the user-entered value’s last character is a period.
4.4 && Logical Operator
Up to this point, all of our if statement examples have used simple conditions. A simple condition evaluates directly to either true or false. In the next three sections, we introduce you to logical operators, like the “and” operator (&&) and the “or” operator (||), which make it possible to construct compound conditions. A compound condition is a conjunction (either an “anding” or an “oring”) of two or more conditions. When
Use desired out- put to specify problem.
 











































































   143   144   145   146   147