Question

(20 pts) Fill in the missing code: This recursive method returns “even” if the length of...

  1. (20 pts) Fill in the missing code:

This recursive method returns “even” if the length of a give String is even, and “odd” if the length of the String is odd.

public static String foo(String s)

{

if (s.length() ==0)

   return “even”;

else if (s.length() = = 1)

   return “odd”;

else

    //your code goes here

}

  1. (40 pts) You coded the following in the file Test.java :

System.out.println( foo(5));

//more code here

public static int foo(int n) //line 9

{

if (n = = 0)

   return 1;

else

   System.out.println(n* foo(n-1) );

}                                    //line 15

At compile time, you get the following error:

Text.java: 15: missing return statement

}                                 //line 15

^

1 error

Explain what the problem is and how to fix it.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
public static String foo(String s)
{
    if (s.length() == 0)
        return "even";
    else if (s.length() == 1)
        return "odd";
    else
        return foo(s.substring(2));
}

/////////////////////////////////////////////////////////////////////////////

public static int foo(int n) //line 9
{
    if (n == 0)
        return 1;
    else
        return (n* foo(n-1));
}
Add a comment
Know the answer?
Add Answer to:
(20 pts) Fill in the missing code: This recursive method returns “even” if the length of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java class quiz need help~ This is the Backwards.java code. public class Backwards { /** *...

    Java class quiz need help~ This is the Backwards.java code. public class Backwards { /** * Program starts with this method. * * @param args A String to be printed backwards */ public static void main(String[] args) { if (args.length == 0) { System.out.println("ERROR: Enter a String on commandline."); } else { String word = args[0]; String backwards = iterativeBack(word); // A (return address) System.out.println("Iterative solution: " + backwards); backwards = recursiveBack(word); // B (return address) System.out.println("\n\nRecursive solution: " +...

  • Question 2: Levenshtein Take the recursive Java program Levenshtein.java and convert it to the equivalent C...

    Question 2: Levenshtein Take the recursive Java program Levenshtein.java and convert it to the equivalent C program. Tip: You have explicit permission to copy/paste the main method for this question, replacing instances of System.out.println with printf, where appropriate. Notes You can get the assert function from assert.h. Try running the Java program on the CS macOS machines. You can compile and run the program following the instructions provided in assignment 0. Assertions are disabled by default. You can enable assertions...

  • 10. Recursive Append Download the file AppendRec.java. Write your code inside the appendNTimes method and use...

    10. Recursive Append Download the file AppendRec.java. Write your code inside the appendNTimes method and use the main method to test your code. Submit the file AppendRec.java. There is no need to delete the main method, the autograder will only grade appendNTimes. The method appendNTimes is recursive and takes two arguments, a string and an integer. It returns the original string appended to the original string n times. The method signature is as follows public static String appendNTimes ( String...

  • (20 pts) Inside a method main, we see code like:      Airplane.foo3(34.6); From this, reconstruct the...

    (20 pts) Inside a method main, we see code like:      Airplane.foo3(34.6); From this, reconstruct the header of method foo3 (which belongs to class Airplane); make appropriate assumptions if necessary. Write the method header as your answer.                (20 pts) Inside method main, we see code like: Airplane a = new Airplane(); int n = a.foo4(“Hello”); From this, reconstruct the header of method foo4 (which belongs to class Airplane) Write the method header as your answer. (40 pts) You coded...

  • : (6 points) Typical hash function for a string key. Fill the missing line of code....

    : (6 points) Typical hash function for a string key. Fill the missing line of code. Th hash function should compute a polynomial of the form: s[0] + s[1] * 37 + s[2] * 372 + s[3] * 373 + + s[n-l] * 37", where s[i] is the it character of string s, for i = 0, . . ., n-1. The length of string s is n. si size_t hash(const string &s) 1f const int n- s.length); for (int...

  • For Questions 1-3: consider the following code: public class A { private int number; protected String...

    For Questions 1-3: consider the following code: public class A { private int number; protected String name; public double price; public A() { System.out.println(“A() called”); } private void foo1() { System.out.println(“A version of foo1() called”); } protected int foo2() { Sysem.out.println(“A version of foo2() called); return number; } public String foo3() { System.out.println(“A version of foo3() called”); Return “Hi”; } }//end class A public class B extends A { private char service; public B() {    super();    System.out.println(“B() called”);...

  • For Questions 1-3: consider the following code: public class A { private int number; protected String...

    For Questions 1-3: consider the following code: public class A { private int number; protected String name; public double price; public A() { System.out.println(“A() called”); } private void foo1() { System.out.println(“A version of foo1() called”); } protected int foo2() { Sysem.out.println(“A version of foo2() called); return number; } public String foo3() { System.out.println(“A version of foo3() called”); Return “Hi”; } }//end class A public class B extends A { private char service; public B() {   super();   System.out.println(“B() called”); } public...

  • JAVA Write a method that accepts a String as an argument. The method should use recursion...

    JAVA Write a method that accepts a String as an argument. The method should use recursion to display each individual character in the String. Then, modify the method you just wrote so it displays the String backwards. The following code does not correctly display the String backwards. It merely moves the first character of the String to the end: public static void displayCharacter(String s)    {        if(s.length() == 0)            return;        else       ...

  • b) Consider the following code. public static int f(int n) if (n == 1) return 0;...

    b) Consider the following code. public static int f(int n) if (n == 1) return 0; else if (n % 2 == 0). return g(n/2); else return g(n+1); public static int g(int n) int r = n % 3; if (r == 0) return f(n/3); else if (r == 1) return f(n+2); else return f(2 * n); // (HERE) public static void main(String[] args) { int x = 3; System.out.println(f(x)); (1) (5 points) Draw the call stack as it would...

  • I just need to add comment for the code blow. Pleas add comment for each method...

    I just need to add comment for the code blow. Pleas add comment for each method and class if comments left out. package exercise01; /*************************************************************************** * <p> This program demonstrates the technique of recursion and includes * recursive methods that are defined for a variety of mathematical * functions. *    * <br>A recursive method is one that directly or indirectly calls itself * and must include: * <br>(1) end case: stopping condition * which terminates/ends recursion * <br>(2) reduction:...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT