Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

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

image text in transcribed

image text in transcribed

image text in transcribed

Provided code:

Recursion class:

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 class:

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")); } }

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 00 fib(1) should be 11 fib(2) should be 11 fib (5) should be 55 fib(10) should be 5555 fib(13) should be 233233 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 1616 mult (2,8) should be 1616 mult (2,8) should be 1616 mult (4,3) should be 1212 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 1616 mult (2,8) should be 1616 mult (2,8) should be 1616 mult (4,3) should be 1212 mult (3,4) should be 1212 3. The Exponent Method TO DO: 1. Write a method that computes jk. 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 3232 expt(5,2) should be 2525 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). 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("[Aa-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 1212 The longest word in the following quote: Try to be like the turtle - at ease in your own shell. should be 66 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 aa dedupechars("MississiPpi") should be MiSisiPi > MisisiPi dedupechars ("swimmmMming") should be swiming swiming

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

Define Administration?

Answered: 1 week ago