Self-Review Questions

  1. Consider the following mystery method:
    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

  2. Examine the following code segment
    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

  3. Write a recursive method that prints the following pattern?
    #####
    ####
    ###
    ##
    #
    

    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);
    	}
    }
    

  4. Write a recursive method that prints the following pattern?
    #
    ##
    ###
    ####
    #####
    

    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);
    	}		
    }
    

  5. Write a recursive method that removes all nodes with data equal to keyItem
    public void removeAll(Object keyItem, Node current)

    	public void removeAll(Object keyItem)
    	{
    		head = removeAll(head, keyItem);
    	}
    	private Node removeAll(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;
    	}
    	
    

  6. Write a recursive method that removes all nodes with data greater than keyItem
    public void removeAllGreaterItems(Comparable keyItem, Node current)

    	public void removeAllGreaterItems(Comparable keyItem)
    	{
    		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;
    	}
    	
    

  7. Write a recursive method (you are allowed to have helper-methods)
    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);
    }