Download Define a class called Rektang

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Practice Recursion
1. Implement a recursive method palindrome that takes a string as a parameter and returns true if the
string is a palindrome and false otherwise.
2. Implement a recursive method fib that takes an integer num as a parameter and returns the nth Fibonacci
number. Fib sequence is 1, 2, 3, 5, 8, 13, …
3. Implement a recursive method reversal that takes an integer num as a parameter and prints its digits in
reverse order on one line. Try to see if you can also print it one digit per line.
4. Implement a recursive method mult that takes two integer parameters, x and y and returns their product.
(note: remember that multiplication is really multiple additions)
5. Implement a recursive method repete that takes as parameters a String s and an integer i and returns a
String that has s repeated i times. For example, if the given string is "CS113" and the integer is 3 then
the return value would be "CS113 CS113 CS113 ".
Note that if the integer is 0, then the empty string "" should be returned.
6. Write a recursive method chain (that takes an integer parameter x. The method prints x asterisks,
followed by x exclamation points. If x = 5 we get as output: *****!!!!!
7. Write a method show with one positive int parameter called n. The method will write 2 n-1 integers.
Here are the patterns of output for various values of n:
n=1: Output is: 1
n=2: Output is: 1 2 1
n=3: Output is: 1 2 1 3 1 2 1
n=4: Output is: 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1
And so on. Note that the output for n always consists of the output for n-1, followed by n itself, followed
by a second copy of the output for n-1
8. Write a recursive method converts that takes a decimal integer and a base integer as parameters. It
converts the decimal format into the new base representation. Example: if the decimal is 28 and the base
is 2 the output would be: 1 1 1 0 0 (the binary equivalent)
Try problems 4, 5, and 3 first. Then do any of the others.
Related documents