CS 100Lecture 41 CS100J Lecture 4 n Previous Lecture –Programming Concepts n iteration n programming patterns (templates) –Java Constructs n while-statements –Reading: L&L, Sec ; or S. Sec n This Lecture –Programming Concepts n Rules of thumb –learn and use patterns –inspiration from hand-working problem –boundary conditions –validation n Pattern for processing input values up to (but not including) a stopping signal –Example n Processing exam grades –Java Constructs n Casts n Rounding –Reading, Lewis & Loftus, Section 3.9
CS 100Lecture 42 Problem Context n n Input Data: Zero or more exam grades (between 0 and 100), followed by a stopping value of -1. Assume input is well-formed. n n Sample Input Data: n n Degenerate Input Data: n n The General Task: Input the grades and “process” them, i.e., print some information about them.
CS 100Lecture 43 Sample Tasks 1. Print the grades. 2. Print the number of grades. 3. Print the average grade. 4. Print the highest grade. 5. All of the above.
CS 100Lecture 44 Programming Rules of Thumb Learn program patterns of general utility and use a relevant pattern (if you know one) Learn program patterns of general utility and use a relevant pattern (if you know one) for the problem at hand. n n Seek inspiration by systematically working test data by hand. Be introspective; ask yourself: “What am I doing?” n n Declare variables for each piece of information you maintain when working the problem by hand. Write comments that precisely describe the contents of each variable. n n Remember to consider the problem’s boundary conditions. n n Validate your program by tracing it on simple test data.
CS 100Lecture 45 A Useful Pattern /* “Process” integer input values until (but not including) some designated stoppingValue. */ variable = in.readInt(); variable = in.readInt(); while ( variable != stoppingValue ) while ( variable != stoppingValue ) { variable = in.readInt(); variable = in.readInt(); } /* Do for each integer between 1 and n. */ variable = 1; variable = 1; while ( variable <= n ) while ( variable <= n ) { variable = variable + 1; variable = variable + 1; } Contrast with:
CS 100Lecture 46 Placeholders in Pattern variable, , , , and stoppingValue are placeholders variable contains the most recently read input value. Replace it uniformly by a name that is meaningful for the given problem. stoppingValue is the value designated to signal the end of the input data. is the initialization: statements that initialize variables before any input value is processed. is where you process each input value except the stoppingValue. is the finalization: where you perform operations after all input values have been processed.
CS 100Lecture 47 Pattern for “processing” grades /* “Process” grades up to (but not including) a stopping signal of -1. */ grade = in.readInt(); grade = in.readInt(); while (grade != -1 ) while (grade != -1 ) { grade = in.readInt(); grade = in.readInt(); } int grade; // the grade being processed.
CS 100Lecture 48 Task 1: Print the grades where : System.out.println( grade ); /* Print each grade input up to a stopping signal of -1. */ grade = in.readInt(); grade = in.readInt(); while (grade != -1 ) while (grade != -1 ) { grade = in.readInt(); grade = in.readInt(); } int grade; // the grade being processed.
CS 100Lecture 49 Task 2: Count the grades where : count = 0; count = count + 1; System.out.println( count ); /* Print count of grades input up to a stopping signal of -1. */ grade = in.readInt(); grade = in.readInt(); while (grade != -1 ) while (grade != -1 ) { grade = in.readInt(); grade = in.readInt(); } int grade; // the grade being processed. int count; // # of grades so far.
CS 100Lecture 410 Task 3: Print the average where : count = 0; sum = 0; : count = count + 1; sum = sum + grade; : System.out.println( sum / count ); /* Print average of grades input up to a stopping signal of -1. */ grade = in.readInt(); grade = in.readInt(); while (grade != -1 ) while (grade != -1 ) { grade = in.readInt(); grade = in.readInt(); } int grade; // the grade being processed. int count; // # of grades so far. int sum; // sum of grades so far.
CS 100Lecture 411 Casts and Rounding This statement prints the average using integer division, e.g. 7/2 is 3 : This statement prints the average using integer division, e.g. 7/2 is 3 : : System.out.println ( sum/count ); This statement prints the average using “double precision floating point division”, e.g., (double)7/2 is 3.5 : This statement prints the average using “double precision floating point division”, e.g., (double)7/2 is 3.5 : ’ : System.out.println ( (double)sum/count (double)sum/count ); ); n An expression of the form (type)expression (type)expression is a cast, which converts the value of expression to a value of the given type, if possible. is a cast, which converts the value of expression to a value of the given type, if possible. This statement prints the average as a “rounded double precision floating point number”, e.g., Math.round((double)7/2) is 4 : This statement prints the average as a “rounded double precision floating point number”, e.g., Math.round((double)7/2) is 4 : ’’ : System.out.println ( Math.round((double)sum / count) Math.round((double)sum / count) ); );
CS 100Lecture 412 Boundary Conditions n What if no one takes the exam? ’’’: if ( count == 0 ) if ( count == 0 ) System.out.println(”No grades”); System.out.println(”No grades”); else else System.out.println( System.out.println( ”Average: ” + (double)sum / count ”Average: ” + (double)sum / count ); );
CS 100Lecture 413 Task 4: Print highest grade where : high = 0; if (grade > high) high = grade; ’ high = Math.max(grade, high); System.out.println( high ); /* Print max of grades input up to a stopping signal of -1. */ grade = in.readInt(); grade = in.readInt(); while (grade != -1 ) while (grade != -1 ) { grade = in.readInt(); grade = in.readInt(); } int grade; // the grade being processed. int high; // highest grade so far.
CS 100Lecture 414 Boundary Conditions Again n What if no one takes the exam? ’’’ : if ( count == 0 ) if ( count == 0 ) System.out.println(”No grades”); System.out.println(”No grades”); else else System.out.println(”High: ” + high); System.out.println(”High: ” + high); (where we are anticipating that there will be a variable count in the final program)
CS 100Lecture 415 Task 5: Putting it all together int grade; // the grade being processed. int count; // # of grades so far. int sum; // sum of grades so far. int high; // highest grade so far. /* Print statistics on 0 or more grades in the range 0-100, followed by a stopping signal of -1. */ import java.io.*; public class CUCSApplication { public static void main(String args[]) { declarations // Initialize Text object in to read // from standard input. TokenReader in = new TokenReader(System.in); statements } where declarations are: and where statements are:
CS 100Lecture 416 Task 5: Putting it all together, cont. /* Print statistics on grades input up to, but not including, a stopping signal of -1. */ count = 0; count = 0; sum = 0; sum = 0; high = 0; high = 0; grade = in.readInt(); grade = in.readInt(); while (grade != -1 ) while (grade != -1 ) { System.out.println(”Grade: ” + grade); System.out.println(”Grade: ” + grade); count = count + 1; count = count + 1; sum = sum + grade; sum = sum + grade; high = Math.max(grade, high); high = Math.max(grade, high); grade = in.readInt(); grade = in.readInt(); } if (count == 0) if (count == 0) System.out.println(“No grades”); System.out.println(“No grades”); else { else { System.out.println(”#grades: ” + count); System.out.println(”#grades: ” + count); System.out.println ( System.out.println ( ”Average: ” + (double)sum / count ”Average: ” + (double)sum / count ); ); System.out.println (”High: ” + high); System.out.println (”High: ” + high); }