Java Reference
In-Depth Information
Certain parts of this pseudo-code are proper bits of Java, and those will appear in almost all
conditional statements - the keywords if and else , the round brackets around the test, and the
curly brackets marking the two blocks - while the other three italicized parts will be fleshed out
differently for each particular situation being coded.
It is important to appreciate that only one of the two blocks of statements following the test
will ever be performed following the evaluation of the test. So, in the example from the
insertMoney method, following the test of an inserted amount we shall only either add the
amount to the balance or print the error message. The test uses the greater-than operator ,
“>”, to compare the value in amount against zero. If the value is greater than zero, then it
is added to the balance. If it is not greater than zero, then an error message is printed. By
using a conditional statement, we have, in effect, protected the change to balance in the
case where the parameter does not represent a valid amount. Details of other Java operators
can be found in Appendix C. The obvious ones to mention at this point are “<” (less-than),
“<=” (less-than or equal-to), and “>=” (greater-than or equal-to). All are used to compare
two numeric values, as in the printTicket method.
Boolean expres-
sions have only
two possible val-
ues: true and false.
They are commonly
found controlling
the choice between
the two paths
through a condi-
tional statement.
The test used in a conditional statement is an example of a boolean expression. Earlier in this
chapter, we introduced arithmetic expressions that produced numerical results. A boolean ex-
pression has only two possible values ( true or false ): the value of amount is either greater
than zero ( true ) or it is not greater ( false ). A conditional statement makes use of those two
possible values to choose between two different actions.
Exercise 2.46 Check that the behavior we have discussed here is accurate by creating
a TicketMachine instance and calling insertMoney with various actual parameter val-
ues. Check the balance both before and after calling insertMoney . Does the balance ever
change in the cases when an error message is printed? Try to predict what will happen if you
enter the value zero as the parameter, and then see if you are right.
Exercise 2.47 Predict what you think will happen if you change the test in insertMoney
to use the greater-than or equal-to operator:
if(amount >= 0)
Check your predictions by running some tests. What difference does it make to the behavior
of the method?
Exercise 2.48 Rewrite the if-else statement so that the error message is printed if the
boolean expression is true but the balance is increased if the expression is false. You will obvi-
ously have to rewrite the condition to make things happen this way around.
Exercise 2.49 In the figures project we looked at in Chapter 1 we used a boolean field to
control a feature of the circle objects. What was that feature? Was it well suited to being con-
trolled by a type with only two different values?
Search WWH ::

Custom Search