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); }...
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; }
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.
Given the following function:      int C(int n, int k)                  {              
Given the following function:      int C(int n, int k)                  {                     if (k= =0) return 1;                        else return (C(n-1, k-1) * n)/k;                                       } What type of function is this? Recursive or Iterative. Explain your answer.
There is a Java program that is missing one recursive function: public class Fibonacci { /*...
There is a Java program that is missing one recursive function: public class Fibonacci { /* / 0 when n = 0 * fib(n) = | 1 when n = 1 * \ fib(n-1)+fib(n-2) otherwise */ public static int fib(int n) { return 0; } /* Fibonacci Test Framework * * Note, this takes a long time to compute fib(44). */ public static void main(String[] args) { int[] input = { 11, 22, 33, 44}; int[] expect = { 89,...
Given the following function in C++: int main(void) { int a = 2; int b =...
Given the following function in C++: int main(void) { int a = 2; int b = myFunction(a); a = b + 1; b = myFunction(a); cout << ”b = ” << b << endl; return 0; } int z = myFunction(int x) { static int n = 0; n = n + 1; int z = x + n; return z; } What is printed by the cout on the screen?
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT