1 / 12

CS100J Lecture 11

CS100J Lecture 11. Previous Lecture Scope of names and the lifetime of variables blocks and local variables methods and parameters classes, class variables, and instance variables This Lecture Java Constructs arrays and indexing increment and decrement for -statements Reading:

jgervais
Download Presentation

CS100J Lecture 11

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS100J Lecture 11 • Previous Lecture • Scope of names and the lifetime of variables • blocks and local variables • methods and parameters • classes, class variables, and instance variables • This Lecture • Java Constructs • arrays and indexing • increment and decrement • for-statements • Reading: • Lewis and Loftus, Sections 3.8 and 6.1-6.2 • Savitch, Sections 6.1 – 6.3 Lecture 11

  2. Motivating Example • Given a list of 0 or more grades (between 0 and 100) followed by -1, print a histogram of the grades, i.e., a table of grades and their frequencies. grade frequency 0 0 1 0 2 1 … … 99 18 100 13 • Is there a program pattern that applies? • What must be done for each grade? • What must be done before reading any grades? • What must be done after reading all grades? • How many separate counters are needed? Lecture 11

  3. A Relevant Pattern int grade; // the grade being processed. /* “Process” grades until (but not including) a stopping signal of -1. */ a grade = in.readInt(); while (grade != -1 ) { b grade = in.readInt(); } g • where • a : /* Initialize 101 counters to 0. */ • b : /* Increment counter for grade. */ • g : /* Print histogram. */ Lecture 11

  4. A Tedious Solution int grade; // the grade being processed. // For i in 0..100, freqi is # of grades of i. int freq0, freq1, . . ., freq100; /* “Process” grades until (but not including) a stopping signal of -1. */ /* Initialize 101 counters to 0. */ freq0 = 0; freq1 = 0; . . .; freq100 = 0; grade = in.readInt(); while (grade != -1 ) { /* Increment counter for grade. */ if ( grade == 0 ) freq0 = freq0 + 1; else if ( grade == 1 ) freq1 = freq1 + 1; else . . . if ( grade == 100 ) freq100 = freq100 + 1; grade = in.readInt(); } /* Print histogram. */ System.out.println(”Grade Frequency”); System.out.println( 0 + ” ” + freq0 ); System.out.println( 1 + ” ” + freq1 ); . . . System.out.println( 100 + ” ” + freq100 ); Lecture 11

  5. An Elegant Solution int grade; // the grade being processed. // For i in 0..100, freq[i] is # of grades of i. int[] freq = new int[101]; /* “Process” grades until (but not including) a stopping signal of -1. */ /* Initialize 101 counters to 0. */ grade = 0; while ( grade<=100 ) { freq[grade] = 0; grade = grade + 1; } grade = in.readInt(); while (grade != -1 ) { /* Increment counter for grade. */ freq[grade] = freq[grade] + 1; grade = in.readInt(); } /* Print histogram. */ System.out.println(”Grade Frequency”); grade = 0; while ( grade <= 100 ){ System.out.println( grade + ” ” + freq[grade] ); grade = grade + 1; } Lecture 11

  6. Trace Input Data 100 99 100 2 . . . -1 grade 0 1 2 99 100 freq . . . Lecture 11

  7. Array Declaration and Construction • An array is an object that consists of a collection of variables (its elements) all with the same type, e.g., array-of-int objects. • The declaration: int[] identifier ; declares identifier, a variable that can contain a reference to an array-of-int object. • The constructor invocation: new int[ length ] constructs an array-of-int object that consists of length int variables with subscripts 0 through length-1 • Thus, the initialized declaration int[] identifier = new int[ length ]; creates the following structure: identifier 0 length-1 . . . Lecture 11

  8. The Index Operator [] • The phrase identifier [ expression ] provides indexed access to an element of the array referred to by identifier, namely, access to the element with subscript equal to the value of expression. • Examples: freq[1+2+3] = 17; // set freq[6] to 17. grade = 6; freq [grade] = 0; // set freq[6] to 0. // Increment freq[6] by 1. freq [grade] = freq[grade] + 1; Lecture 11

  9. Subscripts • The phrase identifier[expression] is likeidentifier.field-name except that the “field name” is an integer computed by evaluating the expression. • Subscripts can be specified by arbitrary expressions. The value of the subscript is obtained by evaluating the expression. • The value of the subscript must be in the range 0 through one less than the number of elements in the array. • Do not confuse the subscript of an array element with the value contained in the array element. Lecture 11

  10. Some Shorthand • Increment and Decrement Operations variable++ is equivalent to variable = variable + 1 Similarly, variable -- is equivalent to variable = variable - 1 • Example: freq[grade]++ ; Lecture 11

  11. More Shorthand • The statement for (initialization; condition; increment) statement is shorthand for initialization; while ( condition ) { statement ; increment ; } Example: for (grade=0; grade<=100; grade++) freq[grade] = 0; Lecture 11

  12. An Elegant Solution, revisited int grade; // the grade being processed. // For i in 0..100, freq[i] is # of grades of i. int[] freq = new int[101]; /* “Process” grades until (but not including) a stopping signal of -1. */ /* Initialize 101 counters to 0. */ for ( grade = 0; grade<=100; grade++ ) freq[grade] = 0; grade = in.readInt(); while (grade != -1 ) { /* Increment counter for grade. */ freq[grade]++; grade = in.readInt(); } /* Print histogram. */ System.out.println(”Grade Frequency”); for ( grade = 0; grade<=100; grade++ ) System.out.println( grade + ” ” + freq[grade] ); Lecture 11

More Related