forked from ChrisMayfield/ThinkJavaCode2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTables.java
More file actions
65 lines (52 loc) · 1.48 KB
/
Tables.java
File metadata and controls
65 lines (52 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/**
* Encapsulation and generalization.
*/
public class Tables {
public static void printRow() {
for (int i = 1; i <= 6; i++) {
System.out.printf("%4d", 2 * i);
}
System.out.println();
}
public static void printRow(int n) {
for (int i = 1; i <= 6; i++) {
System.out.printf("%4d", n * i); // generalized n
}
System.out.println();
}
public static void printTable() {
for (int i = 1; i <= 6; i++) {
printRow(i);
}
}
public static void printTable(int rows) {
for (int i = 1; i <= rows; i++) { // generalized rows
printRow(i);
}
}
public static void printRow(int n, int cols) {
for (int i = 1; i <= cols; i++) { // generalized cols
System.out.printf("%4d", n * i);
}
System.out.println();
}
public static void printTable2(int rows) {
for (int i = 1; i <= rows; i++) {
printRow(i, rows);
}
}
public static void main(String[] args) {
System.out.println("\nprintRow()");
printRow();
System.out.println("\nprintRow(6)");
printRow(6);
System.out.println("\nprintTable()");
printTable();
System.out.println("\nprintTable(6)");
printTable(6);
System.out.println("\nprintRow(6, 6)");
printRow(6, 6);
System.out.println("\nprintTable2(6)");
printTable2(6);
}
}