isPalindrome(“”a””));
System.out.printf(“”isPalindrome(“”Aibohphobia””) should be true -> %bn””

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 ALLfunctions. 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

Thank you for your time!

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 -> %dn”, fib(0));
System.out.printf(“fib(1) should be 1 -> %dn”, fib(1));
System.out.printf(“fib(2) should be 1 -> %dn”, fib(2));
System.out.printf(“fib(5) should be 5 -> %dn”, fib(5));
System.out.printf(“fib(10) should be 55 -> %dn”, fib(10));
System.out.printf(“fib(13) should be 233 -> %dn”, fib(13));
System.out.println();

Recursion.Java

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){
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 -> %dn”, fib(0));
System.out.printf(“fib(1) should be 1 -> %dn”, fib(1));
System.out.printf(“fib(2) should be 1 -> %dn”, fib(2));
System.out.printf(“fib(5) should be 5 -> %dn”, fib(5));
System.out.printf(“fib(10) should be 55 -> %dn”, fib(10));
System.out.printf(“fib(13) should be 233 -> %dn”, fib(13));
System.out.println();

System.out.println(“Testing out the multiplication method”);
System.out.printf(“mult(8, 2) should be 16 -> %dn”, mult(8, 2));
System.out.printf(“mult(2, 8) should be 16 -> %dn”, mult(2, 8));
System.out.printf(“mult(4, -3) should be -12 -> %dn”, mult(4, -3));
System.out.printf(“mult(-3, 4) should be -12 -> %dn”, mult(-3, 4));
System.out.println();

System.out.println(“Testing out the exponent method”);
System.out.printf(“expt(2, 5) should be 32 -> %dn”, expt(2, 5));
System.out.printf(“expt(5, 2) should be 25 -> %dn”, expt(5, 2));
System.out.println();

System.out.println(“Testing out the palindrome method”);
System.out.printf(“isPalindrome(“a””) should be true -> %bn””