Java Reference
In-Depth Information
information from both. Then, we eliminate the statement-comment for the repe-
tend of the outer loop and write the two loops together. We usually omit the
braces surrounding the outer-loop repetend, since the repetend is a single state-
ment —a for-statement. It is one of the few contexts in this text where you will
find nested loops that are not separated by a statement-comment.
This schema processes the elements in row-major order . Processing the ele-
ments in column-major order means processing those in the first column, then
those in the second column, etc.
9.2.3
An interesting table
Activity 9-2.3 develops a method that constructs a table of interest values. Given
a number of years y and an interest number of interest rates n to calculate, the
method constructs an array interest[0..y-1][0..n-1] where interest[r]
[c] is the balance after r years when interest accumulates at the rate of (5 + .05
*c) percent per year. Watch the development of this method on the CD.
Get the method
from a footnote
on lesson page
9-2.
9.2.4
Row-major search
The function of this subsection returns an instance of class Coordinates , which
is given in Fig. 9.4. The function performs a row-major search of the array for a
value x, as stated in this specification:
Get the method
from a footnote
on lesson page
9-2.
/** = first index (r, c) in row-major order of x in d
* ( or the pair (d.length, 0) if x not in d) */
public static Coordinates search( int [][] d, int x)
/** An instance is a pair (r, c) of integers
public class Coordinates {
/** The row number and column number. */
int r; int c;
// Constructor: an instance (r, c)
public Coordinates( int r, int c) {
this .r= r;
this .c= c;
}
// = the string "(r, c)" (where r and c are replaced by the values in their fields)
public String toString() {
return "(" + r + ", " + c + ")";
}
}
Figure 9.4:
Class Coordinates
Search WWH ::




Custom Search