home—info—lects—labs—exams—hws
tutor/PIs—breeze (snow day)
Object120 + its docs—java.lang docs—java.util docs
lect09c
loops that accumulate info
Review
-
How to decide: should a method be made static, or not?
-
If no objects are involved at all, then it must be static.
-
Similarly: If it makes sense to call this method
even before any objects get created, then it must be static.
-
Even if objects are involved: If ten different object would all give
the same answer to the question,
then make it static.
(E.g. for a class FordFocus, asking what the odometer reading
is will give different answers (make getOdomoter
non-static), but asking what
the EPA's city-mpg rating is always returns the same answer
(make getEPARating static, for all FordFocuses.)
- Steps to help you write a loop:
- What will you use for your index variable?
(What is its initial value? How will you update it?)
Usually, indices either go from 0 up to a stopping-point
(“processedSoFar”),
or from 1 to through the stopping-point
(“nextToProcessed”).
It's okay to just use the idiomatic name “i”,
but do think about which of the two above it means.
- What will you use for your accumulator (“so-far” variable?
(What is its initial value? How will you update it?)
Using the name “somethingSoFar”
can be helpful in keeping you on-track.
- The type of the accumulator is almost always the same
type as the function's return-type.
- To help you think about how to update:
Suppose you've gone through the loop 17 times -- what exactly
should
your accumulator variable hold?
What will it hold after 18 times?
The difference in these two answers
is 'how to update the accumulator variable'.
Accumulators can be Strings
1The ITEC department does not condone
forging scientific data. ↩
home—info—lects—labs—exams—hws
tutor/PIs—breeze (snow day)
Object120 + its docs—java.lang docs—java.util docs