Download presentation
Presentation is loading. Please wait.
Published byBeatrice Spencer Modified over 8 years ago
1
CS 100Lecture 111 CS100J Lecture 11 n Previous Lecture –Scope of names and the lifetime of variables n blocks and local variables n methods and parameters n classes, class variables, and instance variables n This Lecture –Java Constructs n arrays and indexing n increment and decrement n for-statements –Reading: n Lewis and Loftus, Sections 3.8 and 6.1-6.2 n Savitch, Sections 6.1 – 6.3
2
CS 100Lecture 112 Motivating Example n 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. gradefrequency 00 10 21 …… 9918 10013 n Is there a program pattern that applies? n What must be done for each grade? n What must be done before reading any grades? n What must be done after reading all grades? n How many separate counters are needed?
3
CS 100Lecture 113 A Relevant Pattern where : /* Initialize 101 counters to 0. */ /* Increment counter for grade. */ /* Print histogram. */ /* “Process” grades until (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.
4
CS 100Lecture 114 A Tedious Solution int grade; // the grade being processed. // For i in 0..100, freqi is # of grades of i. int freq0, freq1,..., freq100; int freq0, freq1,..., freq100; /* “Process” grades until (but not including) a stopping signal of -1. */ /* Initialize 101 counters to 0. */ /* Initialize 101 counters to 0. */ freq0 = 0; freq1 = 0;...; freq100 = 0; grade = in.readInt(); grade = in.readInt(); while (grade != -1 ) { while (grade != -1 ) { /* Increment counter for grade. */ /* 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(); grade = in.readInt(); } /* Print histogram. */ /* Print histogram. */ System.out.println(”Grade Frequency”); System.out.println(”Grade Frequency”); System.out.println( 0 + ” ” + freq0 ); System.out.println( 0 + ” ” + freq0 ); System.out.println( 1 + ” ” + freq1 ); System.out.println( 1 + ” ” + freq1 );... System.out.println( 100 + ” ” + freq100 ); System.out.println( 100 + ” ” + freq100 );
5
CS 100Lecture 115 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]; int[] freq = new int[101]; /* “Process” grades until (but not including) a stopping signal of -1. */ /* Initialize 101 counters to 0. */ /* Initialize 101 counters to 0. */ grade = 0; while ( grade<=100 ) { while ( grade<=100 ) { freq[grade] = 0; grade = grade + 1; } grade = in.readInt(); grade = in.readInt(); while (grade != -1 ) { while (grade != -1 ) { /* Increment counter for grade. */ /* Increment counter for grade. */ freq[grade] = freq[grade] + 1; grade = in.readInt(); grade = in.readInt(); } /* Print histogram. */ /* Print histogram. */ System.out.println(”Grade Frequency”); System.out.println(”Grade Frequency”); grade = 0; grade = 0; while ( grade <= 100 ){ while ( grade <= 100 ){ System.out.println( System.out.println( grade + ” ” + freq[grade] grade + ” ” + freq[grade] ); ); grade = grade + 1; }
6
CS 100Lecture 116 Trace Input Data 100 99 100 2... -1 grade 0 1 2 99 100 0 1 2 99 100freq...
7
CS 100Lecture 117 Array Declaration and Construction n 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. n The declaration: int[] identifier ; int[] identifier ; declares identifier, a variable that can contain a reference to an array-of-int object. n The constructor invocation: new int[ length ] constructs an array-of-int object that consists of length int variables with subscripts 0 through length -1 n Thus, the initialized declaration int[] identifier = new int[ length ]; creates the following structure: identifier 0 length-1...
8
CS 100Lecture 118 The Index Operator [] n The phrase identifier [ expression ] 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. n 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; freq [grade] = freq[grade] + 1;
9
CS 100Lecture 119 Subscripts The phrase identifier[expression] is like identifier.field-name except that the “field name” is an integer computed by evaluating the expression. The phrase identifier[expression] is like identifier.field-name except that the “field name” is an integer computed by evaluating the expression. n Subscripts can be specified by arbitrary expressions. The value of the subscript is obtained by evaluating the expression. n The value of the subscript must be in the range 0 through one less than the number of elements in the array. n Do not confuse the subscript of an array element with the value contained in the array element.
10
CS 100Lecture 1110 Some Shorthand n Increment and Decrement Operations variable ++ variable ++ is equivalent to variable = variable + 1 variable = variable + 1Similarly, variable -- is equivalent to variable = variable - 1 n Example: freq[grade]++ ;
11
CS 100Lecture 1111 More Shorthand n The statement for (initialization; condition; increment) for (initialization; condition; increment) statement statement is shorthand for initialization; while ( condition ) { statement ; increment ; } Example: for (grade=0; grade<=100; grade++) freq[grade] = 0;
12
CS 100Lecture 1112 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]; int[] freq = new int[101]; /* “Process” grades until (but not including) a stopping signal of -1. */ /* Initialize 101 counters to 0. */ /* Initialize 101 counters to 0. */ for ( grade = 0; grade<=100; grade++ ) for ( grade = 0; grade<=100; grade++ ) freq[grade] = 0; grade = in.readInt(); grade = in.readInt(); while (grade != -1 ) { while (grade != -1 ) { /* Increment counter for grade. */ /* Increment counter for grade. */freq[grade]++; grade = in.readInt(); grade = in.readInt(); } /* Print histogram. */ /* Print histogram. */ System.out.println(”Grade Frequency”); System.out.println(”Grade Frequency”); for ( grade = 0; grade<=100; grade++ ) for ( grade = 0; grade<=100; grade++ ) System.out.println( System.out.println( grade + ” ” + freq[grade] grade + ” ” + freq[grade] ); );
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.