Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit b42cec2

Browse filesBrowse files
committed
ch06 revisions
1 parent 8ddb25f commit b42cec2
Copy full SHA for b42cec2

File tree

Expand file treeCollapse file tree

2 files changed

+60
-26
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+60
-26
lines changed
Open diff view settings
Collapse file

‎ch06/Loops.java‎

Copy file name to clipboardExpand all lines: ch06/Loops.java
+19-9Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Examples from Chapter 6.
2+
* Demonstrates uses of loops.
33
*/
44
public class Loops {
55

@@ -26,15 +26,22 @@ public static void plusplus() {
2626
int i = 1;
2727
while (i <= 5) {
2828
System.out.println(i);
29-
i++; // add 1 to i
29+
i++; // add 1 to i
3030
}
3131
}
3232

3333
public static void appreciate() {
3434
int i = 2;
3535
while (i <= 8) {
3636
System.out.print(i + ", ");
37-
i += 2; // add 2 to i
37+
i += 2; // add 2 to i
38+
}
39+
System.out.println("Who do we appreciate?");
40+
}
41+
42+
public static void appreciate2() {
43+
for (int i = 2; i <= 8; i += 2) {
44+
System.out.print(i + ", ");
3845
}
3946
System.out.println("Who do we appreciate?");
4047
}
@@ -57,22 +64,25 @@ public static void nested() {
5764
}
5865

5966
public static void main(String[] args) {
60-
System.out.println("countdown");
67+
System.out.println("\ncountdown");
6168
countdown(3);
6269

63-
System.out.println("sequence");
70+
System.out.println("\nsequence");
6471
sequence(10);
6572

66-
System.out.println("plusplus");
73+
System.out.println("\nplusplus");
6774
plusplus();
6875

69-
System.out.println("appreciate");
76+
System.out.println("\nappreciate");
7077
appreciate();
7178

72-
System.out.println("loopvar");
79+
System.out.println("\nappreciate2");
80+
appreciate2();
81+
82+
System.out.println("\nloopvar");
7383
loopvar();
7484

75-
System.out.println("nested");
85+
System.out.println("\nnested");
7686
nested();
7787
}
7888

Collapse file

‎ch06/Strings.java‎

Copy file name to clipboardExpand all lines: ch06/Strings.java
+41-17Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import java.util.Scanner;
2+
13
/**
2-
* Demonstrates uses of Strings.
4+
* Demonstrates uses of strings.
35
*/
46
public class Strings {
57

@@ -10,8 +12,8 @@ public static void main(String[] args) {
1012
String fruit = "banana";
1113
char letter0 = fruit.charAt(0);
1214

13-
if (letter0 == 'a') {
14-
System.out.println('?');
15+
if (letter0 == 'A') {
16+
System.out.println("It's an A!");
1517
}
1618

1719
System.out.print("Roman alphabet: ");
@@ -26,6 +28,17 @@ public static void main(String[] args) {
2628
}
2729
System.out.println();
2830

31+
// Which loop to use
32+
33+
Scanner in = new Scanner(System.in);
34+
System.out.print("Enter a number: ");
35+
while (!in.hasNextDouble()) {
36+
String word = in.next();
37+
System.err.println(word + " is not a number");
38+
System.out.print("Enter a number: ");
39+
}
40+
double number = in.nextDouble();
41+
2942
// String iteration
3043

3144
for (int i = 0; i < fruit.length(); i++) {
@@ -43,33 +56,44 @@ public static void main(String[] args) {
4356
int index = fruit.indexOf('a');
4457
int index2 = fruit.indexOf('a', 2);
4558

59+
// Substrings
60+
61+
System.out.println(fruit.substring(0, 3));
62+
System.out.println(fruit.substring(2, 5));
63+
System.out.println(fruit.substring(6, 6));
64+
65+
System.out.println(fruit.substring(0));
66+
System.out.println(fruit.substring(2));
67+
System.out.println(fruit.substring(6));
68+
4669
// String comparison
4770

71+
System.out.print("Play again? ");
72+
String answer = in.nextLine();
73+
if (answer == "yes") { // wrong!
74+
System.out.println("Let's go!");
75+
} else {
76+
System.out.println("Goodbye!");
77+
}
78+
79+
if (answer.equals("yes")) { // correct
80+
System.out.println("Let's go!");
81+
}
82+
4883
String name1 = "Alan Turing";
4984
String name2 = "Ada Lovelace";
5085
if (name1.equals(name2)) {
5186
System.out.println("The names are the same.");
5287
}
5388

5489
int diff = name1.compareTo(name2);
55-
if (diff == 0) {
56-
System.out.println("The names are the same.");
57-
} else if (diff < 0) {
90+
if (diff < 0) {
5891
System.out.println("name1 comes before name2.");
5992
} else if (diff > 0) {
6093
System.out.println("name2 comes before name1.");
94+
} else {
95+
System.out.println("The names are the same.");
6196
}
62-
63-
// Substrings
64-
65-
System.out.println(fruit.substring(0));
66-
System.out.println(fruit.substring(2));
67-
System.out.println(fruit.substring(6));
68-
69-
System.out.println(fruit.substring(0, 3));
70-
System.out.println(fruit.substring(2, 5));
71-
System.out.println(fruit.substring(6, 6));
72-
7397
}
7498

7599
/**

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.