Java Reference
In-Depth Information
A call of writeSquares(1); prints 1 . The method should throw an IllegalArgumentException if it is passed
a value less than 1.
7. Write a recursive method called writeChars that accepts an integer parameter n and that prints out a total of n char-
acters. The middle character of the output should always be an asterisk ( "*" ). If you are asked to write out an even
number of characters, then there will be two asterisks in the middle ( "**" ). Before the asterisk(s) you should write
out less-than characters ( "<" ). After the asterisk(s) you should write out greater-than characters ( ">" ). Your method
should throw an IllegalArgumentException if it is passed a value less than 1. For example, the following calls
produce the following output:
Method call Output produced
----------------------------------------
writeChars(1); *
writeChars(2); **
writeChars(3);
<*>
writeChars(4);
<**>
writeChars(5);
<<*>>
writeChars(6);
<<**>>
writeChars(7);
<<<*>>>
writeChars(8);
<<<**>>>
8. Write a recursive method called multiplyEvens that returns the product of the first n even integers. For example,
multiplyEvens(1) returns 2 and multiplyEvens(4) returns 384 (because 2 * 4 * 6 * 8 = 384). The method
should throw an IllegalArgumentException if it is passed a value less than or equal to 0.
9. Write a recursive method called sumTo that accepts an integer parameter n and returns a real number representing
the sum of the first n reciprocals. In other words, sumTo(n) returns (1 + 1/2 + 1/3 + 1/4 + ... + 1/ n ). For example,
sumTo(2) should return 1.5 . The method should return 0.0 if it is passed the value 0 and throw an
IllegalArgumentException if it is passed a value less than 0.
10. Write a recursive method called repeat that accepts a string s and an integer n as parameters and that returns s
concatenated together n times. For example, repeat("hello", 3) returns "hellohellohello" , and
repeat("ok", 1) returns "ok" , and repeat("bye", 0) returns "" . String concatenation is an expensive opera-
tion, so for an added challenge try to solve this problem while performing fewer than n concatenations.
11. Write a recursive method called isReverse that accepts two strings as parameters and returns true if the two
strings contain the same sequence of characters as each other but in the opposite order (ignoring capitalization), and
false otherwise. For example, the call of isReverse("hello", "eLLoH") would return true . The empty string,
as well as any one-letter string, is considered to be its own reverse.
12. Write a recursive method called indexOf that accepts two strings as parameters and that returns the starting index
of the first occurrence of the second string inside the first string (or
1 if not found). For example, the call of
indexOf("Barack Obama", "bam") would return 8. (Strings already have an indexOf method, but you may
not call it in your solution.)
13. Write a recursive method called evenDigits that accepts an integer parameter and that returns the integer formed
by removing the odd digits from it. For example, evenDigits(8342116) returns 8426 and evenDigits(-34512)
returns
42. If the number is 0 or has no even digits, such as 35159 or 7, return 0. Leading zeros in the result should
be ignored.
Search WWH ::




Custom Search