RU beehive logo ITEC dept promo banner
ITEC 120
2012fall
dbraffitt
ibarland

homeinfolectslabsexamshws
tutor/PIsbreeze (snow day)
Object120 + its docsjava.lang docsjava.util docs

lect02a
the design recipe
revisit: stub functions and test drivers

The Design Recipe: A checklist

In writing our functions so far, we actually followed a series of steps to help guide us to the answer. Let's make those steps explicit, and then apply them to a new problem.

  1. Data analysis — What are the pieces of information at hand, for this problem, and which data types should be used to represent them in the program? (E.g., int, String, boolean, ...?)
  2. Examples of the data. Make a bunch of example instances.
  3. Test cases: Write a test driver with 2-3 test cases (or more, as appropriate). If I were testing a function foo, my test function might look like:
    static void myTestFunc() {
      System.out.println( "Testing: #vowels in `hello`, starting from index 3:" );
      System.out.println( "Desired: " + 1 );
      System.out.println( "Actual:  " + foo(3,"hello") );
    
      System.out.println( "Testing: #vowels in `hello`, starting from index 0:" );
      System.out.println( "Desired: " + 2 );
      System.out.println( "Actual:  " + foo(0,"hello") );
    
      System.out.println( "Testing: #vowels in `a`, starting from index 0:" );
      System.out.println( "Desired: " + 1 );
      System.out.println( "Actual:  " + foo(0,"a") );
    
    
    
      System.out.println( "Testing: #vowels in ``, starting from index 0:" );
      System.out.println( "Desired: " + 0 );
      System.out.println( "Actual:  " + foo(0,"") );
      }
    
  4. Signature — for each method (function), specify its name, what input parameters it needs (name and type), and what type of information it will return.
  5. Comments (written in javadoc). Describe what each parameter means (for example “the price of the item, in dollars”, or “the number of students officially enrolled in the course”) as well as the meaning of the return value.
  6. Complete the stub function, and compile. Note that you still haven't yet written any code which computes a solution.
  7. The body of the function. This is the only non-automatic part of the process. Reflect on the test cases you worked through by hand: how did you get from the input to the output? What is the general case?

    Things to help you on your way:

    1. Remind yourself of exactly what pieces of info you have, to calculate your answer from.
    2. What are the types of each of these data? What pertinent methods can you call, on data of that type?

  8. Test — run the test cases you already wrote down from step 2. Does your program give the results you expect?
  9. Refactor — review and (as necessary) rewrite your code. Does it obey the The Laws of Programming?

Applying the design recipe

Variables vs. one-big-return

In class we'll write buffetPrice in two different ways: using just one big return statement, and another using a variable to store a subresult.

To do, with your neighbor: Re-write at pizzaArea to use a variable.

To do: Re-write last3 so that it uses just one big return statement.

homeinfolectslabsexamshws
tutor/PIsbreeze (snow day)
Object120 + its docsjava.lang docsjava.util docs


©2012, Ian Barland, Radford University
Last modified 2012.Sep.03 (Mon)
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Powered by PLT Scheme