Question

In: Computer Science

Invent a recursive function and save the program in a file named " q3c.java” It should...

Invent a recursive function and save the program in a file named " q3c.java” It should not be any of the familiar ones (like fibonacci or binomial coefficients or any of those). Make one up. Make sure it is well-defined (no ambiguity), make sure it isn’t “infinitely recursive”. • Implement it in a Java program and demonstrate it with at least 7 test values. • Add necessary comments to the program to explain it. • In comments, describe your test inputs and outputs (and their correctness).

Solutions

Expert Solution

import java.util.Arrays;
import java.util.List;

class Main
{
        //This  Function will print all combinations of phrases that can be formed by words from each of the given lists
        public static void printAll(List<List<String>> list,String res, int n)
        {
                
                if (n == list.size())// to check if we have traversed each list
                {
                        System.out.println(res.substring(1));// print phrase after removing end space
                        return;
                }
                int m = list.get(n).size();

                
                for (int i = 0; i < m; i++)//  for each word in current list
                {
                        String out = res + " " + list.get(n).get(i);// append current word to output
            printAll(list, out, n + 1);// recuring for next list
                }
        }

        public static void main(String[] args)
        {
                List<List<String>> list = Arrays.asList(
                                                Arrays.asList("Anna", "Bryan","Cassy","Delan"),
                                                Arrays.asList( "Plays", "Hates", "Watches" ),
                                                Arrays.asList( "Cricket", "Soccer", "Chess" )
                                        );

                printAll(list, "", 0);
        }
}

OUTPUT:

Anna Plays Cricket
Anna Plays Soccer
Anna Plays Chess
Anna Hates Cricket
Anna Hates Soccer
Anna Hates Chess
Anna Watches Cricket
Anna Watches Soccer
Anna Watches Chess
Bryan Plays Cricket
Bryan Plays Soccer
Bryan Plays Chess
Bryan Hates Cricket
Bryan Hates Soccer
Bryan Hates Chess
Bryan Watches Cricket
Bryan Watches Soccer
Bryan Watches Chess
Cassy Plays Cricket
Cassy Plays Soccer
Cassy Plays Chess
Cassy Hates Cricket
Cassy Hates Soccer
Cassy Hates Chess
Cassy Watches Cricket
Cassy Watches Soccer
Cassy Watches Chess
Delan Plays Cricket
Delan Plays Soccer
Delan Plays Chess
Delan Hates Cricket
Delan Hates Soccer
Delan Hates Chess
Delan Watches Cricket
Delan Watches Soccer
Delan Watches Chess



Related Solutions

You should assume that there will be an object file named “foreignsub.o” which implements a function...
You should assume that there will be an object file named “foreignsub.o” which implements a function whose prototype is given in the header file “foreignsub.h”. The header file “foreignsub.h” consists of the following line. int sub(int n, int *A, int *B, int *C); However, you do not know what this function does exactly. For your testing purpose, you may produce such a function, e.g., returning the maximum value among the 3n integers in the three arrays. You are also given...
Requirements: C++ Array/File Functions Write a function named arrayToFile. The function should accept 3 arguments: The...
Requirements: C++ Array/File Functions Write a function named arrayToFile. The function should accept 3 arguments: The name of the file, a pointer to an array, and the size of the array. The function should open the specified file in binary mode, write the contents of the array to file, and then close the file. Write another function named fileToArray. This function should accept 3 arguments: the name of the file, a pointer, to an int array, and the size of...
Write scores of 20 students on a quiz to a file. The file should be named...
Write scores of 20 students on a quiz to a file. The file should be named scores.txt. The scores should be random numbers between 0-10. Next, read the scores from scores.txt and double them and print the scores to screen. c++
Write a program that creates an output file named rand_nums.txt. Open the file and write 100...
Write a program that creates an output file named rand_nums.txt. Open the file and write 100 random integers between -50 and +50 (inclusive) to the file. Be sure to handle any file IO exceptions. Remember to close the file. Write a program that opens rand_nums.txt for input. Create two output files pos.txt and neg.txt. Read through the input file, one line at a time, converting each line into an integer (no exception handling, yet). If the number is positive, write...
Write a recursive function named multiply that takes two positive integers as parameters and returns the...
Write a recursive function named multiply that takes two positive integers as parameters and returns the product of those two numbers (the result from multiplying them together). Your program should not use multiplication - it should find the result by using only addition. To get your thinking on the right track: 7 * 4 = 7 + (7 * 3) 7 * 3 = 7 + (7 * 2) 7 * 2 = 7 + (7 * 1) 7 *...
PYTHON: Write a recursive function named linear_search that searches a list to find a given element....
PYTHON: Write a recursive function named linear_search that searches a list to find a given element. If the element is in the list, the function returns the index of the first instance of the element, otherwise it returns -1000. Sample Output >> linear_search(72, [10, 32, 83, 2, 72, 100, 32]) 4 >> linear_search(32, [10, 32, 83, 2, 72, 100, 32]) 1 >> linear_search(0, [10, 32, 83, 2, 72, 100, 32]) -1000 >> linear_search('a', ['c', 'a', 'l', 'i', 'f', 'o', 'r',...
Python program: Write a program that reads a text file named test_scores.txt to read the name...
Python program: Write a program that reads a text file named test_scores.txt to read the name of the student and his/her scores for 3 tests. The program should display class average for first test (average of scores of test 1) and average (average of 3 tests) for each student. Expected Output: ['John', '25', '26', '27'] ['Michael', '24', '28', '29'] ['Adelle', '23', '24', '20'] [['John', '25', '26', '27'], ['Michael', '24', '28', '29'], ['Adelle', '23', '24', '20']] Class average for test 1...
Write a C++ program that start the assignment by creating a file named pointerTasks.cpp with an...
Write a C++ program that start the assignment by creating a file named pointerTasks.cpp with an empty main function, then add statements in main() to accomplish each of the tasks listed below. Some of the tasks will only require a single C++ statement, others will require more than one. For each step, include a comment in your program indicating which step you are completing in the following statement(s). The easiest way to do this is copy and paste the below...
Write a C++ program that start the assignment by creating a file named pointerTasks.cpp with an...
Write a C++ program that start the assignment by creating a file named pointerTasks.cpp with an empty main function, then add statements in main() to accomplish each of the tasks listed below. Some of the tasks will only require a single C++ statement, others will require more than one. For each step, include a comment in your program indicating which step you are completing in the following statement(s). The easiest way to do this is copy and paste the below...
There is a Java program that is missing one recursive function: public class Ackermann { /*...
There is a Java program that is missing one recursive function: public class Ackermann { /* / n+1 when m = 0 * ack(m,n) = | ack(m-1,1) when m > 0 and n = 0 * \ ack(m-1, ack(m, n-1)) otherwise */ public static int ack(int m, int n) { return 0; } /* Ackermann's Function Test Framework * * Be extremely careful with these test cases. Ackermann's grows very fast. * For example, ack(4, 0) = 13, but ack(5,0)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT