Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  • Comments are enclosed in between /** and */ and must immediately precede the java constructs (e.g. class, field, method) that are to be commented.
  • Each method should have a comment that describes the purpose of the method.
  • Each method should have a comment for each parameter, if any. The comment for a parameter has the form:
    • @param parameter-name some-description
  • Each function method should have a comment for the returned object. The comment for the returned object has the form:
    • @return some-description
  • Each method should have a comment for each exception thrown, if any. The comment for an exception has the form:
    • @exception exception-type some-description
      Here are two files that at documented in javadoc style.
      Code Block
      /**
       * Represents a person who knows how to compute the number of months till his/her
       * next birthday.
       * @author DXN
       * @since Copyright 2009 by DXN - All rights reserved
       */
      class Person {
          /**
           * birth month
           */
          int bm;
         
          /**
           * Computes the number of month until the next birthday given the current month.
           * @param cm an integer between 1 and 12 as the current month.
           * @return an integer between 0 and 11.
           */
          int nMonthTillBD (int cm) {
              return (bm - cm + 12) % 12;
          }
      }
       
      /**
       * JUnit test class to test all methods in class Person.
       * @author DXN
       * @since 02/20/2009
       */
      class TestPerson extends TestCase {
         
          /**
           * Tests nMonthTillBD by covering three cases:
           * the current month is less than the birth month 
           * the current month is equal to the birth month
           * the current month is greater than the birth month.
           */
          void test_nMonthTillBD() {
             
              Person peter = new Person(9); // a person born in September.
             
              assertEquals("Calling nMonthTillBD(2).", 7, peter.nMonthTillBD(2));
              assertEquals("Calling nMonthTillBD(9).", 0, peter.nMonthTillBD(9));
              assertEquals("Calling nMonthTillBD(12).", 9, peter.nMonthTillBD(12));
          }   
      }
      

Creating javadoc using !DrJava:

DrJava has incorporated the javadoc utility into its IDE. Since javadoc does not know anything about language levels, we will delay running the javadoc utility on our code until we switch to the Full Java language.

For the “daring”"daring", you can try the following steps at your own risk!

...

Recall our object model (common known in the Scheme world as “data definition”"data definition") for lists of int.

  • IntList is an abstract list of int .
  • EmptyIntList is an IntList
  • ConsIntList(first, rest) is an !IntList, where first is an int and rest is an IntList .

...

Code Block
/**
 * Abstract list structure.
 */
abstract class IntList {
}
 
/**
 * Concrete empty list structure containing nothing.
 */
class EmptyIntList extends IntList {
}
 
/**
 * Concrete non-empty list structure containing an int, called first,
 * and a rest, which is a list structure.
 */
class ConsIntList extends IntList {
    int first;
    IntList rest;
}

...

Code Block
abstract class IntList {
    abstract returnType methodName(parameter_list); // returnType may be void
}
 
class EmptyIntList extends IntList {
    returnType methodName(parameter_list) {
        // base case code
    }
}
 
class ConsIntList extends IntList {
    int first;
    IntList rest;
    returnType methodName(parameter_list) {
        // …... first …...
        // …... rest.methodName(parameter_list)…...
    }
 
}

A Scheme-like String representation of lists.

Suppose we want to display an empty list as () and the list containing 1, 2, 3 as (1, 2, 3) . How do we do this? We need to add a method to the InList hierarchy of classes to perform this computation. Let’s Let's call this method listString() and let’s let's proceed together.

Step 1: Instantiate the interpreter code template given above by replacing returnType with String , the methodName with listString and the parameter_list with nothing. Be sure to create one file for each class. The code will not compile. Why?

...

Step 4: Write the code for listString in ConsIntList . You can try the structural recursive code given in the template and see that it will not work. This is because by the time you reach the end of the list (i.e. when rest is the empty list), you need to do something different from what EmptyIntList . listString() is programmed to do. You will need to call on rest to perform an auxiliary (“helper”"helper") method to get the job done. Let’s Let's call this helper method listStringHelp .

What does listStringHelp need to know? You can pass to it what you (as the current list) know and ask rest to complete the job. What does the current list know? It knows it can build the string "(" + first (meaning "(" concatenate with the default String representation of =first=) and it can pass this string to the helper method. So in effect the helper takes as parameter the accumulated string representation of the list so far and delegates the job for completing the final string representation to rest.

...

Step 7: Write the code for EmptyIntList.listStringHelp(String acc) What should the empty do here? It knows it has the accumulated string representation of the whole list so far and that all it needs is the closing parenthesis. So all it has to do is to add the closing parenthesis to the accumulated string and return: return acc + ")"; (And make sure it passes the JUnit test).

...

Code Block
abstract class IntList {
    /**
     * Computes a String representation of this list wiht matching parentheses
     * as in Scheme.  For example, the list containing 1, 2 and 3 should return
     * (1, 2, 3) and the empty list should return ().
     * @return a non empty String consisting of elements in this list enclosed
     * in a pair of matching parenthesis, separated by commas.
     */
    abstract String listString();
   
    /**
     * Accumulator helper method for listString to compute the String
     * required representation of this list given the accumulated
     * String representation of the preceding list.
     * @param acc the accumulated String representation of the list that
     * precedes this list.
     * @return a non empty String consisting of elements in this list enclosed
     * in a pair of matching parenthesis, separated by commas.
     */
    abstract String listStringHelp(String acc);
}
 
 
class EmptyIntList extends IntList {
    /**
     * @return "()"
     */
    String listString() {
        return "()";
    }
   
    /**
     * @param acc the accumulated String representation of the list that
     * precedes this list.  For example "(5, 3"
     * @return a non empty String consisting of elements in this list enclosed
     * in a pair of matching parenthesis, separated by commas.  For example,
     * "(5, 3)"
     */
    String listStringHelp(String acc) {
        return acc + ")";
    }
}
 
class ConsIntList extends IntList {
    int first;
    IntList rest;
   
    /**
     * Calls on rest to perform the helper method listStringHelp passing it
     * the accumulated String representation so far, which is "(" + first.
     * @return a non empty String consisting of elements in this list enclosed
     * in a pair of matching parenthesis, separated by commas.
     */
    String listString() {
        return rest.listStringHelp("(" + first);
    }
   
    /**
     * @param acc the accumulated String representation of the list that
     * precedes this list.  For example "(5, 3"
     * @return a non empty String consisting of elements in this list enclosed
     * in a pair of matching parenthesis, separated by commas.  For example,
     * "(5, 3)"
     */
    String listStringHelp(String acc) {
        return rest.listStringHelp(acc + ", " + first);
    }
}
 
/**
 * Testing empty lists.
 */
class TestEmptyIntList extends TestCase {
   
    /**
     */
    void test_listString() {
        EmptyIntList mt = new EmptyIntList();
       
        assertEquals(mt + ".listString()", "()", mt.listString());
    } 
}

Lab Exercises

  1. Write another version of listString that is tail recursive. Call it listString2 . (Can you think of a non-tail recursive version?)
  2. Write a method called prodNums that returns the product of the number in the list, using a tail recursive helper method.
  3. Revise the preceding definition to terminate immediately if it encounters a 0 in the list of numbers being multiplied.
  4. Write a method called makePalindrome that returns a list consisting of the input list and its mirror around the last element, using a (non tail-recursive) helper with an accumulator. For example, (1, 2, 3).makePalindrome () returns the list (1, 2, 3, 2, 1) .
  5. Write a method called reverse that reverses the list using a tail-recursive helper.

...