Question: Recursion.java import java.util.Scanner; public class Recursion { /** * Every number after the first two is the sum of the two preceding ones. * index:




Recursion.java
import java.util.Scanner;
public class Recursion {
/** * Every number after the first two is the sum of the two preceding ones. * index: 0, 1, 2, 3, 4, 5, 6... * output: 0, 1, 1, 2, 3, 5, 8... * @param n which fibonacci number to compute. * @return the fibonacci number. */ public static int fib(int n){ return 0; }
/** * Write a multiplication method recursively using repeated addition. * Do not use the multiplication operator or a loop. * * @param j a positive or negative integer. * @param k a positive or negative integer. * @return the product of the k and j. */ public static int mult(int j, int k){ return 0; }
/** * Write a method that computes j^k. * Do not use Math.pow() or a loop. * @param j a non-negative number * @param k a non-negative number * @return j^k */ public static int expt(int j, int k){ return 0; }
/** * Check to see if a word is a palindrome. Should be case-independent. * @param word a String without whitespace * @return true if the word is a palindrome */ public static boolean isPalindrome(String word){ return false; }
/** * 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 {@code .replaceAll("[^a-zA-Z]", "")} * If you use a Scanner, you will need a helper method to do the recursion on the Scanner object. * * @param words A String containing one or more words. * @return The length of the longest word in the String. * @see Scanner#Scanner(String) * @see Scanner#next() * @see String#split(String) * @see String#replaceAll(String, String) * @see Math#max(int, int) */ public static int longestWordLength(String words){ return 0; }
/** * Remove consecutive duplicate characters from a String. * Case should not matter, if two or more consecutive duplicate * characters have different cases, then the first letter should be kept. * @param word A word with possible consecutive duplicate characters. * @return A word without any consecutive duplicate characters. */ public static String dedupeChars(String word){ return null; }
public static void main(String [] args){ // Test your methods here! // Uncomment each block as you are ready to test it. //Note: in zyBooks the main in Main.java will run instead, and it is all of the below statements.
/* System.out.println("Testing the fibonacci method"); System.out.printf("fib(0) should be 0 -> %d ", fib(0)); System.out.printf("fib(1) should be 1 -> %d ", fib(1)); System.out.printf("fib(2) should be 1 -> %d ", fib(2)); System.out.printf("fib(5) should be 5 -> %d ", fib(5)); System.out.printf("fib(10) should be 55 -> %d ", fib(10)); System.out.printf("fib(13) should be 233 -> %d ", fib(13)); System.out.println();
System.out.println("Testing out the multiplication method"); System.out.printf("mult(8, 2) should be 16 -> %d ", mult(8, 2)); System.out.printf("mult(2, 8) should be 16 -> %d ", mult(2, 8)); System.out.printf("mult(4, -3) should be -12 -> %d ", mult(4, -3)); System.out.printf("mult(-3, 4) should be -12 -> %d ", mult(-3, 4)); System.out.println();
System.out.println("Testing out the exponent method"); System.out.printf("expt(2, 5) should be 32 -> %d ", expt(2, 5)); System.out.printf("expt(5, 2) should be 25 -> %d ", expt(5, 2)); System.out.println();
System.out.println("Testing out the palindrome method"); System.out.printf("isPalindrome(\"a\") should be true -> %b ", isPalindrome("a")); System.out.printf("isPalindrome(\"Aibohphobia\") should be true -> %b ", isPalindrome("Aibohphobia")); System.out.printf("isPalindrome(\"noon\") should be true -> %b ", isPalindrome("noon")); System.out.printf("isPalindrome(\"Recursion\") should be false -> %b ", isPalindrome("Recursion")); System.out.println();
System.out.println("Testing out the longestWordLength method "); String quoteOne = "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."; System.out.printf("The longest word in the following quote: %s should be 12 -> %d ", quoteOne, longestWordLength(quoteOne)); String quoteTwo = "Try to be like the turtle at ease in your own shell."; System.out.printf("The longest word in the following quote: %s should be 6 -> %d ", quoteTwo, longestWordLength(quoteTwo)); System.out.println();
System.out.println("Testing the dedupeChars method"); System.out.printf("dedupeChars(\"a\") should be a -> %s ", dedupeChars("a")); System.out.printf("dedupeChars(\"aa\") should be a -> %s ", dedupeChars("aa")); System.out.printf("dedupeChars(\"MiSsisSiPpi\") should be MiSisiPi -> %s ", dedupeChars("MiSsisSiPpi")); System.out.printf("dedupeChars(\"swimMmMming\") should be swiming -> %s ", dedupeChars("swimMmMming")); */ } }
Main.java
public class Main extends Recursion{
public static void main(String [] args){ //This is essentially the same as the main in Recursion.java //This tests each method implemented and is a read-only file.
System.out.println("Testing the fibonacci method"); System.out.printf("fib(0) should be 0 -> %d ", fib(0)); System.out.printf("fib(1) should be 1 -> %d ", fib(1)); System.out.printf("fib(2) should be 1 -> %d ", fib(2)); System.out.printf("fib(5) should be 5 -> %d ", fib(5)); System.out.printf("fib(10) should be 55 -> %d ", fib(10)); System.out.printf("fib(13) should be 233 -> %d ", fib(13)); System.out.println(); System.out.println("Testing out the multiplication method"); System.out.printf("mult(8, 2) should be 16 -> %d ", mult(8, 2)); System.out.printf("mult(2, 8) should be 16 -> %d ", mult(2, 8)); System.out.printf("mult(-2, -8) should be 16 -> %d ", mult(-2, -8)); System.out.printf("mult(4, -3) should be -12 -> %d ", mult(4, -3)); System.out.printf("mult(-3, 4) should be -12 -> %d ", mult(-3, 4)); System.out.println(); System.out.println("Testing out the exponent method"); System.out.printf("expt(2, 5) should be 32 -> %d ", expt(2, 5)); System.out.printf("expt(5, 2) should be 25 -> %d ", expt(5, 2)); System.out.println(); System.out.println("Testing out the palindrome method"); System.out.printf("isPalindrome(\"a\") should be true -> %b ", isPalindrome("a")); System.out.printf("isPalindrome(\"Aibohphobia\") should be true -> %b ", isPalindrome("Aibohphobia")); System.out.printf("isPalindrome(\"noon\") should be true -> %b ", isPalindrome("noon")); System.out.printf("isPalindrome(\"Recursion\") should be false -> %b ", isPalindrome("Recursion")); System.out.println();
System.out.println("Testing out the longestWordLength method "); String quoteOne = "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."; System.out.printf("The longest word in the following quote: %s should be 12 -> %d ", quoteOne, longestWordLength(quoteOne)); String quoteTwo = "Try to be like the turtle at ease in your own shell."; System.out.printf("The longest word in the following quote: %s should be 6 -> %d ", quoteTwo, longestWordLength(quoteTwo)); System.out.println();
System.out.println("Testing the dedupeChars method"); System.out.printf("dedupeChars(\"a\") should be a -> %s ", dedupeChars("a")); System.out.printf("dedupeChars(\"aa\") should be a -> %s ", dedupeChars("aa")); System.out.printf("dedupeChars(\"MiSsisSiPpi\") should be MiSisiPi -> %s ", dedupeChars("MiSsisSiPpi")); System.out.printf("dedupeChars(\"swimMmMming\") should be swiming -> %s ", dedupeChars("swimMmMming")); } }
Module 2: Lab 4 - Branching Recursion 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/ LRecursion.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 O 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 fib0 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 jak. 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 is Palindrome ("a") should be true -> true is Palindrome ("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. this thod, your output should look like this: When you a Testing the dedupechars method dedupeChars ("a") should be a -> dedupeChars ("aa") should be a -> a dedupeChars ("Mississippi") should be Misisipi -> MisisiPi dedupechars("swimmmmming") should be swiming -> swiming Module 2: Lab 4 - Branching Recursion 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/ LRecursion.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 O 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 fib0 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 jak. 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 is Palindrome ("a") should be true -> true is Palindrome ("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. this thod, your output should look like this: When you a Testing the dedupechars method dedupeChars ("a") should be a -> dedupeChars ("aa") should be a -> a dedupeChars ("Mississippi") should be Misisipi -> MisisiPi dedupechars("swimmmmming") should be swiming -> swiming
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
