public static void mystery(int level) { if (level == 0) { System.out.print("*"); } else { System.out.print("["); mystery(level - 1); System.out.print(","); mystery(level - 1); System.out.print("]"); }
Show the output that would be produced by the following calls
public class Demo { public static void main(String[] ags) { System.out.println( fun(5) ); } public static int fun(int n) { if (n == 0) return 1; else return n + fun(n/2); } }
and predict the output
##### #### ### ## #
public static void fun(int n){ if(n == 0) return; else{ for(int i=0; i<n; i++) System.out.print("#"); System.out.println(); fun(n-1); } }
# ## ### #### #####
public static void fun(int n){ if(n == 0) return; else{ fun(n-1); for(int i=0; i<n; i++) System.out.print("#"); System.out.println(); } } OR public static void fun(int n){ if (n == 6) return; else{ for(int i=0; i<n; i++) System.out.print("#"); System.out.println(); fun(n+1); } }
public void removeAll(Object keyItem, Node current)
public void removeAll(Object keyItem) { head = removeAll(head, keyItem); } private NoderemoveAll(Node p, Object key) { if(p == null) return null; else if(key.equals(p.data)) return removeAll(p.next, key); else p.next = removeAll(p.next, key); return p; }
public void removeAllGreaterItems(Comparable keyItem, Node current)
public void removeAllGreaterItems(ComparablekeyItem) { head = removeAllGreaterItems(head, keyItem); } private Node removeAllGreaterItems(Node p, Comparable key) { if(p == null) return null; else if(key.compareTo(p.data) < 0) return removeAllGreaterItems(p.next, key); else p.next = removeAllGreaterItems(p.next, key); return p; }
public void pattern(int[] input)
that takes an array of integers input and prints it out input.length times. The print must be in the following specific order: each line starts at a new element and wraps around to the beginning of the array (if needed) until each element has been printed out.
This sample output occurs when the input is the array {0, 1, 2, 3, 4}
:
0 1 2 3 4 1 2 3 4 0 2 3 4 0 1 3 4 0 1 2 4 0 1 2 3
public static void pattern(int[] input){ patternRec(input, 0); } private static void patternRec(int[] input, int n){ if(n == input.length) return; for(int i=n; i<input.length; i++) System.out.print(input[i] + " "); for(int i=0; i<n; i++) System.out.print(input[i] + " "); System.out.println(); patternRec(input, n+1); }