Skip to content

Commit 353ee38

Browse files
committed
reviewed ch09
1 parent f484141 commit 353ee38

7 files changed

Lines changed: 143 additions & 134 deletions

File tree

appe/Tables.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* Generating tables.
3+
*/
4+
public class Tables {
5+
6+
public static void example() {
7+
int i = 1;
8+
while (i < 10) {
9+
double x = i;
10+
System.out.println(x + " " + Math.log(x));
11+
i = i + 1;
12+
}
13+
}
14+
15+
public static void example2() {
16+
int i = 1;
17+
while (i < 10) {
18+
double x = i;
19+
System.out.println(x + " " + Math.log(x) / Math.log(2));
20+
i = i + 1;
21+
}
22+
}
23+
24+
public static void example3() {
25+
final double LOG2 = Math.log(2);
26+
int i = 1;
27+
while (i < 100) {
28+
double x = i;
29+
System.out.println(x + " " + Math.log(x) / LOG2);
30+
i = i * 2;
31+
}
32+
}
33+
34+
public static void main(String[] args) {
35+
System.out.println("example");
36+
example();
37+
38+
System.out.println("example2");
39+
example2();
40+
41+
System.out.println("example3");
42+
example3();
43+
}
44+
45+
}
46+
47+
48+
public static void printTable4(int rows) {
49+
for (int i = 1; i <= rows; i = i + 1) {
50+
printRow3(i, rows);
51+
}
52+
}
53+
54+
public static void printRow4(int n, int cols) {
55+
int i;
56+
for (i = 1; i <= cols; i = i + 1) {
57+
System.out.printf("%4d", n * i);
58+
}
59+
System.out.println(i);
60+
}
61+

