Homework 11


Assignment: 2 problems, 35 points for #1 and 15 points for #2 (no Extra Credit this week), due on Reading: Your CS5 lecture notes and the online readings for Sect 11.1.


Problem 1     The recursive roster     (35 points)     [topics: recursion]

Problem 1 is this homework's Pair Programming problem and can be worked on in groups of two as described at http://www.cs.hmc.edu/cs5/pair_programming.html.



For this problem, you'll be implementing a menu of options similar to those in previous assignments. No classes other than CS5App will be needed. Each menu option will require its own method, however, and all of these methods must be recursive.

The menu of options

Your program should offer the following menu (in a while loop) to the user:

The Recursive Roster:
  (1) factorial
  (2) power
  (3) harmonic series
  (4) palindrome checker
  (5) hanoi solver
  (6) min mover
  (7) list sorter
  (9) quit

Which option would you like?  
Each menu option will ask for one or more inputs, pass them to a recursive method, and then print out an appropriate result. Each option is described in more detail below. A suitable interface is shown in the example run.

The recursive methods

All of these should be in the CS5App class:

  1. Factorial: This method should have the signature
       public static double factorial(int n) 
      
    It should return the factorial of the input n as a double. If the input is zero or negative, this method should return 1.0.


  2. Power: This method should have the signature
       public static double power(double b, int n) 
      
    It should return the value of b to the n power. If n is zero or negative, this method should return 1.0. Remember that taking a power is NOT repeated squaring -- it's repeated multiplication by the base, b.


  3. Harmonic Series: This method should have the signature
       public static double harmonic(int n) 
      
    It should return the value of the first n terms of the harmonic sequence, i.e., the sum of the reciprocals of the first n positive integers. For example,
      harmonic(1) = 1/1 = 1.0
      harmonic(2) = 1/1 + 1/2 = 1.5
      harmonic(5) = 1/1 + 1/2 + 1/3 + 1/4 + 1/5 = 2.28333
      
    If n is one or less, this method should return 1.0. Watch out for type casting here -- remember that in java, 1/2 is equal to 0, but 1.0/2 is equal to 0.5!


  4. Palindrome checker: This method should have the signature
       public static boolean isPalindrome(String s) 
      
    It should return true if s is a palindrome, i.e., if s is the same either forwards or backwards. It should return false otherwise. Note that any word of one letter is a palindrome and any word of zero letters is also a palindrome. After all, one-letter and zero-letter words do read the same forwards and backwards.

    Additional notes:
  5. Tower of Hanoi Solver: This method should have the signature
     
      public static void solve_hanoi(int height, int fromPole, int withPole, int toPole)
       
    It should print the series of moves needed to solve the Towers of Hanoi puzzle for height disks (it is because the answer is being printed to the console that the return type is void). From main you'll want to assume the source pole (the one originally containing the disks) is pole 1 and that the destination pole (where they end up) is 3. Pole 2 is the "temporary pole".


  6. Minimum mover: This method should have the signature
       public static void moveMinToLeft(double[] A, int L, int U) 
      
    This method does not return anything, but when called the minimum element of the array A that was anywhere between index L and index U inclusive, should be swapped around so that it now is located at index L. (These stand for "lower" and "upper" indices.) Other elements may be swapped around as well, but no elements of A should be deleted or added.

    Note that for this moveMinToLeft option and the sort one below, your code (in main somewhere) will have to ask the user for, first, the number of elements to have in the array, and then the elements in the array, and THEN it should ask for the low and high indices that will be used for L and U when calling moveMinToLeft. (You can assume the user always enters "valid" values for L and H.) It will also need to print the array after the method is called. See the example below for a guide to input and output.

  7. List sorter: This method should have the signature
       public static void sort(double[] A, int L, int U) 
      
    It does not return anything, but after sort is called, the elements between index L and U, inclusive, in the input array A should be sorted in ascending order. Hint: Use the above moveMinToLeft method, as well as recursion.


Sample Run

Here is an example run -- the text the user types is in blue. Remember that all of the prompting for inputs and printing of outputs should be done in main:

+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
Choose a recursive function to test:
   1  factorial
   2  power
   3  harmonic series
   4  palindrome checker
   5  hanoi solver
   6  min mover
   7  list sorter
   9  quit

Choose an option: 1



