A message on standard error, starting with the program's name
Then exit status 1
Each one went through fail
So each looks the same, to a person and to a script
Lecture 1's standard
A stack trace is never the response: no run so far ends in one
Every input gets a described response: Sum fails
The manual describes no message and no exit status
And the manual sits in the source:
$ java Sum --help
Sum: unknown option: --help
What are the checks for?
Lines 28 to 55 check the arguments
Line 61 checks each word of the input
Lines 73 to 75 check that there is something to average
Take one away, and run the same command
The lines go blank, so the rest keep their numbers
A negative precision
40if(precision<0){41fail("--precision must not be negative, got: "42+precision);43}
$ java Sum -a --precision=-1
Sum: --precision must not be negative, got: -1
Without lines 40 to 43
$ java Sum -a --precision=-1
1 2Exception in thread "main" java.util.UnknownFormatConversionException:Conversion = '.' at java.base/java.util.Formatter.parse(Formatter.java:2818) at java.base/java.util.Formatter.format(Formatter.java:2744) at java.base/java.io.PrintStream.format(PrintStream.java:1183) at java.base/java.io.PrintStream.printf(PrintStream.java:1081) at Sum.main(Sum.java:78)
Line 78 builds the format %.-1f, and printf throws
Without lines 73 to 75
73if(count==0){74fail("cannot average zero values");75}
$ java Sum -a
NaN$ echo $?0
Type nothing; end the input at once.
NaN, not a number: line 76 divides 0 by 0
Exit status 0: a script would take NaN for an average
The average of no numbers
No single right answer: refuse, as Sum does, or print 0
One answer is always wrong: ignore the problem
As the copy without lines 73 to 75 does
USS Yorktown: a crew member entered a zero
A program divided by it, and the cruiser stopped for hours
Sum's manual should say which answer Sum gives
Brian Kernighan and Rob Pike, 1999.
Checks at the edge
Each check stands between the user and the computation
The user owes Sum nothing
The computation should get only what it can handle
Split main into methods with specifications, as in Lecture 2
The checks stay at the edge, in main
A method inside may then have a precondition
Homework 1's Gradebook has the same shape
Coming up
Tuesday: section, on assertions and specifications