CS230 Data Structures, Wellesley College, Fall 2008

Lab 2: Recursion with numbers and strings

Problem 1: Powers

In this problem we will use recursion to write a simple Java program that contains a couple of class methods.

It's a good idea to have a labs directory in your account. You can put all your labs from the semester in this folder. Create a labs directory, and then create a subdirectory named lab2, under your labs directory, and make it your working directory, as shown below. You can move your lab1 folder from last week into your labs folder as well.

map of file structure

Then follow these steps:

  1. Create a new file called Recurse.java.
    You can do this one of two ways:
  2. If your Java is a bit rusty, you can use this template as a guide
  3. Add a class method called power() to Recurse.java. This method should compute x^n, according to the following recursive definition:

    x^0 = 1.0

    for all x

    x^n = x * x^(n-1)

    for n>0

     

  4. Add a main() method to your program, and write a few lines of code to invoke power(). Compile and run your program to make sure it works correctly. Think about some good test cases to use!

     

  5. Add one more class method to your program, called morePower(), to compute x^n according to the following alternative definition:

    x^n = x^(n/2) * x^(n/2)

    for even values of n

    x^n = x * x^(n/2) * x^(n/2)

    for odd values of n

  6. Test morePower() (here is a mere wisp of a start of testing)
  7. Format the output so it looks good (check out the DecimalFormat class in the Java API)

If you had to choose between the two implementations above, which one would you prefer? Why? Write your answer in the comments of the method.

When you are done, just for practice with the submit command, submit your Recurse.java program electronically by executing the following command, while still in your lab2 directory:

submit cs230 powers *.*

Executing this command results in the creation of a subdirectory, named powers, under your drop directory. The powers directory will contain all the files in your currently working directory.

Problem 2: Working with Strings

  racecar   tuna roll or a nut   otto sees otto

Problem 3: Play with Digits

  • Write a class DigitPlay, that contains a public class method called numDigits(). This method takes a positive integer as an argument, and counts and returns the number of digits in the input number. For example, if the method is passed the number 123456 as argument, it should return 6, since there are 6 digits in the input number.
  • Add another method to your class, called sumDigits() that, again takes a positive integer as an argument and returns the sum of the digits in the input number. For example, if the method is passed the number 123456 as argument, it should return the integer 21, since 1+2+3+4+5+6 = 21. The definition of this method will be very similar to the numDigits() one.

    Both of the above methods should be defined recursively. As always, test each method as soon as you define it, before you proceed to add more code to your program.

  • Most identification numbers, such as the ISBN number on books or the Universal Product Code (UPC) on grocery products or the identification number on a traveler’s check, have at least one digit in the number that is a check digit. The check digit is used to detect errors in the number. The simplest check digit scheme is to add one digit to the identification number so that the sum of all the digits, including the check digit, is evenly divisible by some particular integer. For example, American Express Traveler’s checks add a check digit so that the sum of the digits in the id number is evenly divisible by 9. United Parcel Service adds a check digit to its tracking numbers such that a weighted sum of the digits (some of the digits in the number are multiplied by numbers other than 1) is divisible by 7.

    Add one more method to your DigitPlay program, called validateNumber() that takes a positive integer, and determines if it is a possible AMEX Traveler’s check id number. (Question: If the sum is indeed divisible by 9, can you tell for sure that the input number is a correct AMEX Traveler’s check id number?)

    Problem 4: Pascal's Triangle

    If you have extra time...
    Write a program to print out the Nth line of Pascal's Triangle, as shown below. The number N should be passed to the program as a command-line argument.
    Click here if you need a hint!
                             1
                         1       1
                      1      2      1 
                   1     3       3     1
                1     4      6      4     1
             1     5     10      10    5     1
          1     6     15     20     15    6     1
       1     7     21    35      35    21    7     1
    1     8     28    56     70     56    28    8     1