Type a nonegative integer to take the factorial of:  6

The factorial of 6 is 720.0


+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
< Recursive Roster menu >

Choose an option: 2



Type a base (a double) and a power (a nonnegative int):  5.5  3

5.5 to the 3 power is 166.375


+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
< Recursive Roster menu >

Choose an option: 3



Type a positive number of terms:  1000

The sum of 1000 terms is 7.485470860550343


+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
< Recursive Roster menu >

Choose an option: 3



Type a positive number of terms:  5

The sum of 5 terms is 2.283333333333333


+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
< Recursive Roster menu >

Choose an option: 4



Type a possible palindrome:  racecar

 racecar is a palindrome


+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
< Recursive Roster menu >

Choose an option: 4



Type a possible palindrome:  racecars

 racecars is NOT a palindrome


+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
< Recursive Roster menu >

Choose an option: 5

Enter tower height 3

Solution is:
1->3 
1->2 
3->2 
1->3 
2->1 
2->3 
1->3 

+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
< Recursive Roster menu >

Choose an option: 6



How many numbers would you like to handle?  5

Type the 5 numbers:
#0: 67.8
#1: 54.01
#2: 1.10
#3: 100
#4: -65

Type the low index (L):  1
Type the low index (U):  3

The new list is
67.8
1.1
54.01
100.0
-65.0


+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
< Recursive Roster menu >

Choose an option: 7



How many numbers would you like to sort?  5

Type the 5 numbers:
67.8
54.01
1.10
100
-65

Type the low index (L):  0
Type the low index (U):  4

The sorted list from 0 to 4 (inclusive) is 
-65.0
1.1
54.01
67.8
100.0


+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
< Recursive Roster menu >

Choose an option: 9

Submission    Be sure to submit your CS5App.java file under homework 11, problem 1.

Extra Credit There is no Extra Credit this week.


Problem 2     Final Project -- getting started     [topics: more practice with Classes and Objects]

The final project (linked here) is a large program that implements a movie database. (The full description will be handed out in class during week 14.) Because it is by far the largest software project in CS 5, this problem intends to be sure that everyone has at least a skeleton prepared in advance. (Trying to write the movie database a few hours before the deadline would not be a good idea!)

Thus, for this problem, you will need to write all of the classes and data members needed for the movie database. In addition, you will need to write stubs (empty methods) for all of the functions -- with the correct signature and return value for each; you do not need to actually implement them until week 14. You should also comment them this week (that way you'll have a sense of what each does!)

What do I return? You may return anything of the correct type, but you do need to return something in order for your code to compile (which we will check!). So, for example, you could return 0 for an int, 0.0 for a double, and true for a boolean. For objects, the value you should return is null, which is a reference that doesn't refer to anything! Though it's not too useful in and of itself, it is a legal value for any variable of some class type. Thus, the stub for your "getFilmDB" method in the Director class could look like this:

  public FilmDB getFilmDB()
  {
    return null;
  }
Finally, your code should compile and run. By run I mean it needs to implement an initial question (ask the user for the capacity of the database) and then provide a main menu of user options (as well as accept their input). Of course, aside from the quit option, individual options do not have to work until week 14.

Commenting: Your code should have the usual comments for each method (this will help considerably when writing the actual methods for the final assignment!). That is, they should have a brief note of purpose and should explain their inputs and outputs. Here's a complete example for the above getFilmDB method in the Director class:

  /* method: getFilmDB, used to obtain all of a director's films
   *
   * inputs: none
   * return: an object of class FilmDB holding "this" director's films
   */

 public FilmDB getFilmDB()
 {
   return null;
 }

Classes: Here is a list of each class, along with their data and method members. A short description for each is here so that you'll be able to write comments on what each method does. You may include more methods (either this week or in week 14), but you should have at least these:

Sample Run:

Welcome to the bottomless CS 5 film database!

Please choose from the following list:

   0 Display all directors
   1 Display all films
   2 Display all films by Title
   3 Display films by Director
   4 Display films by Year
   5 Display films by Rating (G, PG, ...)
   6 Display films by Review (0-10)
   7 Add a new film

   8 Save database to a file
   9 Load database from a file

  42 Quit

Enter a choice:  42


Until next time, the gallery is closed.

Submission    For this problem, submit your CS5App.java file under Homework 11, Problem 2.