Recursion & Collections API Recursion Revisited Programming Assignments using the Collections API.

Post on 11-Jan-2016

244 views 1 download

Transcript of Recursion & Collections API Recursion Revisited Programming Assignments using the Collections API.

Recursion & Collections API

• Recursion Revisited

• Programming Assignments using the Collections API

Recursion as Repetition

public static void hello (int N)

{ for (int k = 0; k < N; k++)

System.out.println (“Hello World!”);

}

Recursion as Repetition

public static void hello (int N)

{

if ( N == 0)

return;

else

{

System.out.println (“Hello World!”);

hello (N – 1);

}

}

Recursion as Repetition

• Write a recursive method pow() that returns xn, where x and n are both integers greater than or equal to zero.

Recursion as Repetition

public static long pow (int x, int n)

{ if ( x == 0) return 0;

if ( n == 0) return 1;

long result = x * pow ( x, n – 1);

return result;

}

Recursive String Methods

• Write a recursive method that will print the characters in a string recursively, one character at a time.

Recursive String Methods

public static void printString (String s)

{ if ( s.length() == 0)

return;

else

{ System.out.println ( s.charAt(0) );

printString ( s.substring (1) );

}

}

Recursive String Methods

• Write a recursive method that will print a String in reverse order one character at a time.

Recursive String Methods

public static void printReverse ( String s )

{ if ( s.length() > 0 )

{ printReverse ( s.substring ( 1 ) );

System.out.println ( s.charAt ( 0 ) );

}

}

• Many recursive solutions involve breaking a sequential structure, such as a string or an array, into its head and tail. An operation is performed on the head, and the algorithm recurses on the tail.

Recursive String Methods

• Write a recursive method that will count the number of occurrences of the character ch in the String s.

Recursive String Methods

public static int countChar (String s, char ch)

{ if ( s.length() == 0 )

return 0;

else if ( s.charAt ( 0 ) == ch)

return 1 + countChar ( s.substring (1), ch);

else

return 0 + countChar ( s.substring (1), ch);

}

Recursive String Methods

• Write a recursive method to rotate a String by N characters to the right. For example, rotateR (“hello”, 3) should return “llohe”.

Recursive String Methods

public static String rotateR (String s, int n)

{ if ( n == 0 )

return s;

else

{ StringBuffer buf = new StringBuffer ();

buf.append (s.charAt (s.length() - 1));

buf.append (s.substring (0, s.length() - 1));

return rotateR (buf.toString(), n - 1);

}

}

Recursive String Methods

• Write a recursive method to convert a String representing a binary number to its decimal equivalent. For example, binTodecimal (“101011”) should return the int 43.

Recursive String Methods

public static int binTodecimal (String s)

{

if ( s.length() == 1)

return Integer.parseInt (s);

else

return Integer.parseInt (s.substring (s.length() - 1)) +

2 * binTodecimal (s.substring (0, s.length() - 1);

}

Recursive String Methods

• A palindrome is a string that is the same as its reverse – “radar” and “able was I ere I saw elba”. Write a recursive boolean method that determines whether its String parameter is a palindrome.

Recursive String Methods

public static boolean palindrome ( String s)

{

if ( s.length() == 0 || s.length() == 1 )

return true;

else

{ if ( s.charAt(0) != s.charAt (s.length() - 1))

return false;

else

return palindrome ( s.substring (1, s.length() - 1) );

}

}

Recursive Array Methods

• Write a recursive method that will do a sequential search on an array.

Recursive Array Methods

public static int rSearch (int[] arr, int head, int key )

{ if ( head == arr.length )

return –1;

else if ( arr[head] == key )

return head;

else

return rSearch ( arr, head + 1, key );

}

Recursive Array Methods

• Write a recursive method that will do a selection sort on an array.

Recursive Array Methods

public static void selectionSort ( int[] arr, int last ){ if ( last > 0 )

{ int maxLoc = findMaxIdx ( arr, last);swap ( arr, last, maxLoc );selectionSort ( arr, last – 1 );

}}public static int findMaxIdx ( arr, last){ int maxIdx = 0;

for (int i = 0; i <= last; i++)if ( arr [i] > arr[maxIdx)

maxIdx = i;}

Program 1

• Create a class with a method that takes a string and returns the number of characters that only occur once in the string. It is expected that the method will be called repeatedly with the same strings. Since the counting operation can be time consuming, the method should cache the results so that when the method is given a string previously encountered, it will simply retrieve the stored result. Use collections wherever appropriate.

Program 2

• Write a program that reads strings from input and outputs them sorted by length, shortest string first. If a subset of input strings has the same length, your program should output them in alphabetical order.