Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please write the code for method 5 and 6 This is the code I have so far but am getting errors with longestWordLength method and

Please write the code for method 5 and 6

image text in transcribed

This is the code I have so far but am getting errors with longestWordLength method and dedupeChars method

import java.util.Scanner;

/** * Recitation created by Gareth Halladay, 08/17. * Content was gathered from two sources: *

    *
  • http://www.cs.wustl.edu/~kjg/cse131/modules/recursion/lab.html *
  • http://codingbat.com/prob/p273235?parent=/home/bono@usc.edu/recursionLab *
* */ 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){ if (n == 0) { return 0; } if (n == 1) { return 1; } return fib(n - 1) + fib(n - 2);

}

/** * 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){ if (k == 0) { return 0; } if (k

}

/** * 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){ if (k == 0){ return 1; } if (k == 1) { return j; } return mult(j, expt(j, k - 1)); }

/** * 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){ int n = word.length(); word = word.toLowerCase(); for (int i = 0; i

/** * 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){ String [] s = words.split("\\words+"); int max = 0; for (String word : s) max = Math.max(max, word.length()); return max; }

/** * 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){ StringBuilder sb = new StringBuilder(); for (int i = 0; i

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

} }

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 to Expert-Tailored 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

Recommended Textbook for

Essentials of Database Management

Authors: Jeffrey A. Hoffer, Heikki Topi, Ramesh Venkataraman

1st edition

133405680, 9780133547702 , 978-0133405682

More Books

Students also viewed these Databases questions