Skip to content

Commit a60b1d1

Browse files
committed
review ch08..ch11
1 parent ae811e0 commit a60b1d1

14 files changed

+370
-135
lines changed

ch08/ArrayExamples.java

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import java.util.Arrays;
2-
import java.util.Random;
32

43
/**
54
* Demonstrates uses of arrays.
@@ -62,8 +61,6 @@ public static void main(String[] args) {
6261
// reduce
6362
double total = sum(a);
6463
System.out.println("total = " + total);
65-
66-
makeHistogram();
6764
}
6865

6966
/**
@@ -100,57 +97,4 @@ public static double sum(double[] a) {
10097
return total;
10198
}
10299

103-
/**
104-
* Returns an array of random integers.
105-
*/
106-
public static int[] randomArray(int size) {
107-
Random random = new Random();
108-
int[] a = new int[size];
109-
for (int i = 0; i < a.length; i++) {
110-
a[i] = random.nextInt(100);
111-
}
112-
return a;
113-
}
114-
115-
/**
116-
* Computes the number of array elements in [low, high).
117-
*/
118-
public static int inRange(int[] a, int low, int high) {
119-
int count = 0;
120-
for (int i = 0; i < a.length; i++) {
121-
if (a[i] >= low && a[i] < high) {
122-
count++;
123-
}
124-
}
125-
return count;
126-
}
127-
128-
/**
129-
* Example code related to histograms.
130-
*/
131-
public static void makeHistogram() {
132-
int numValues = 8;
133-
int[] array = randomArray(numValues);
134-
printArray(array);
135-
136-
int[] scores = randomArray(30);
137-
int a = inRange(scores, 90, 100);
138-
int b = inRange(scores, 80, 90);
139-
int c = inRange(scores, 70, 80);
140-
int d = inRange(scores, 60, 70);
141-
int f = inRange(scores, 0, 60);
142-
143-
// making a histogram
144-
int[] counts = new int[100];
145-
for (int i = 0; i < scores.length; i++) {
146-
int index = scores[i];
147-
counts[index]++;
148-
}
149-
150-
// histogram with enhanced for loop
151-
counts = new int[100];
152-
for (int score : scores) {
153-
counts[score]++;
154-
}
155-
}
156100
}

ch08/Fruit.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Fruit exercise.
3+
*/
4+
public class Fruit {
5+
6+
public static int banana(int[] a) {
7+
int kiwi = 1;
8+
int i = 0;
9+
while (i < a.length) {
10+
kiwi = kiwi * a[i];
11+
i++;
12+
}
13+
return kiwi;
14+
}
15+
16+
public static int grapefruit(int[] a, int grape) {
17+
for (int i = 0; i < a.length; i++) {
18+
if (a[i] == grape) {
19+
return i;
20+
}
21+
}
22+
return -1;
23+
}
24+
25+
public static int pineapple(int[] a, int apple) {
26+
int pear = 0;
27+
for (int pine: a) {
28+
if (pine == apple) {
29+
pear++;
30+
}
31+
}
32+
return pear;
33+
}
34+
35+
}

ch08/Histogram.java

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import java.util.Random;
2+
3+
/**
4+
* Example code related to histograms.
5+
*/
6+
public class Histogram {
7+
8+
/**
9+
* Returns an array of random integers.
10+
*/
11+
public static int[] randomArray(int size) {
12+
Random random = new Random();
13+
int[] a = new int[size];
14+
for (int i = 0; i < a.length; i++) {
15+
a[i] = random.nextInt(100);
16+
}
17+
return a;
18+
}
19+
20+
/**
21+
* Computes the number of array elements in [low, high).
22+
*/
23+
public static int inRange(int[] a, int low, int high) {
24+
int count = 0;
25+
for (int i = 0; i < a.length; i++) {
26+
if (a[i] >= low && a[i] < high) {
27+
count++;
28+
}
29+
}
30+
return count;
31+
}
32+
33+
public static void main(String[] args) {
34+
int numValues = 8;
35+
int[] array = randomArray(numValues);
36+
ArrayExamples.printArray(array);
37+
38+
int[] scores = randomArray(30);
39+
int a = inRange(scores, 90, 100);
40+
int b = inRange(scores, 80, 90);
41+
int c = inRange(scores, 70, 80);
42+
int d = inRange(scores, 60, 70);
43+
int f = inRange(scores, 0, 60);
44+
45+
// making a histogram
46+
int[] counts = new int[100];
47+
for (int i = 0; i < scores.length; i++) {
48+
int index = scores[i];
49+
counts[index]++;
50+
}
51+
52+
// histogram with enhanced for loop
53+
counts = new int[100];
54+
for (int score : scores) {
55+
counts[score]++;
56+
}
57+
}
58+
59+
}

