home—info—lects—labs—exams—hws
tutor/PIs—breeze (snow day)
Object120 + its docs—java.lang docs—java.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.
-
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, ...?)
-
Examples of the data.
Make a bunch of example instances.
-
Test cases:
Write a test driver with 2-3 test cases (or more, as appropriate).
- A test case includes input(s), and the
expected output.
- Include at least one test with a degenerate input
(if appropriate). For numbers, consider 0, 1.
For Strings, consider the empty string.
- Include at least one typical-use case.
- Consider whether fractional or negative numbers are
possible inputs. Either include a test case with them,
or make sure your comments state clearly they are not allowed.
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,"") );
}
|
-
Signature — for each method (function),
specify its name, what input parameters it needs (name and type),
and what type of information it will return.
- 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.
-
Complete the stub function, and compile.
Note that you still haven't yet written any code which
computes a solution.
-
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:
- Remind yourself of
exactly what pieces of info you have, to calculate your answer from.
- What are the types of each of these data?
What pertinent methods can you call, on data of that type?
-
Test — run the test cases you already wrote down
from step 2.
Does your program give the results you expect?
-
Refactor — review and (as necessary) rewrite your
code. Does it obey the The Laws of Programming?
Applying the design recipe
-
We'll write buffetPrice together, paying attention to these steps.
Krusteaze Pizza Korp. has an all-you-can-eat buffet meal.
In order to encourage people to bring their friends,
the price is $8 for the first person,
plus $4.50 for each friend after that.
Write a function buffetPrice,
which is given the size of the group (1 or bigger),
and returns the total cost.
(Your function will be used by the cash register.)
-
To do, with your neighbor:
following these steps,
write last3:
A function that takes in a String
and returns its last three letters.
(We'll declare that the provided String must have
at least three letters.)
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.
home—info—lects—labs—exams—hws
tutor/PIs—breeze (snow day)
Object120 + its docs—java.lang docs—java.util docs