Question

Consider the recursive function pingPong described below. static String pingPong(int x) { if (x % 2...

Consider the recursive function pingPong described below.

static String pingPong(int x) {
  if (x % 2 == 0 && x % 3 == 0)
    return "ping-pong";
  else if (x % 2 == 0)
    return "ping," + pingPong(x - 1);
  else if (x % 3 == 0)
    return "pong," + pingPong(x - 1);
  else
    return "?," + pingPong(x - 1);
}

a) what's the base-case of pingPong?

b) what's the result of pingPong(10)?

Homework Answers

Answer #1

a) what's the base-case of pingPong?

In the given function, the base case is:

  if (x % 2 == 0 && x % 3 == 0)
    return "ping-pong";

It is called the base case because it terminates the recursion by not making any further recursive calls and provides a suitable output.

b) what's the result of pingPong(10)?

The output of pingPong(10) will be:

ping,pong,ping,?,ping-pong

The code with corresponding output is as follows:

import java.util.*;
public class ping{
    public static String pingPong(int x) {
          if (x % 2 == 0 && x % 3 == 0)
                return "ping-pong";
          else if (x % 2 == 0)
                return "ping," + pingPong(x - 1);
          else if (x % 3 == 0)
                return "pong," + pingPong(x - 1);
          else
                return "?," + pingPong(x - 1);
}

     public static void main(String []args){
        String s;
                s=pingPong(10);
                System.out.println(s);
     }
}

Output:

Know the answer?
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
Similar Questions
Consider the following Java program : public static void main (string args [ ]) { int...
Consider the following Java program : public static void main (string args [ ]) { int result, x ; x = 1 ; result = 0; while (x < = 10) { if (x%2 == 0) result + = x ; + + x ; } System.out.println(result) ; } } Which of the following will be the output of the above program? A. 35 B. 30 C. 45 D. 35 2. public static void main(String args[]) { int i =...
QUESTION 1 For the following recursive function, find f(5): int f(int n) { if (n ==...
QUESTION 1 For the following recursive function, find f(5): int f(int n) { if (n == 0)    return 0; else    return n * f(n - 1); } A. 120 B. 60 C. 1 D. 0 10 points    QUESTION 2 Which of the following statements could describe the general (recursive) case of a recursive algorithm? In the following recursive function, which line(s) represent the general (recursive) case? void PrintIt(int n ) // line 1 { // line 2...
What is returned by the call: ben(51) ? public static String ben(int x) { if(x /...
What is returned by the call: ben(51) ? public static String ben(int x) { if(x / 5 <= 0) return x % 5; else return (x % 5) + ben(x / 5); }
public class Mystery { public static String mystery(String str, int input) { String result = "";...
public class Mystery { public static String mystery(String str, int input) { String result = ""; for (int i = 0; i < str.length() - 1; i++) { if (input == 0) { str = ""; result = str; } if (input == -2) { result = str.substring(2, 4); } if (input == 1) { result = str.substring(0, 1); } if (input == 2) { result = str.substring(0, 2); } if (input == 3) { result = str.substring(2, 3); }...
Given the recursive method definition:    public static int recurMethod (int num) {         if (num...
Given the recursive method definition:    public static int recurMethod (int num) {         if (num > 10)             return num;         else             return num + recurMethod(num / 4);     } And the initial call: int answer = recurMethod(320); list the values that will be returned from each call to the previous call of recursMethod. Start with the last recursive call, and work backwards, with one space between each returned value, ending with the value returned to the original...
Consider the following two methods: public static boolean isTrue(int n){        if(n%2 == 0)          ...
Consider the following two methods: public static boolean isTrue(int n){        if(n%2 == 0)           return true;        else           return false; } public static int Method(int[] numbers, int startIndex) { if(startIndex >= numbers.length)        return 0; if (isTrue(numbers[startIndex]))      return 1 + Method(numbers, startIndex + 1); else      return Method(numbers, startIndex + 1); } What is the final return value of Method() if it is called with the following parameters: numbers = {1, 2, 2, 3, 3,...
Draw a program flow graph for the function below int binsearch(int x,int v[],int n) { int...
Draw a program flow graph for the function below int binsearch(int x,int v[],int n) { int low,high,mid; low=0; high=n-1; while(low<high) { mid = ( low + high ) / 2; if( x < v[mid]) high = mid - 1; else if ( x > v[mid]) low = mid + 1; else return mid; } return -1; }
class Ex1{ 2. public static void main(String args[]){ 3. int x = 10; 4. int y...
class Ex1{ 2. public static void main(String args[]){ 3. int x = 10; 4. int y = new Ex1().change(x); 5. System.out.print(x+y); 6. } 7. int change(int x){ 8. x=12; 9. return x; 10. } 11. } Can you please explain this entire code and what is happening?
Write a recursive method public static int sumEveryOther(int n) that takes a positive int as an...
Write a recursive method public static int sumEveryOther(int n) that takes a positive int as an argument and returns the sum of every other int from n down to 1. For example, the call sumEveryOther(10) should return 30, since 10 + 8 + 6 + 4 + 2 = 30. The call sumEveryOther(9) should return 25 since 9 + 7 + 5 + 3 + 1 = 25. Your method must use recursion.
Fill in the gap to complete the recursive function below so that it splits the characters...
Fill in the gap to complete the recursive function below so that it splits the characters of the input parameter into separate lines. The new line character in Java is \n public static String lemon(String s){ if(s.length() == 0 || str.length() ==1 ) return s; return___________________________ }
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT