Java Reference
In-Depth Information
The field band is assigned to F, and the setBand() method returns. We are
back in the turnOn() method, which sets the field tuning equal to the parame-
ter t. The turnOn() method is now complete, so flow of control jumps back
down to main().
The next line of code in main() is a println() statement, but before println() is
invoked, the getTuning() method of radio is called. Notice that no arguments
are passed in because getTuning() has no parameters. Control jumps to get-
Tuning(), which prints out the following:
Inside getTuning
The getTuning() method declares that it returns a float, so it must do so
somewhere in the method. Notice that it returns the tuning field, which is a
float containing the current radio station. The value of tuning is returned-by-
value, meaning that a copy of tuning is sent back to main(), similarly to how
arguments are copied into parameters.
The copy of tuning is sent back as a float and then concatenated to “The tun-
ing is “, displaying something like the following:
The tuning is 100.3
Control is back within main(), and the volume is set to 7. Recall that the
setVolume() sets the volume parameter to -5 to demonstrate that the argument
has not changed. The output of displaying the contents of x looks like the
following:
x = 7
The volume is then turned up by invoking turnUp() twice. The band is
changed by invoking changeBand(), and the final output looks similar to the
following:
The volume is now 9, the band is A, and the tuning is 100.3
Figure 5.2 shows a sample output of the ListenToRadio program.
Figure 5.2
Sample output of the ListenToRadio program when the command-line argument
is 100.3.
Search WWH ::




Custom Search