Skip to content

Commit f484141

Browse files
committed
reviewed ch08
1 parent 9cc490e commit f484141

6 files changed

Lines changed: 54 additions & 45 deletions

File tree

appe/Digit.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* Tests whether x is a single digit integer.
3+
*
4+
* @param x the integer to test
5+
* @return true if x has one digit, false otherwise
6+
*/
7+
public static boolean isSingleDigit(int x) {
8+
if (x > -10 && x < 10) {
9+
return true;
10+
} else {
11+
return false;
12+
}
13+
}
14+
15+
16+
System.out.println(isSingleDigit(2));
17+
boolean bigFlag = !isSingleDigit2(17);
18+

ch08/CodingBat.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* CodingBat examples from Chapter 8.
3+
*/
4+
public class CodingBat {
5+
6+
public static String noX(String str) {
7+
if (str.length() == 0) {
8+
return "";
9+
}
10+
char c = str.charAt(0);
11+
if (c == 'x') {
12+
return noX(str.substring(1));
13+
} else {
14+
return c + noX(str.substring(1));
15+
}
16+
}
17+
18+
public int array11(int[] nums, int index) {
19+
if (index >= nums.length) {
20+
return 0;
21+
}
22+
if (nums[index] == 11) {
23+
return 1 + array11(nums, index + 1);
24+
} else {
25+
return array11(nums, index + 1);
26+
}
27+
}
28+
29+
}
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
public class Recursive {
1+
/**
2+
* Stack diagram exercise.
3+
*/
4+
public class Exercise {
25

36
public static void main(String[] args) {
47
System.out.println(prod(1, 4));
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Recursion exercise.
2+
* Palindrome exercise.
33
*/
44
public class Recurse {
55

ch08/Recursion.java

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,11 @@ public static void main(String[] args) {
44
System.out.println("countdown");
55
countdown(3);
66

7-
System.out.println("countup");
8-
countup(3);
9-
10-
System.out.println("newLine");
11-
newLine();
12-
137
System.out.println("nLines");
148
nLines(3);
159

16-
System.out.println("threeLine");
17-
threeLine();
10+
System.out.println("countup");
11+
countup(3);
1812

1913
System.out.println("displayBinary");
2014
displayBinary(23);
@@ -34,12 +28,6 @@ public static void newLine() {
3428
System.out.println();
3529
}
3630

37-
public static void threeLine() {
38-
newLine();
39-
newLine();
40-
newLine();
41-
}
42-
4331
public static void nLines(int n) {
4432
if (n > 0) {
4533
System.out.println();

ch08/Series.java

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,7 @@
33
*/
44
public class Series {
55

6-
public static void countup(int n) {
7-
if (n == 0) {
8-
System.out.println("Blastoff!");
9-
} else {
10-
countup(n - 1);
11-
System.out.println(n);
12-
}
13-
}
14-
15-
/**
16-
* Tests whether x is a single digit integer.
17-
*
18-
* @param x the integer to test
19-
* @return true if x has one digit, false otherwise
20-
*/
216
public static boolean isSingleDigit(int x) {
22-
if (x > -10 && x < 10) {
23-
return true;
24-
} else {
25-
return false;
26-
}
27-
}
28-
29-
public static boolean isSingleDigit2(int x) {
307
return x > -10 && x < 10;
318
}
329

@@ -48,12 +25,6 @@ public static int fibonacci(int n) {
4825

4926
public static void main(String[] args) {
5027

51-
countup(3);
52-
System.out.println("Have a nice day.");
53-
54-
System.out.println(isSingleDigit(2));
55-
boolean bigFlag = !isSingleDigit2(17);
56-
5728
int z = 9;
5829
if (isSingleDigit(z)) {
5930
System.out.println("z is small");

0 commit comments

Comments
 (0)