ch09/Max.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ public class Max {
1111
public static void main(String[] args) {
1212
System.out.println(Arrays.toString(args));
1313

14+
if (args.length == 0) {
15+
System.err.println("Usage: java Max <numbers>");
16+
return;
17+
}
18+
1419
int max = Integer.MIN_VALUE;
1520
for (String arg : args) {
1621
int value = Integer.parseInt(arg);

ch09/Objects.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import java.math.BigInteger;
2+
3+
/**
4+
* Demonstates uses of objects and wrappers.
5+
*/
6+
public class Objects {
7+
8+
public static void main(String[] args) {
9+
10+
// primitives vs objects
11+
12+
int number = -2;
13+
char symbol = '!';
14+
char[] array = {'c', 'a', 't'};
15+
String word = "dog";
16+
17+
// Strings are immutable
18+
19+
String name = "Alan Turing";
20+
String upperName = name.toUpperCase();
21+
22+
String text = "Computer Science is fun!";
23+
text = text.replace("Computer Science", "CS");
24+
25+
// Wrapper classes
26+
27+
Integer x = new Integer(123);
28+
Integer y = new Integer(123);
29+
if (x == y) { // false
30+
System.out.println("x and y are the same object");
31+
}
32+
if (x.equals(y)) { // true
33+
System.out.println("x and y have the same value");
34+
}
35+
36+
String str = "12345";
37+
int num = Integer.parseInt(str);
38+
39+
num = 12345;
40+
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);
53+
}
54+
55+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* Exercise on encapsulation and generalization.
33
*/
4-
public class Exercise {
4+
public class Parens {
55

66
public static void main(String[] args) {
77
String s = "((3 + 7) * 2)";
File renamed without changes.

ch09/Tables.java

Lines changed: 21 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,8 @@
11
/**
2-
* Generating tables; encapsulation and generalization.
2+
* Encapsulation and generalization.
33
*/
44
public class Tables {
55

6-
public static void example() {
7-
int i = 1;
8-
while (i < 10) {
9-
double x = i;
10-
System.out.println(x + " " + Math.log(x));
11-
i = i + 1;
12-
}
13-
}
14-
15-
public static void example2() {
16-
int i = 1;
17-
while (i < 10) {
18-
double x = i;
19-
System.out.println(x + " " + Math.log(x) / Math.log(2));
20-
i = i + 1;
21-
}
22-
}
23-
24-
public static void example3() {
25-
final double LOG2 = Math.log(2);
26-
int i = 1;
27-
while (i < 100) {
28-
double x = i;
29-
System.out.println(x + " " + Math.log(x) / LOG2);
30-
i = i * 2;
31-
}
32-
}
33-
34-
public static void example4() {
35-
int i = 1;
36-
while (i <= 6) {
37-
System.out.printf("%4d", 2 * i);
38-
i = i + 1;
39-
}
40-
System.out.println();
41-
}
42-
436
public static void printRow() {
447
int i = 1;
458
while (i <= 6) {
@@ -49,109 +12,66 @@ public static void printRow() {
4912
System.out.println();
5013
}
5114

52-
public static void printRow2(int n) {
15+
public static void printRow(int n) {
5316
int i = 1;
5417
while (i <= 6) {
55-
System.out.printf("%4d", n * i);
18+
System.out.printf("%4d", n * i); // generalized n
5619
i = i + 1;
5720
}
5821
System.out.println();
5922
}
6023

61-
public static void example5() {
62-
int i = 1;
63-
while (i <= 6) {
64-
printRow2(i);
65-
i = i + 1;
66-
}
67-
}
68-
6924
public static void printTable() {
7025
int i = 1;
7126
while (i <= 6) {
72-
printRow2(i);
27+
printRow(i);
7328
i = i + 1;
7429
}
7530
}
7631

77-
public static void printTable2(int rows) {
32+
public static void printTable(int rows) {
7833
int i = 1;
79-
while (i <= rows) {
80-
printRow2(i);
34+
while (i <= rows) { // generalized rows
35+
printRow(i);
8136
i = i + 1;
8237
}
8338
}
8439

85-
public static void printRow3(int n, int cols) {
40+
public static void printRow(int n, int cols) {
8641
int i = 1;
87-
while (i <= cols) {
42+
while (i <= cols) { // generalized cols
8843
System.out.printf("%4d", n * i);
8944
i = i + 1;
9045
}
9146
System.out.println();
9247
}
9348

94-
public static void printTable3(int rows) {
49+
public static void printTable2(int rows) {
9550
int i = 1;
9651
while (i <= rows) {
97-
printRow3(i, rows);
52+
printRow(i, rows); // added rows argument
9853
i = i + 1;
9954
}
10055
}
10156

102-
public static void printTable4(int rows) {
103-
for (int i = 1; i <= rows; i = i + 1) {
104-
printRow3(i, rows);
105-
}
106-
}
107-
108-
public static void printRow4(int n, int cols) {
109-
int i;
110-
for (i = 1; i <= cols; i = i + 1) {
111-
System.out.printf("%4d", n * i);
112-
}
113-
System.out.println(i);
114-
}
115-
11657
public static void main(String[] args) {
117-
System.out.println("example");
118-
example();
119-
120-
System.out.println("example2");
121-
example2();
122-
123-
System.out.println("example3");
124-
example3();
125-
126-
System.out.println("example4");
127-
example4();
128-
129-
System.out.println("example5");
130-
example5();
131-
132-
System.out.println("printRow");
58+
System.out.println("printRow()");
13359
printRow();
13460

135-
System.out.println("printRow2");
136-
printRow2(6);
61+
System.out.println("printRow(6)");
62+
printRow(6);
13763

138-
System.out.println("printTable");
64+
System.out.println("printTable()");
13965
printTable();
14066

141-
System.out.println("printTable2");
142-
printTable2(6);
143-
144-
System.out.println("printRow3");
145-
printRow3(6, 6);
146-
147-
System.out.println("printTable3");
148-
printTable3(6);
67+
System.out.println("printTable(6)");
68+
printTable(6);
14969

150-
System.out.println("printRow4");
151-
printRow4(6, 6);
70+
System.out.println("printRow(6, 6)");
71+
printRow(6, 6);
15272

153-
System.out.println("printTable4");
154-
printTable4(6);
73+
System.out.println("printTable2(6)");
74+
printTable2(6);
15575
}
15676

15777
}

ch09/Things.java

Lines changed: 0 additions & 32 deletions
This file was deleted.

0 commit comments

Comments
 (0)