Java Reference
In-Depth Information
The following step illustrates how to enter code for the for loop and
assignment statements.
To Enter Code for the for Loop and Assignment Statements
1. Enter lines 23 through 29 as shown in Figure 5-13 on the previous page.
The for loop and assignment statements are displayed (Figure 5-14).
for loop
variable
assignments
FIGURE 5-14
Line 23 includes a for statement to define the for loop with three parameters.
The first parameter declares and sets the i variable to 0. The second parameter
tests i to make sure it is less than the sum of the sm and non variables, which rep-
resent the number of smoking and nonsmoking rooms respectively. The third
parameter increments the variable by one, using the increment operator, ++. Line
24 assigns each element of the occupied array a value of false. The for loop thus
will execute until the number of occupied rooms is not less than the total number
of rooms.
Lines 26 and 27 assign the passed values, sm and non, to the class variables,
numSmoking and numNonSmoking, for use in all methods of the Rooms class.
Line 28 is the closing brace for the Rooms() constructor method. This method
now can be called from the driver class to create an instance of the Rooms
object. When called, it must pass two integer arguments.
 
Search WWH ::




Custom Search