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 6cc2f10

Browse filesBrowse files
committed
ch09 revisions
1 parent 4ce6193 commit 6cc2f10
Copy full SHA for 6cc2f10

File tree

Expand file treeCollapse file tree

4 files changed

+73
-41
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

4 files changed

+73
-41
lines changed
Open diff view settings
Collapse file

‎ch09/ArgValid.java‎

Copy file name to clipboard
+20Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Example of argument validation.
3+
*/
4+
public class ArgValid {
5+
6+
public static boolean isCapitalized(String str) {
7+
if (str == null || str.isEmpty()) {
8+
return false;
9+
}
10+
return Character.isUpperCase(str.charAt(0));
11+
}
12+
13+
public static void main(String[] args) {
14+
System.out.println(isCapitalized("Hi!"));
15+
System.out.println(isCapitalized("bye"));
16+
System.out.println(isCapitalized(""));
17+
System.out.println(isCapitalized(null));
18+
}
19+
20+
}
Collapse file

‎ch09/BigInt.java‎

Copy file name to clipboard
+20Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import java.math.BigInteger;
2+
3+
/**
4+
* BigInteger examples.
5+
*/
6+
public class BigInt {
7+
8+
public static void main(String[] args) {
9+
long x = 17;
10+
BigInteger big = BigInteger.valueOf(x);
11+
12+
String s = "12345678901234567890";
13+
BigInteger bigger = new BigInteger(s);
14+
15+
BigInteger a = BigInteger.valueOf(17);
16+
BigInteger b = BigInteger.valueOf(1700000000);
17+
BigInteger c = a.add(b);
18+
}
19+
20+
}
Collapse file

‎ch09/Objects.java‎

Copy file name to clipboard
+20-16Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import java.math.BigInteger;
2-
31
/**
42
* Demonstrates uses of objects and wrappers.
53
*/
@@ -14,22 +12,40 @@ public static void main(String[] args) {
1412
char[] array = {'c', 'a', 't'};
1513
String word = "dog";
1614

15+
String word1 = new String("dog"); // creates a string object
16+
String word2 = "dog"; // implicitly creates a string object
17+
18+
// the null keyword
19+
20+
String name0 = null;
21+
int[] combo = null;
22+
// System.out.println(name0.length()); // NullPointerException
23+
// System.out.println(combo[0]); // NullPointerException
24+
1725
// Strings are immutable
1826

1927
String name = "Alan Turing";
2028
String upperName = name.toUpperCase();
2129

30+
name.toUpperCase(); // ignores the return value
31+
System.out.println(name);
32+
name = name.toUpperCase(); // references the new string
33+
System.out.println(name);
34+
2235
String text = "Computer Science is fun!";
2336
text = text.replace("Computer Science", "CS");
2437

2538
// Wrapper classes
2639

40+
Integer i = Integer.valueOf(5);
41+
System.out.println(i.equals(5)); // displays true
42+
2743
Integer x = Integer.valueOf(123);
2844
Integer y = Integer.valueOf(123);
29-
if (x == y) { // false
45+
if (x == y) { // false
3046
System.out.println("x and y are the same object");
3147
}
32-
if (x.equals(y)) { // true
48+
if (x.equals(y)) { // true
3349
System.out.println("x and y have the same value");
3450
}
3551

@@ -38,18 +54,6 @@ public static void main(String[] args) {
3854

3955
num = 12345;
4056
str = Integer.toString(num);
41-
42-
// BigInteger arithmetic
43-
44-
long z = 17;
45-
BigInteger big = BigInteger.valueOf(z);
46-
47-
String s = "12345678901234567890";
48-
BigInteger bigger = new BigInteger(s);
49-
50-
BigInteger a = BigInteger.valueOf(17);
51-
BigInteger b = BigInteger.valueOf(1700000000);
52-
BigInteger c = a.add(b);
5357
}
5458

5559
}
Collapse file

‎ch09/Tables.java‎

Copy file name to clipboardExpand all lines: ch09/Tables.java
+13-25Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,73 +4,61 @@
44
public class Tables {
55

66
public static void printRow() {
7-
int i = 1;
8-
while (i <= 6) {
7+
for (int i = 1; i <= 6; i++) {
98
System.out.printf("%4d", 2 * i);
10-
i = i + 1;
119
}
1210
System.out.println();
1311
}
1412

1513
public static void printRow(int n) {
16-
int i = 1;
17-
while (i <= 6) {
14+
for (int i = 1; i <= 6; i++) {
1815
System.out.printf("%4d", n * i); // generalized n
19-
i = i + 1;
2016
}
2117
System.out.println();
2218
}
2319

2420
public static void printTable() {
25-
int i = 1;
26-
while (i <= 6) {
21+
for (int i = 1; i <= 6; i++) {
2722
printRow(i);
28-
i = i + 1;
2923
}
3024
}
3125

3226
public static void printTable(int rows) {
33-
int i = 1;
34-
while (i <= rows) { // generalized rows
27+
for (int i = 1; i <= rows; i++) { // generalized rows
3528
printRow(i);
36-
i = i + 1;
3729
}
3830
}
3931

4032
public static void printRow(int n, int cols) {
41-
int i = 1;
42-
while (i <= cols) { // generalized cols
33+
for (int i = 1; i <= cols; i++) { // generalized cols
4334
System.out.printf("%4d", n * i);
44-
i = i + 1;
4535
}
4636
System.out.println();
4737
}
4838

4939
public static void printTable2(int rows) {
50-
int i = 1;
51-
while (i <= rows) {
52-
printRow(i, rows); // added rows argument
53-
i = i + 1;
40+
for (int i = 1; i <= rows; i++) {
41+
printRow(i, rows);
5442
}
5543
}
5644

5745
public static void main(String[] args) {
58-
System.out.println("printRow()");
46+
System.out.println("\nprintRow()");
5947
printRow();
6048

61-
System.out.println("printRow(6)");
49+
System.out.println("\nprintRow(6)");
6250
printRow(6);
6351

64-
System.out.println("printTable()");
52+
System.out.println("\nprintTable()");
6553
printTable();
6654

67-
System.out.println("printTable(6)");
55+
System.out.println("\nprintTable(6)");
6856
printTable(6);
6957

70-
System.out.println("printRow(6, 6)");
58+
System.out.println("\nprintRow(6, 6)");
7159
printRow(6, 6);
7260

73-
System.out.println("printTable2(6)");
61+
System.out.println("\nprintTable2(6)");
7462
printTable2(6);
7563
}
7664

0 commit comments

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