CS113 Lab 10: Recursion excursion
Due Thursday, December 1st, before midnight
Goals
-
Practice writing recursive functions
Recursive functions
In the file, Recursion.java
, implement each of the following functions
recursively. You are given basecode containing
functions stubs along with a main
for testing. You can download the
basecode here or use the file in Dropbox.
You must not modify the function parameters and return types for the given functions! BUT you can add additional functions. For example, when working with arrays, define a recursive function that takes an index (like the examples from class). |
-
Function
reven(int n)
should recursively sum the even numbers starting at 2 and ending at n. For example,reven(10)
should return 2+4+6+8+10 = 30. -
max of a list Function should recursively find the largest item in the list and return it. For example: the list
{5,20,-4,18}
has max of 20. -
Function
rrevStr(String word)
should return the reverse of a given string. For example: the reverse of"hello"
is"olleh"
. -
Function
rblastoff(int n)`should count down (print one number per line) from the given start number to 1, then print "blastoff!". For example, `rblastoff(10)
should count down from 10 to 1, then print "blastoff!". -
Function (
rgetMod()
) asks the user to enter an integer that is divisible by 3. Assume the user will enter an integer. If the given integer is not divisible by 3 (Hint: use%
to check), the function should recursively ask the user for another integer. This function should return the valid integer. Below is console output from runningrgetMod
.Enter a number: 4 Not divisible by 3! Enter a number: 6
rgetMod
should return the user’s number! -
Function (
rxerox(String phrase,int n)
) should generate and return a String containingn
copies of the item. For example,rxerox("paper", 4)
should return"paperpaperpaperpaper"
, andrxerox("2", 10)
should return"2222222222"
. -
Function
rraise(int n, int m)
returnsn
raised to the mth power. For example, callingrraise(6,2)
returns 36. -
Function
risPalindrome(String text)
returnstrue
if given text is a palindrome (same forwards and backwards) andfalse
otherwise. For example,risPalindrome("HANNAH")
should returntrue
. -
Function
raverage(int[] L)
returns the average of a list of integers. Hint: implement this using a recursive function that computes the sum. -
Function
rmax(int[] L)
returns the max value of a list of integers. -
Function
rreverse(int[] L)
reverses the contents of a list inline (e.g. without creating a copy of the list) -
Function
rcount(char x,String S)
returns how many timesx
is in the StringS
. For example,rcount("a", "aardvark")
should return 3.
Testing your functions
You are given test code in main
. Functions that return a value are tested using the
test
function. This function throws an exception if the function returns an incorrect value.
For example, suppose there is a bug in our function rrevStr
, running gives this error
java.lang.Exception: Test Failed: yolleh != olleh
at Recursion.test(Recursion.java:118)
at Recursion.main(Recursion.java:133)
The above error shows us that our function returned "yolleh" but should have returned the value "olleh". We can also see that the function we need to fix is called on line 133.
We encourage you to add your own tests! When your program is free of errors, it will produce the following output.
$ java RecursionSoln
5
4
3
2
1
blastoff
Enter an integer: 10
10 is not divisble by 3!
Enter an integer: -1
-1 is not divisble by 3!
Enter an integer: 22
22 is not divisble by 3!
Enter an integer: 33
SUCCESS: rgetMod 0.0 = 0.0
SUCCESS: reven 30.0 = 30.0
SUCCESS: reven 30.0 = 30.0
SUCCESS: reven 0.0 = 0.0
SUCCESS: rrevStr olleh = olleh
SUCCESS: rrevStr A = A
SUCCESS: rrevStr =
SUCCESS: rrevStr HANNAH = HANNAH
SUCCESS: rxerox lalalala = lalalala
SUCCESS: rxerox =
SUCCESS: rraise 8.0 = 8.0
SUCCESS: rraise 1.0 = 1.0
SUCCESS: risPalindrome true = true
SUCCESS: risPalindrome false = false
SUCCESS: risPalindrome true = true
SUCCESS: risPalindrome true = true
SUCCESS: risPalindrome true = true
SUCCESS: rcount 2.0 = 2.0
SUCCESS: rcount 0.0 = 0.0
SUCCESS: raverage 6.25 = 6.25
SUCCESS: raverage 0.0 = 0.0
SUCCESS: rmax 10.0 = 10.0
SUCCESS: rmax 10.0 = 10.0
SUCCESS: rmax 2.0 = 2.0
SUCCESS: rmax -1.0 = -1.0
SUCCESS: rreverse [8, 10, 5, 2] = [8, 10, 5, 2]
SUCCESS: rreverse [8, 5, 2] = [8, 5, 2]
SUCCESS: rreverse [] = []
What to hand-in
-
The program,
Recursion.java
-
Make sure your programs have a header containing your name, date, and description.
-
No writeup is necessary for this assignment
How to hand-in
-
Copy your files to your dropbox, into the folder called
A10
.