Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

//FIX dedupeChars method import java.util.Scanner; public class Recursion { public static int fib(int n) { if (n == 0) { return 0; } else if

//FIX dedupeChars method

import java.util.Scanner;

public class Recursion {

public static int fib(int n) { if (n == 0) { return 0; } else if (n == 1) { return 1; } else { return fib(n-1) + fib(n-2); } } public static int mult(int m, int n) { if (n == 0) { return 0; } else if (n > 0) { return m + mult(m, n-1); } else { return -mult(m, -n); } } public static int expt(int j, int k) { if (k == 0) { return 1; } else { return j * expt(j, k-1); } } public static boolean isPalindrome(String s) { s = s.toLowerCase(); if (s.length()

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

// THE ERROR THAT NEEDS TO BE FIXED:

image text in transcribed

Testing the dedupeChars method dedupeChars("a") should be a > a dedupeChars ("aa") should be a a a dedupeChars("MiSsissippi") should be MiSisipi > MiSsisSiPpi dedupeChars ("swimMmMming") should be swiming swimMmMming

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

Inductive Databases And Constraint Based Data Mining

Authors: Saso Dzeroski ,Bart Goethals ,Pance Panov

2010th Edition

1489982175, 978-1489982179

More Books

Students also viewed these Databases questions

Question

design a simple disciplinary and grievance procedure.

Answered: 1 week ago