|
home—info—lects—labs—exams—hws
textbook—tutor/PIs—java.lang docs—java.util docs
The previous lab had three phases: read input, print the average, and then have a user repeatedly probe the array interactively.
Today's task: rewrite your program so that main is only three lines long (but it still behaves as before):
public static void main( String[] args ) { double[] myNums = readArrayFromKbd(5); System.out.println( "The average value is " + avg(myNums) ); letUserQueryArray(myNums); } |
(Make those methods static, since their answer doesn't depend on which object is being called.)
Discussion:
is this code easier to write and understand, or more difficult?
Is writing the helper methods easier, when they are only in charge of
one small task, independent of the rest of program?
In general, humans can only cope with large software systems by
breaking them down into small, independent methods (and classes)
which call each other.
Large programs will have literally thousands of methods.
(And often, each individual method will be quite short.)
Note: We have relaxed lab12a's problem statement, so that we can assume the user only types integers, and never a non-integer; we stop when we read a number not in the expected range.
Here is a high-level description of how to handle the input-gathering, when we can assume that only integers are entered:
n ← prompt-and-read-a-number while n is in 1..5, begin: print the requested item from our array n ← prompt-and-read-a-number endwhile(Of course, this is written in pseudocode, not actual Java.) The steps for "prompt-and-read-a-number" might be a couple lines of code, and has to be repeated in two places (ugh) — a “loop and a half”.
For the curious, If you really wanted to handle the case which includes non-integers being typed:
print "Please enter a number: " while (s.hasNextIntInRange(1,5)) { n ← s.nextInt() print the (n-1)st item from our array print "Please enter a number: " endHowever, Scanner doesn't quite have such a method, and I can't write that as a method myself, using Scanner.
boolean amDone ← false; print "Please enter a number:" while (not amDone, and s.hasNextInt()), do: n ← nextInt(); if n is in 1..5, then: print the (n-1)st item from our array print "Please enter a number: " else amDone ← true endif endwhileOne surprising fact: the order of the two tests in the “and” are important: if you test s.hasNextInt() first, that method will wait for something to be typed, even if amDone has been set to true.
Finally, a longer (but more methodical) approach would use two two variables, for the input: a boolean numberWasTyped along with an int theNumberTyped; the latter's value is garbage if numberWasTyped isn't true:
print "Please enter a number:" if (s.hasNextInt()) { numberWasTyped ← true; numberTyped ← nextInt(); } else { numberWasTyped ← false; numberTyped ← -99; // doesn't matter. } while (numberWasTyped && numberTyped is in 1..5), do: print the (n-1)st item from our array print "Please enter a number:" if (s.hasNextInt()), then: numberWasTyped ← true; numberTyped ← nextInt(); else numberWasTyped ← false; numberTyped ← -99; // doesn't matter. endif endwhile
home—info—lects—labs—exams—hws
textbook—tutor/PIs—java.lang docs—java.util docs
©2009, Ian Barland, Radford University Last modified 2009.Apr.15 (Wed) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |