In: Computer Science
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).
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