Skip to content

Commit 69c58c2

Browse files
committed
ch07 revisions
1 parent b42cec2 commit 69c58c2

3 files changed

Lines changed: 19 additions & 20 deletions

File tree

ch07/ArrayExamples.java

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public static void main(String[] args) {
3030
System.out.println(counts[i]);
3131
}
3232

33+
// displaying arrays
3334
int[] array = {1, 2, 3, 4};
3435
printArray(array);
3536

@@ -49,9 +50,9 @@ public static void main(String[] args) {
4950
// copying with Arrays class
5051
double[] c = Arrays.copyOf(a, a.length);
5152

52-
// traversal
53+
// traversing arrays
5354
for (int i = 0; i < a.length; i++) {
54-
a[i] = Math.pow(a[i], 2.0);
55+
a[i] *= a[i];
5556
}
5657

5758
// search
@@ -66,33 +67,33 @@ public static void main(String[] args) {
6667
/**
6768
* Prints the elements of an array.
6869
*/
69-
public static void printArray(int[] array) {
70-
System.out.print("{" + array[0]);
71-
for (int i = 1; i < array.length; i++) {
72-
System.out.print(", " + array[i]);
70+
public static void printArray(int[] a) {
71+
System.out.print("{" + a[0]);
72+
for (int i = 1; i < a.length; i++) {
73+
System.out.print(", " + a[i]);
7374
}
7475
System.out.println("}");
7576
}
7677

7778
/**
7879
* Returns the index of the target in the array, or -1 if not found.
7980
*/
80-
public static int search(double[] a, double target) {
81-
for (int i = 0; i < a.length; i++) {
82-
if (a[i] == target) {
81+
public static int search(double[] array, double target) {
82+
for (int i = 0; i < array.length; i++) {
83+
if (array[i] == target) {
8384
return i;
8485
}
8586
}
86-
return -1;
87+
return -1; // not found
8788
}
8889

8990
/**
9091
* Returns the total of the elements in an array.
9192
*/
92-
public static double sum(double[] a) {
93+
public static double sum(double[] array) {
9394
double total = 0.0;
94-
for (int i = 0; i < a.length; i++) {
95-
total += a[i];
95+
for (int i = 0; i < array.length; i++) {
96+
total += array[i];
9697
}
9798
return total;
9899
}

ch07/Doubloon.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Example from the end of Chapter 7.
33
*/
44
public class Doubloon {
5-
5+
66
public static boolean isDoubloon(String s) {
77

88
// count the number of times each letter appears
@@ -21,10 +21,9 @@ public static boolean isDoubloon(String s) {
2121
}
2222
return true;
2323
}
24-
24+
2525
public static void main(String[] args) {
26-
System.out.println(isDoubloon("Mama"));
27-
System.out.println(isDoubloon("Lama"));
26+
System.out.println(isDoubloon("Mama")); // true
27+
System.out.println(isDoubloon("Lama")); // false
2828
}
29-
3029
}

ch07/Histogram.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ public static int inRange(int[] a, int low, int high) {
3131
}
3232

3333
public static void main(String[] args) {
34-
int numValues = 8;
35-
int[] array = randomArray(numValues);
34+
int[] array = randomArray(8);
3635
ArrayExamples.printArray(array);
3736

3837
int[] scores = randomArray(30);

0 commit comments

Comments
 (0)