ch08/MakeDubMus.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Stack diagram exercise.
3+
*/
4+
public class MakeDubMus {
5+
6+
public static int[] make(int n) {
7+
int[] a = new int[n];
8+
for (int i = 0; i < n; i++) {
9+
a[i] = i + 1;
10+
}
11+
return a;
12+
}
13+
14+
public static void dub(int[] jub) {
15+
for (int i = 0; i < jub.length; i++) {
16+
jub[i] *= 2;
17+
}
18+
}
19+
20+
public static int mus(int[] zoo) {
21+
int fus = 0;
22+
for (int i = 0; i < zoo.length; i++) {
23+
fus += zoo[i];
24+
}
25+
return fus;
26+
}
27+
28+
public static void main(String[] args) {
29+
int[] bob = make(5);
30+
dub(bob);
31+
System.out.println(mus(bob));
32+
}
33+
34+
}

ch09/Exercise.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Exercise on encapsulation and generalization.
3+
*/
4+
public class Exercise {
5+
6+
public static void main(String[] args) {
7+
String s = "((3 + 7) * 2)";
8+
int count = 0;
9+
10+
for (int i = 0; i < s.length(); i++) {
11+
char c = s.charAt(i);
12+
if (c == '(') {
13+
count++;
14+
} else if (c == ')') {
15+
count--;
16+
}
17+
}
18+
19+
System.out.println(count);
20+
}
21+
22+
}

ch09/Max.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
*/
66
public class Max {
77

8+
/**
9+
* Converts args to integers and prints the max.
10+
*/
811
public static void main(String[] args) {
912
System.out.println(Arrays.toString(args));
1013

@@ -15,5 +18,7 @@ public static void main(String[] args) {
1518
max = value;
1619
}
1720
}
21+
System.out.println("The max is " + max);
1822
}
23+
1924
}

ch09/Recurse.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Recursion exercise.
3+
*/
4+
public class Recurse {
5+
6+
/**
7+
* Returns the first character of the given String.
8+
*/
9+
public static char first(String s) {
10+
return s.charAt(0);
11+
}
12+
13+
/**
14+
* Returns all but the first letter of the given String.
15+
*/
16+
public static String rest(String s) {
17+
return s.substring(1);
18+
}
19+
20+
/**
21+
* Returns all but the first and last letter of the String.
22+
*/
23+
public static String middle(String s) {
24+
return s.substring(1, s.length() - 1);
25+
}
26+
27+
/**
28+
* Returns the length of the given String.
29+
*/
30+
public static int length(String s) {
31+
return s.length();
32+
}
33+
34+
}

ch09/StringsThings.java

Lines changed: 66 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,17 @@
33
*/
44
public class StringsThings {
55

6-
/**
7-
* Reverses a string, returns a new String.
8-
*/
9-
public static String reverse(String s) {
10-
String r = "";
11-
for (int i = s.length() - 1; i >= 0; i--) {
12-
r = r + s.charAt(i);
13-
}
14-
return r;
15-
}
16-
176
public static void main(String[] args) {
187

8+
// Characters
9+
10+
String fruit = "banana";
11+
char letter0 = fruit.charAt(0);
12+
13+
if (letter0 == 'a') {
14+
System.out.println('?');
15+
}
16+
1917
System.out.print("Roman alphabet: ");
2018
for (char c = 'A'; c <= 'Z'; c++) {
2119
System.out.print(c);
@@ -28,6 +26,47 @@ public static void main(String[] args) {
2826
}
2927
System.out.println();
3028

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
38+
39+
for (int i = 0; i < fruit.length(); i++) {
40+
char letter = fruit.charAt(i);
41+
System.out.println(letter);
42+
}
43+
44+
for (char letter : fruit.toCharArray()) {
45+
System.out.println(letter);
46+
}
47+
48+
int length = fruit.length();
49+
char last = fruit.charAt(length - 1); // correct
50+
51+
System.out.println(reverse(fruit));
52+
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+
63+
// The indexOf method
64+
65+
int index = fruit.indexOf('a');
66+
int index2 = fruit.indexOf('a', 2);
67+
68+
// String comparison
69+
3170
String name1 = "Alan Turing";
3271
String name2 = "Ada Lovelace";
3372
if (name1.equals(name2)) {
@@ -43,17 +82,24 @@ public static void main(String[] args) {
4382
System.out.println("name2 comes before name1.");
4483
}
4584

46-
String fruit = "banana";
85+
// Wrapper classes
4786

48-
for (int i = 0; i < fruit.length(); i++) {
49-
char letter = fruit.charAt(i);
50-
System.out.println(letter);
51-
}
87+
String str = "12345";
88+
int num = Integer.parseInt(str);
5289

53-
for (char letter : fruit.toCharArray()) {
54-
System.out.println(letter);
55-
}
90+
num = 12345;
91+
str = Integer.toString(num);
92+
}
5693

57-
System.out.println(reverse(fruit));
94+
/**
95+
* Reverses a string, returns a new String.
96+
*/
97+
public static String reverse(String s) {
98+
String r = "";
99+
for (int i = s.length() - 1; i >= 0; i--) {
100+
r = r + s.charAt(i);
101+
}
102+
return r;
58103
}
104+
59105
}

0 commit comments

Comments
 (0)