This table illustrates how code for echoing, printing the max, and printing the average of a sequences of grades, terminated by -1, all fit into the template/pattern we have given for processing an input sequence. notes: + view this in a mono-spaced (aka fixed width, aka non-proportional) font, e.g. courier, in a window that can display 104 columns; if you wish to print this, you may wish to consider landscape mode. + we use too-short variable names ($g$ instead of $grade$) to save space. + we butcher $System.out.println$ as $println$ to save space. + assume the following two lines start off each code segment: TokenReader in = new TokenReader(System.in); int g; // next grade to process For SavitchIn, use $g=SavitchIn.readInt()$ echo max print average +--------------------------+---------------------------+----------------------------+ set up: | g = in.readInt(); | int m = -1; // max so far | int n = 0; // count so far | read first value | | g = in.readInt(); | int t = 0; // total so far | init. other vars | | | g = in.readInt(); | +--------------------------+---------------------------+----------------------------+ while (not done) { | while (g != -1) { | while (g != -1) { | while (g != -1) { | +--------------------------+---------------------------+----------------------------+ process current | println(g); | if (g>m) m = g; | n = n+1; | | | | t = t+g; | +--------------------------+---------------------------+----------------------------+ read next | g = in.readInt(); | g = in.readInt(); | g = in.readInt(); | +--------------------------+---------------------------+----------------------------+ } | } | } | } | +--------------------------+---------------------------+----------------------------+ finish up | | if (m == -1) | if (n==0) | | | println("no values"); | println("no values"); | | | else | else | | | println(m); | println(t/(double) n); | +--------------------------+---------------------------+----------------------------+