Skip to content

Commit c4817f0

Browse files
Comments made for each loop statement
1 parent ce03ee9 commit c4817f0

File tree

2 files changed

+22
-13
lines changed

2 files changed

+22
-13
lines changed

.DS_Store

6 KB
Binary file not shown.

ch08/Fruit.java

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,44 @@
11
/**
22
* Fruit exercise.
33
*/
4-
public class Fruit {
54

5+
/*
6+
The purpose of this exercise is to practice reading code and recognizing the traversal
7+
patterns in this chapter. The following methods are hard to read, because instead of using
8+
meaningful names for the variables and methods, they use names of fruit.
9+
For each method, write one sentence that describes what the method does, without getting
10+
into the details of how it works. For each variable, identify the role it plays.
11+
*/
12+
public class Fruit {
13+
//Int method defining an int array named "a". one param
614
public static int banana(int[] a) {
715
int kiwi = 1;
816
int i = 0;
17+
//While 'i' is less than the array length
918
while (i < a.length) {
10-
kiwi = kiwi * a[i];
11-
i++;
19+
kiwi = kiwi * a[i]; //array counting by 1
20+
i++;// allows i to traverse through the index by 1
1221
}
13-
return kiwi;
22+
return kiwi; // ==3
1423
}
15-
24+
//Two params. Int method defining an int array named "a". param for an int variable # of grapes
1625
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;
26+
for (int i = 0; i < a.length; i++) { // As long as 'i' is below the array length run the code below.
27+
if (a[i] == grape) { // When the index i is equal to grape
28+
return i; //return i's index
2029
}
2130
}
22-
return -1;
31+
return -1; // return once the loop is complete
2332
}
24-
33+
// Two params. Int method defining an int array named "a". param for an int variable # of apple
2534
public static int pineapple(int[] a, int apple) {
2635
int pear = 0;
27-
for (int pine: a) {
36+
for (int pine: a) { //for each pine value in the a[] run this loop
2837
if (pine == apple) {
29-
pear++;
38+
pear++; //pear adds one every time the if statement is true
3039
}
3140
}
32-
return pear;
41+
return pear; //return the new value after the loop is done
3342
}
3443

3544
}

0 commit comments

Comments
 (0)