Recursion Practice Welcome back! In this lab, we will be reviewing recursion by practicing with some basic recursion problems. Objectives Increase familiarity with recursive logic by working through several recursive problems. Taking into consideration a few corner cases through analyzing the test cases. Using a regex expression that will remove punctuation. Getting Started This lab includes the following .java file: L4/ └── Recursion.java └── Main.java* *Main.java is a read-only file used for testing. It is not included in the starter jar. Here is the starter jar if you would like to code in a different environment: L4.jar. Please complete ALL functions. Make sure to read the description for each function carefully. Do not include any for or while loops in your methods. These can all be completed in a purely recursive style, so do it recursively! In the spirit of incremental development, implement each method one at a time, look at the test cases and take into consideration what is being tested, then uncomment the corresponding test code and see if it works. If not, try to determine what went wrong and try again. Lab Assignment In this lab, you will complete the following methods: - fib - mult - expt - isPalindrome - longestWordLength - dedupeChars 1. The Fibonacci Method TO DO: 1. Write this method so that it returns the Fibonacci number for any input integer n . The Fibonacci sequence begins with 0 and then 1 follows. All subsequent values are the sum of the previous two, for example: 0, 1, 1, 2, 3, 5, 8, 13. Complete the fib() method, which takes in an index, n, and returns the nth value in the sequence. Every number after the first two is the sum of the two preceding ones. For each index 0, 1, 2, 3, 4, 5, 6... the output is: 0, 1, 1, 2, 3, 5, 8... When you run this method, your output should look like this: Testing the fibonacci method fib(0) should be 0 -> 0 fib(1) should be 1 -> 1 fib(2) should be 1 -> 1 fib(5) should be 5 -> 5 fib(10) should be 55 -> 55 fib(13) should be 233 -> 233 2. The Multiplication Method TO DO: 1. Write a multiplication method recursively using repeated addition. Do not use the multiplication operator or a loop. When you run this method, your output should look like this: Testing out the multiplication method mult(8, 2) should be 16 -> 16 mult(2, 8) should be 16 -> 16 mult(-2, -8) should be 16 -> 16 mult(4, -3) should be -12 -> -12 mult(-3, 4) should be -12 -> -12 3. The Exponent Method TO DO: 1. Write a method that computes j^k. Do not use Math.pow() or a loop. When you run this method, your output should look like this: Testing out the exponent method expt(2, 5) should be 32 -> 32 expt(5, 2) should be 25 -> 25 4. The isPalindrome Method TO DO: 1. Write a method that checks to see if a word is a palindrome. This should be case-independent. When you run this method, your output should look like this: Testing out the palindrome method isPalindrome("a") should be true -> true isPalindrome("Aibohphobia") should be true -> true isPalindrome("noon") should be true -> true isPalindrome("Recursion") should be false -> false 5. The Longest Word Length Method TO DO: 1. Write a method that returns length of the longest word in the given String using recursion (no loops). Hint: a Scanner may be helpful for finding word boundaries. After delimiting by space, use the following method on your String to remove punctuation .replaceAll("[^a-zA-Z]", "") If you use a Scanner, you will need a helper method to do the recursion on the Scanner object. When you run this method, your output should look like this: Testing out the longestWordLength method The longest word in the following quote: Grit, one of the keys to success. The person who perseveres is the one who will surely win. Success does not come from giving up, it comes from believing in yourself and continuously working towards the realization of a worthy ideal. Do not ever give up on what you want most. You know what you truly want. Believe in your dreams and goals and take daily consistent action in order to make your dreams a reality. should be 12 -> 12 The longest word in the following quote: Try to be like the turtle – at ease in your own shell. should be 6 -> 6 6. The Remove Duplicates Method TO DO: 1. Write a method to remove consecutive duplicate characters from a String. If two or more consecutive duplicate characters have different cases, then the first letter should be kept. When you run this method, your output should look like this: Testing the dedupeChars method dedupeChars("a") should be a -> a dedupeChars("aa") should be a -> a dedupeChars("MiSsisSiPpi") should be MiSisiPi -> MiSisiPi dedupeChars("swimMmMming") should be swiming -> swiming
Recursion Practice
Welcome back! In this lab, we will be reviewing recursion by practicing with some basic recursion problems.
Objectives
- Increase familiarity with recursive logic by working through several recursive problems.
- Taking into consideration a few corner cases through analyzing the test cases.
- Using a regex expression that will remove punctuation.
Getting Started
This lab includes the following .java file:
L4/
└── Recursion.java
└── Main.java*
*Main.java is a read-only file used for testing. It is not included in the starter jar.
Here is the starter jar if you would like to code in a different environment: L4.jar.
Please complete ALL functions. Make sure to read the description for each function carefully.
Do not include any for or while loops in your methods. These can all be completed in a purely recursive style, so do it recursively!
In the spirit of incremental development, implement each method one at a time, look at the test cases and take into consideration what is being tested, then uncomment the corresponding test code and see if it works. If not, try to determine what went wrong and try again.
Lab Assignment
In this lab, you will complete the following methods:
- fib
- mult
- expt
- isPalindrome
- longestWordLength
- dedupeChars
1. The Fibonacci Method
TO DO:
1. Write this method so that it returns the Fibonacci number for any input integer n .
The Fibonacci sequence begins with 0 and then 1 follows. All subsequent values are the sum of the previous two, for example: 0, 1, 1, 2, 3, 5, 8, 13. Complete the fib() method, which takes in an index, n, and returns the nth value in the sequence. Every number after the first two is the sum of the two preceding ones.
For each index 0, 1, 2, 3, 4, 5, 6...
the output is: 0, 1, 1, 2, 3, 5, 8...
When you run this method, your output should look like this:
Testing the fibonacci method
fib(0) should be 0 -> 0
fib(1) should be 1 -> 1
fib(2) should be 1 -> 1
fib(5) should be 5 -> 5
fib(10) should be 55 -> 55
fib(13) should be 233 -> 233
2. The Multiplication Method
TO DO:
1. Write a multiplication method recursively using repeated addition.
Do not use the multiplication operator or a loop.
When you run this method, your output should look like this:
Testing out the multiplication method
mult(8, 2) should be 16 -> 16
mult(2, 8) should be 16 -> 16
mult(-2, -8) should be 16 -> 16
mult(4, -3) should be -12 -> -12
mult(-3, 4) should be -12 -> -12
3. The Exponent Method
TO DO:
1. Write a method that computes j^k.
Do not use Math.pow() or a loop.
When you run this method, your output should look like this:
Testing out the exponent method
expt(2, 5) should be 32 -> 32
expt(5, 2) should be 25 -> 25
4. The isPalindrome Method
TO DO:
1. Write a method that checks to see if a word is a palindrome. This should be case-independent.
When you run this method, your output should look like this:
Testing out the palindrome method
isPalindrome("a") should be true -> true
isPalindrome("Aibohphobia") should be true -> true
isPalindrome("noon") should be true -> true
isPalindrome("Recursion") should be false -> false
5. The Longest Word Length Method
TO DO:
1. Write a method that returns length of the longest word in the given String using recursion (no loops).
Hint: a Scanner may be helpful for finding word boundaries. After delimiting by space, use the following method on your String to remove punctuation .replaceAll("[^a-zA-Z]", "") If you use a Scanner, you will need a helper method to do the recursion on the Scanner object.
When you run this method, your output should look like this:
Testing out the longestWordLength method
The longest word in the following quote:
Grit, one of the keys to success. The person who perseveres is the one who
will surely win. Success does not come from giving up, it comes from believing
in yourself and continuously working towards the realization of a worthy ideal.
Do not ever give up on what you want most. You know what you truly want.
Believe in your dreams and goals and take daily consistent action in order to
make your dreams a reality.
should be 12 -> 12
The longest word in the following quote:
Try to be like the turtle – at ease in your own shell.
should be 6 -> 6
6. The Remove Duplicates Method
TO DO:
1. Write a method to remove consecutive duplicate characters from a String.
If two or more consecutive duplicate characters have different cases, then the first letter should be kept.
When you run this method, your output should look like this:
Testing the dedupeChars method
dedupeChars("a") should be a -> a
dedupeChars("aa") should be a -> a
dedupeChars("MiSsisSiPpi") should be MiSisiPi -> MiSisiPi
dedupeChars("swimMmMming") should be swiming -> swiming
Trending now
This is a popular solution!
Step by step
Solved in 5 steps with 1 images