RU beehive logo ITEC dept promo banner
ITEC 120
2010fall
ibarland

homeinfolectslabsexamshws
tutor/PIsjava.lang docsjava.util docs

lect04a
Nesting if-else

Let's write a function determining whether somebody's track time would qualify for the olympics, in the 100m dash. Follow the steps of the design recipe.

  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. Signature — for each method (function), specify its name, what input parameters it needs (name and type), and what type of information it will return.
  4. 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.
  5. Complete the stub function, and compile. Note that you still haven't yet written any code which computes a solution.
  6. Test cases: Write a test driver with 2-3 test cases (or more, as appropriate).
  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?
Solution (scroll down to the function)

We'll now evolve this program:

AND,OR

Finally: We'll modify qualifies100m so that it just has a single return statement (!). This is not required, but some people prefer this version. (Note that if your function doesn't return a boolean, you won't be able to concentrate the code this much.)

homeinfolectslabsexamshws
tutor/PIsjava.lang docsjava.util docs


©2010, Ian Barland, Radford University
Last modified 2010.Sep.21 (Tue)
Please mail any suggestions
(incl. typos, broken links)
to iba�rlandrad�ford.edu
Powered by PLT Scheme