Java Reference
In-Depth Information
The printTicket method reduces the value of balance by the value of price . As a con-
sequence, if a customer inserts more money than the price of the ticket, then some money will
be left in the balance that could be used toward the price of a second ticket. Alternatively, the
customer could ask to be refunded the remaining balance, and that is what the refundBalance
method does, as we shall see in the next section.
2.15
Scope highlighting
You will have noticed by now that the BlueJ editor displays source code with some addi-
tional decoration: colored boxes around some elements that are not reproduced in the code
samples shown in this topic (Figure 2.5).
Figure 2.5
Scope highlighting
in the BlueJ editor
These colored annotations are known as scope highlighting , and they help clarify logical units
of your program. A scope (also called a block ) is a unit of code usually indicated by a pair of
curly brackets. The whole body of a class is a scope, as is the body of each method and the if
and else parts of an if statement.
As you can see, scopes are often nested: the if statement is inside a method, which is inside a
class. BlueJ helps by distinguishing different kinds of scopes with different colors.
One of the most common errors in the code of beginning programmers is getting the curly
brackets wrong—either by having them in the wrong place or by having a bracket missing alto-
gether. Two things can greatly help in avoiding this kind of error:
Pay attention to proper indentation of your code. Every time a new scope starts (after an open
curly bracket), indent the following code one level more. Closing the scope brings the inden-
tation back. If your indentation is completely out, use BlueJ's “Auto-layout” function (find it
in the editor menu!) to fix it.
 
 
Search WWH ::




Custom Search