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...
Create a file named Good1 nano Good1 Type Welcome in the file and save it. What...
Create a file named Good1 nano Good1 Type Welcome in the file and save it. What command gives you a long listing of your filenames in the current directory, including permissions attached to each file. What command would you use to change a file’s permissions to include read, write and execute permission for the owner of the file only. Explain the following file permissions    a) 777          b) 765          c) 400          d) 666          e) 600         ...
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++
C++ Recursion, Change the delete_node function in the header file to a recursive delete_node function, main...
C++ Recursion, Change the delete_node function in the header file to a recursive delete_node function, main is already set. Hint: It might be helpful to modify the function so that it uses a separate recursive function to perform whatever processing is needed. //////////////////////////////////////////////////////////////header.h////////////////////////////////////////////////////////////////////////////////////////////// #ifndef HEADER_H_ #define HEADER_H_ #include <iostream> using namespace std; template <class T> class LL { private:    struct LLnode    {        LLnode* fwdPtr;        T theData;    };    LLnode* head; public:    LL();...
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 recursive function that finds the minimum value in an ArrayList. Your function signature should...
Write a recursive function that finds the minimum value in an ArrayList. Your function signature should be public static int findMinimum(ArrayList<Integer>) One way to think of finding a minimum recursively is to think “the minimum number is either the last element in the ArrayList, or the minimum value in the rest of the ArrayList”. For example, if you have the ArrayList [1, 3, 2, 567, 23, 45, 9], the minimum value in this ArrayList is either 9 or the minimum...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT