Skip to content

Commit 2dd19cc

Browse files
committed
added ch07 TODO sections
1 parent 98d82bc commit 2dd19cc

File tree

2 files changed

+104
-51
lines changed

2 files changed

+104
-51
lines changed

ch07/Loops.java

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import java.util.Scanner;
2+
3+
public class Loops {
4+
5+
public static void countdown(int n) {
6+
while (n > 0) {
7+
System.out.println(n);
8+
n = n - 1;
9+
}
10+
System.out.println("Blastoff!");
11+
}
12+
13+
public static void sequence(int n) {
14+
while (n != 1) {
15+
System.out.println(n);
16+
if (n % 2 == 0) { // n is even
17+
n = n / 2;
18+
} else { // n is odd
19+
n = n * 3 + 1;
20+
}
21+
}
22+
}
23+
24+
public static double scanDouble() {
25+
Scanner in = new Scanner(System.in);
26+
boolean okay;
27+
do {
28+
System.out.print("Enter a number: ");
29+
if (in.hasNextDouble()) {
30+
okay = true;
31+
} else {
32+
okay = false;
33+
String word = in.next();
34+
System.err.println(word + " is not a number");
35+
}
36+
} while (!okay);
37+
double x = in.nextDouble();
38+
return x;
39+
}
40+
41+
public static double scanDouble2() {
42+
Scanner in = new Scanner(System.in);
43+
while (true) {
44+
System.out.print("Enter a number: ");
45+
if (in.hasNextDouble()) {
46+
break;
47+
}
48+
String word = in.next();
49+
System.err.println(word + " is not a number");
50+
}
51+
double x = in.nextDouble();
52+
return x;
53+
}
54+
55+
public static double addNumbers() {
56+
Scanner in = new Scanner(System.in);
57+
int x = -1;
58+
int sum = 0;
59+
while (x != 0) {
60+
x = in.nextInt();
61+
if (x <= 0) {
62+
continue;
63+
}
64+
System.out.println("Adding " + x);
65+
sum += x;
66+
}
67+
return sum;
68+
}
69+
70+
public static void main(String[] args) {
71+
System.out.println("countdown");
72+
countdown(3);
73+
74+
System.out.println("sequence");
75+
sequence(10);
76+
}
77+
78+
}

ch07/Tables.java

Lines changed: 26 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,7 @@
22
* Examples from Chapter 7.
33
*/
44
public class Tables {
5-
6-
public static void countdown(int n) {
7-
while (n > 0) {
8-
System.out.println(n);
9-
n = n - 1;
10-
}
11-
System.out.println("Blastoff!");
12-
}
13-
14-
public static void sequence(int n) {
15-
while (n != 1) {
16-
System.out.println(n);
17-
if (n % 2 == 0) { // n is even
18-
n = n / 2;
19-
} else { // n is odd
20-
n = n * 3 + 1;
21-
}
22-
}
23-
}
24-
5+
256
public static void example() {
267
int i = 1;
278
while (i < 10) {
@@ -30,7 +11,7 @@ public static void example() {
3011
i = i + 1;
3112
}
3213
}
33-
14+
3415
public static void example2() {
3516
int i = 1;
3617
while (i < 10) {
@@ -39,7 +20,7 @@ public static void example2() {
3920
i = i + 1;
4021
}
4122
}
42-
23+
4324
public static void example3() {
4425
final double LOG2 = Math.log(2);
4526
int i = 1;
@@ -49,7 +30,7 @@ public static void example3() {
4930
i = i * 2;
5031
}
5132
}
52-
33+
5334
public static void example4() {
5435
int i = 1;
5536
while (i <= 6) {
@@ -58,7 +39,7 @@ public static void example4() {
5839
}
5940
System.out.println();
6041
}
61-
42+
6243
public static void printRow() {
6344
int i = 1;
6445
while (i <= 6) {
@@ -67,7 +48,7 @@ public static void printRow() {
6748
}
6849
System.out.println();
6950
}
70-
51+
7152
public static void printRow2(int n) {
7253
int i = 1;
7354
while (i <= 6) {
@@ -76,31 +57,31 @@ public static void printRow2(int n) {
7657
}
7758
System.out.println();
7859
}
79-
60+
8061
public static void example5() {
8162
int i = 1;
8263
while (i <= 6) {
8364
printRow2(i);
8465
i = i + 1;
8566
}
8667
}
87-
68+
8869
public static void printTable() {
8970
int i = 1;
9071
while (i <= 6) {
9172
printRow2(i);
9273
i = i + 1;
9374
}
9475
}
95-
76+
9677
public static void printTable2(int rows) {
9778
int i = 1;
9879
while (i <= rows) {
9980
printRow2(i);
10081
i = i + 1;
10182
}
10283
}
103-
84+
10485
public static void printRow3(int n, int cols) {
10586
int i = 1;
10687
while (i <= cols) {
@@ -109,72 +90,66 @@ public static void printRow3(int n, int cols) {
10990
}
11091
System.out.println();
11192
}
112-
93+
11394
public static void printTable3(int rows) {
11495
int i = 1;
11596
while (i <= rows) {
11697
printRow3(i, rows);
11798
i = i + 1;
11899
}
119100
}
120-
101+
121102
public static void printTable4(int rows) {
122103
for (int i = 1; i <= rows; i = i + 1) {
123104
printRow3(i, rows);
124105
}
125106
}
126-
107+
127108
public static void printRow4(int n, int cols) {
128109
int i;
129110
for (i = 1; i <= cols; i = i + 1) {
130111
System.out.printf("%4d", n * i);
131112
}
132113
System.out.println(i);
133114
}
134-
115+
135116
public static void main(String[] args) {
136-
System.out.println("countdown");
137-
countdown(3);
138-
139-
System.out.println("sequence");
140-
sequence(10);
141-
142117
System.out.println("example");
143118
example();
144-
119+
145120
System.out.println("example2");
146121
example2();
147-
122+
148123
System.out.println("example3");
149124
example3();
150-
125+
151126
System.out.println("example4");
152127
example4();
153-
128+
154129
System.out.println("example5");
155130
example5();
156-
131+
157132
System.out.println("printRow");
158133
printRow();
159-
134+
160135
System.out.println("printRow2");
161136
printRow2(6);
162-
137+
163138
System.out.println("printTable");
164139
printTable();
165-
140+
166141
System.out.println("printTable2");
167142
printTable2(6);
168-
143+
169144
System.out.println("printRow3");
170145
printRow3(6, 6);
171-
146+
172147
System.out.println("printTable3");
173148
printTable3(6);
174-
149+
175150
System.out.println("printRow4");
176151
printRow4(6, 6);
177-
152+
178153
System.out.println("printTable4");
179154
printTable4(6);
180155
}

0 commit comments

Comments
 (0)