Skip to content

Commit 9cc490e

Browse files
committed
reviewed ch06
1 parent ade9ad8 commit 9cc490e

6 files changed

Lines changed: 88 additions & 29 deletions

File tree

File renamed without changes.
File renamed without changes.

ch06/Loops.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,58 @@ public static void sequence(int n) {
2222
}
2323
}
2424

25+
public static void plusplus() {
26+
int i = 1;
27+
while (i <= 5) {
28+
System.out.println(i);
29+
i++; // add 1 to i
30+
}
31+
}
32+
33+
public static void appreciate() {
34+
int i = 2;
35+
while (i <= 8) {
36+
System.out.print(i + ", ");
37+
i += 2; // add 2 to i
38+
}
39+
System.out.println("Who do we appreciate?");
40+
}
41+
42+
public static void loopvar() {
43+
int n;
44+
for (n = 3; n > 0; n--) {
45+
System.out.println(n);
46+
}
47+
System.out.println("n is now " + n);
48+
}
49+
50+
public static void nested() {
51+
for (int x = 1; x <= 10; x++) {
52+
for (int y = 1; y <= 10; y++) {
53+
System.out.printf("%4d", x * y);
54+
}
55+
System.out.println();
56+
}
57+
}
58+
2559
public static void main(String[] args) {
2660
System.out.println("countdown");
2761
countdown(3);
2862

2963
System.out.println("sequence");
3064
sequence(10);
65+
66+
System.out.println("plusplus");
67+
plusplus();
68+
69+
System.out.println("appreciate");
70+
appreciate();
71+
72+
System.out.println("loopvar");
73+
loopvar();
74+
75+
System.out.println("nested");
76+
nested();
3177
}
3278

3379
}
Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* Demonstates uses of Strings.
33
*/
4-
public class StringsThings {
4+
public class Strings {
55

66
public static void main(String[] args) {
77

@@ -26,40 +26,18 @@ public static void main(String[] args) {
2626
}
2727
System.out.println();
2828

29-
// Strings are immutable
30-
31-
String name = "Alan Turing";
32-
String upperName = name.toUpperCase();
33-
34-
String text = "Computer Science is fun!";
35-
text = text.replace("Computer Science", "CS");
36-
37-
// String traversal
29+
// String iteration
3830

3931
for (int i = 0; i < fruit.length(); i++) {
4032
char letter = fruit.charAt(i);
4133
System.out.println(letter);
4234
}
4335

44-
for (char letter : fruit.toCharArray()) {
45-
System.out.println(letter);
46-
}
47-
4836
int length = fruit.length();
4937
char last = fruit.charAt(length - 1); // correct
5038

5139
System.out.println(reverse(fruit));
5240

53-
// Substrings
54-
55-
System.out.println(fruit.substring(0));
56-
System.out.println(fruit.substring(2));
57-
System.out.println(fruit.substring(6));
58-
59-
System.out.println(fruit.substring(0, 3));
60-
System.out.println(fruit.substring(2, 5));
61-
System.out.println(fruit.substring(6, 6));
62-
6341
// The indexOf method
6442

6543
int index = fruit.indexOf('a');
@@ -82,13 +60,16 @@ public static void main(String[] args) {
8260
System.out.println("name2 comes before name1.");
8361
}
8462

85-
// Wrapper classes
63+
// Substrings
64+
65+
System.out.println(fruit.substring(0));
66+
System.out.println(fruit.substring(2));
67+
System.out.println(fruit.substring(6));
8668

87-
String str = "12345";
88-
int num = Integer.parseInt(str);
69+
System.out.println(fruit.substring(0, 3));
70+
System.out.println(fruit.substring(2, 5));
71+
System.out.println(fruit.substring(6, 6));
8972

90-
num = 12345;
91-
str = Integer.toString(num);
9273
}
9374

9475
/**
File renamed without changes.

ch09/Things.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Demonstates uses of Things.
3+
*/
4+
public class Things {
5+
6+
public static void main(String[] args) {
7+
8+
// Strings are immutable
9+
10+
String name = "Alan Turing";
11+
String upperName = name.toUpperCase();
12+
13+
String text = "Computer Science is fun!";
14+
text = text.replace("Computer Science", "CS");
15+
16+
// enhanced
17+
18+
for (char letter : fruit.toCharArray()) {
19+
System.out.println(letter);
20+
}
21+
22+
// Wrapper classes
23+
24+
String str = "12345";
25+
int num = Integer.parseInt(str);
26+
27+
num = 12345;
28+
str = Integer.toString(num);
29+
30+
}
31+
32+
}

0 commit comments

Comments
 (0)