Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS100J Lecture 11 Previous Lecture This Lecture

Similar presentations


Presentation on theme: "CS100J Lecture 11 Previous Lecture This Lecture"— Presentation transcript:

1 CS100J Lecture 11 Previous Lecture This 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 Savitch, Sections 6.1 – 6.3 CS 100 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? CS 100 Lecture 11

3 A Relevant Pattern where 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 } g where a : /* Initialize 101 counters to 0. */ b : /* Increment counter for grade. */ g : /* Print histogram. */ CS 100 Lecture 11

4 A Tedious Solution int grade; // the grade being processed.
// For i in , 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 = freq ; } /* Print histogram. */ System.out.println(”Grade Frequency”); System.out.println( 0 + ” ” + freq0 ); System.out.println( 1 + ” ” + freq1 ); System.out.println( ” ” + freq100 ); CS 100 Lecture 11

5 An Elegant Solution int grade; // the grade being processed.
// For i in , 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; /* Print histogram. */ System.out.println(”Grade Frequency”); while ( grade <= 100 ){ System.out.println( grade + ” ” + freq[grade] ); CS 100 Lecture 11

6 Trace . . . Input Data 100 99 100 2 . . . -1 grade 0 1 2 99 100 freq
freq . . . CS 100 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 length-1 . . . CS 100 Lecture 11

8 The Index Operator [] Examples: 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; CS 100 Lecture 11

9 Subscripts The phrase identifier[expression] is like identifier.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. CS 100 Lecture 11

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

11 More Shorthand is shorthand for 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; CS 100 Lecture 11

12 An Elegant Solution, revisited
int grade; // the grade being processed. // For i in , 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]++; } /* Print histogram. */ System.out.println(”Grade Frequency”); System.out.println( grade + ” ” + freq[grade] ); CS 100 Lecture 11


Download ppt "CS100J Lecture 11 Previous Lecture This Lecture"

Similar presentations


Ads by Google