Java Reference
In-Depth Information
The following step illustrates how to enter the Rooms() constructor method
header and the code to construct an array.
To Enter the Rooms() Constructor Method Header
1. Enter lines 19 through 22 as shown in Figure 5-11 on the previous page.
Be sure to use proper indentation.
The TextPad window displays the code for the Rooms() constructor method
header and the code to construct the occupied array (Figure 5-12).
Rooms() constructor
method header
array constructor
FIGURE 5-12
You can create multiple constructor methods of the same data type if you
anticipate a need for multiple instances of an object with different arguments.
For example, if you plan to use an instance of the Rooms object in a different
application that books reservations only for nonsmoking rooms (passing only
a single integer), you can include a second Rooms() constructor method that
uses a single parameter. A second constructor method header might be coded
as follows:
public Rooms(int rms)
In that case, whether the driver class passes just one argument or two, the Rooms
object would accept the call. This practice of defining more than one method
with the same name is called method overloading . Later chapters will present
additional examples of method overloading.
Counter-Controlled Loops
Recall that when you wanted a program to repeat a set of instructions, you used
a repetition structure called a while loop. As you learned in Chapter 4, a while
loop works well when you need to perform a task an undetermined number of
times. All of the code that should be repeated, or looped, while the condition
is evaluated as true is enclosed in braces. The condition must be a boolean
Search WWH ::




Custom Search