Skip to content

Commit 567f0af

Browse files
committed
ch 18
1 parent 8990de3 commit 567f0af

6 files changed

+179
-3
lines changed

src/ch_18/Exercise18_10.java

+6-3
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,13 @@ public static void main(String[] args) {
2727

2828
public static int count(String str, char a) {
2929
if (str.length() > 0) {
30-
count += str.charAt(str.length() - 1) == a ? 1 : 0;
31-
count(str.substring(0, str.length() - 1), a);
30+
if (str.charAt(str.length() - 1) == a) {
31+
return 1 + count(str.substring(0, str.length() - 1), a);
32+
} else {
33+
return count(str.substring(0, str.length() - 1), a);
34+
}
3235
}
33-
return count;
36+
return 0;
3437

3538
}
3639
}

src/ch_18/Exercise18_13.java

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package ch_18;
2+
3+
import java.util.Scanner;
4+
5+
/**
6+
* *18.13 (Find the largest number in an array) Write a recursive method that returns the
7+
* largest integer in an array.
8+
* <p>
9+
* Write a test program that prompts the user to enter a list of eight integers and displays the largest element.
10+
*/
11+
public class Exercise18_13 {
12+
static int currIdx = 0;
13+
14+
public static void main(String[] args) {
15+
int[] arr = new int[8];
16+
Scanner in = new Scanner(System.in);
17+
System.out.println("Enter a list of eight integers: ");
18+
for (int i = 0; i < 8; i++) {
19+
arr[i] = in.nextInt();
20+
}
21+
in.close();
22+
System.out.println("The largest integer in the list is " + largestInteger(Integer.MIN_VALUE, arr));
23+
24+
25+
}
26+
27+
static int largestInteger(int large, int[] integers) {
28+
if (currIdx == integers.length) {
29+
return large;
30+
}
31+
large = Math.max(large, integers[currIdx++]);
32+
return largestInteger(large, integers);
33+
}
34+
}

src/ch_18/Exercise18_14.java

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package ch_18;
2+
3+
import java.util.Scanner;
4+
5+
/**
6+
* *18.14 (Find the number of uppercase letters in a string)
7+
* Write a recursive method to return the number of uppercase letters in a string.
8+
* <p>
9+
* Write a test program that prompts the user to enter a string and displays the number of uppercase letters in
10+
* the string.
11+
*/
12+
public class Exercise18_14 {
13+
public static void main(String[] args) {
14+
Scanner in = new Scanner(System.in);
15+
System.out.print("Enter a String: ");
16+
String s = in.nextLine().trim();
17+
System.out.println("The number of uppercase letters in " + s + " is: " + numUpperCase(s, 0, 0));
18+
19+
}
20+
21+
static int numUpperCase(String str, int idx, int count) {
22+
if (idx == str.length()) {
23+
return count;
24+
}
25+
if (Character.isUpperCase(str.charAt(idx))) {
26+
count++;
27+
}
28+
return numUpperCase(str, ++idx, count);
29+
}
30+
}

src/ch_18/Exercise18_15.java

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package ch_18;
2+
3+
import java.util.Scanner;
4+
5+
/**
6+
* *18.15 (Occurrences of a specified character in a string) Rewrite Programming Exercise 18.10 using a helper
7+
* method to pass the substring high index to the method.
8+
* The helper method header is:
9+
* public static int count(String str, char a, int high)
10+
*/
11+
public class Exercise18_15 {
12+
public static void main(String[] args) {
13+
Scanner in = new Scanner(System.in);
14+
System.out.print("Enter a String and a character to count it's occurrences:");
15+
String str = in.next();
16+
char ch = in.next().charAt(0);
17+
18+
System.out.println("Character " + ch + " occurs " + count(str, ch) + " times in " + str);
19+
in.close();
20+
}
21+
22+
public static int count(String str, char a, int high) {
23+
if (high > 0) {
24+
return str.charAt(high) == a ? (1 + count(str, a, high - 1)) : (count(str, a, high - 1));
25+
} else {
26+
return 0;
27+
}
28+
}
29+
30+
public static int count(String str, char a) {
31+
return count(str, a, str.length() - 1);
32+
}
33+
}

src/ch_18/Exercise18_16.java

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package ch_18;
2+
3+
import java.util.Scanner;
4+
5+
/**
6+
* *18.16 (Find the number of uppercase letters in an array) Write a recursive method
7+
* to return the number of uppercase letters in an array of characters. You need to
8+
* define the following two methods. The second one is a recursive helper method.
9+
* public static int count(char[] chars)
10+
* public static int count(char[] chars, int high)
11+
* Write a test program that prompts the user to enter a list of characters in one line
12+
* and displays the number of uppercase letters in the list.
13+
*/
14+
public class Exercise18_16 {
15+
public static void main(String[] args) {
16+
Scanner in = new Scanner(System.in);
17+
System.out.print("Enter a list of characters in one line: ");
18+
19+
String line = in.nextLine();
20+
char[] chars = line.toCharArray();
21+
22+
System.out.println("The number of uppercase letters in the list is: " + count(chars));
23+
in.close();
24+
}
25+
26+
public static int count(char[] chars) {
27+
return count(chars, chars.length - 1);
28+
}
29+
30+
public static int count(char[] chars, int high) {
31+
if (high > 0) {
32+
return Character.isUpperCase(chars[high]) ? (1 + count(chars, high - 1)) : (count(chars, high - 1));
33+
} else {
34+
return 0;
35+
}
36+
37+
}
38+
}

src/ch_18/Exercise18_17.java

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package ch_18;
2+
3+
import java.util.Scanner;
4+
5+
/**
6+
* *18.17 (Occurrences of a specified character in an array) Write a recursive method that
7+
* finds the number of occurrences of a specified character in an array. You need to
8+
* define the following two methods. The second one is a recursive helper method.
9+
* public static int count(char[] chars, char ch)
10+
* public static int count(char[] chars, char ch, int high)
11+
* Write a test program that prompts the user to enter a list of characters in one line,
12+
* and a character, and displays the number of occurrences of the character in the list.
13+
*/
14+
public class Exercise18_17 {
15+
public static void main(String[] args) {
16+
Scanner in = new Scanner(System.in);
17+
System.out.print("Enter a list of characters in one line: ");
18+
String line = in.nextLine();
19+
char[] chars = line.toCharArray();
20+
21+
System.out.println("Enter a single character: ");
22+
char ch = in.next().charAt(0);
23+
System.out.println("The character " + ch + " occurs " + count(chars, ch) + " times.");
24+
in.close();
25+
}
26+
27+
public static int count(char[] chars, char ch) {
28+
return count(chars, ch, chars.length - 1);
29+
}
30+
31+
public static int count(char[] chars, char ch, int high) {
32+
if (high > 0) {
33+
return chars[high] == ch ? (1 + count(chars, ch, high - 1)) : (count(chars, ch, high - 1));
34+
} else {
35+
return 0;
36+
}
37+
}
38+
}

0 commit comments

Comments
 (0)