Ask your own question, for FREE!
Computer Science 21 Online
OpenStudy (anonymous):

(Java) Write a method with two parameters, prefix ( a string) and levels(a non-negative integer). The method prints the string prefix followed by "section numbers" of the form 1.1,1.2,1.3 and so on. Start by printing: Box: 1.1 Box: 1.2 all the way to Box:9.9

OpenStudy (anonymous):

I think this problem has to do with finding permutations. Lexicographical permutations with repetitions to be exact. For example perm(A,B,C): AAA AAB AAC ABA ABB ABC ACA ACB ACC CAA CAB CAC CBA CBB CBC CCA CCB CCC If you solve this could you please post the code?

OpenStudy (anonymous):

Regrettably I don't have time to finish this, you need to sort the list and implement a correct print method: import java.util.List; import java.util.ArrayList; public class Permutations { private String a; private int n; public Permutations(String a, int n) { this.a = a; this.n = n; } public List<String> getVariations() { int l = a.length(); int permutations = (int)Math.pow(l, n); char[][] table = new char[permutations][n]; for(int x = 0; x < n; x++) { int t2 = (int)Math.pow(l, x); for (int p1 = 0; p1 < permutations;) { for (int al = 0; al < l; al++) { for (int p2 = 0; p2 < t2; p2++) { table[p1][x] = a.charAt(al); p1++; } } } } List<String> result = new ArrayList<String>(); for(char[] permutation : table) { result.add(new String(permutation)); } return result; } public static void printWithPrefix(String prefix, int length) { Permutations gen = new Permutations("123456789", length); List<String> v = gen.getVariations(); for (String s : v) { System.out.println(prefix + ": " + s); } } public static void main(String[] args) { printWithPrefix("Box", 3); } }

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!