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

Please use Python! def getText(file): This function should open the file named file, and return the...
Please use Python! def getText(file): This function should open the file named file, and return the contents of the file formatted as a single string. During processing, you should (i) remove any blank lines, (ii) remove any lines consisting entirely of CAPITALIZED WORDS , and (iii) replace any explicit ’\n’ (newline) characters with spaces unless directly preceeded by a ’-’ (hyphen), in which case you should simply remove both the hyphen and the newline, restoring the original word. def getText(file):...
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();...
Create a Java class named ReadWriteCSV that reads the attached cis425_io.txt file that you will save...
Create a Java class named ReadWriteCSV that reads the attached cis425_io.txt file that you will save in your workspace as src/cis425_io.txt and displays the following table: -------------------------- | No | Month Name | Days | -------------------------- | 1 | January | 31 | | 2 | February | 28 | | 3 | March | 31 | | 4 | April | 30 | | 5 | May | 31 | | 6 | June | 30 |. | 7...
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...
In this PYTHON 3 program assignment, you will find a text file named WorldSeries.txt. This file...
In this PYTHON 3 program assignment, you will find a text file named WorldSeries.txt. This file contains a chronological list of the World Series' winning teams from 1903 through 2018. The first line in the file is the name of the team that won in 1903, and the last line is the name of the team that won in 2018. (Note the World Series was not played in 1904 and 1994. There are entries in the file indicating this.) 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 *...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT