Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Coding 6 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Collections.

Similar presentations


Presentation on theme: "Java Coding 6 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Collections."— Presentation transcript:

1 Java Coding 6 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email: david@bilkent.edu.tr Collections

2 IMPORTANT… Students… This presentation is designed to be used in class as part of a guided discovery sequence. It is not self- explanatory! Please use it only for revision purposes after having taken the class. Simply flicking through the slides will teach you nothing. You must be actively thinking, doing and questioning to learn! Instructors… You are free to use this presentation in your classes and to make any modifications to it that you wish. All I ask is an email saying where and when it is/was used. I would also appreciate any suggestions you may have for improving it. thank you, David. David

3 MusicCD Class Example MusicCD( title, artist, tracks) StringgetTitle() StringgetArtist() TrackgetTrack(int) intgetDuration() DategetReleaseDate() Stringtitle StringArtist DatereleaseDate ???tracks Date( String) StringtoString() intyear intmonth intday Track( title, length) StringgetTitle() intgetLength() Stringtitle intlength collection of

4 Easy Collections Use Java’s ArrayList class ArrayList() booleanadd(Object) voidadd(int, Object) intsize() Objectget(int) Objectremove(int) Objectset(int, Object) Objectclone() StringtoString() booleanequals( Object) ArrayList An object of the ArrayList class provides a container which can hold any number of other objects… NEAT! Objects arranged in a sequence: LIST [ milk, eggs, bread, honey ]

5 Easy Problem Read in a set of positive integer values and then print out a table showing the average, each of the values and their difference from the average. Average is 5 ValueDiff -------------- 105 3-2 61 1-4 -------------- Example output… Umm… must remember all the values we read in in order to print the table. Could use ArrayList… BUT integers are not Objects! (use Integer wrapper class)

6 Not-so-easy Collections Arrays Common data structure All elements of same type Are Objects in Java Basis of ArrayList class! 365110 12340 grades Each element has unique successor & predecessor (except first & last.) Each element identified by an index (label/subscript) Name for entire structure

7 Array Syntax (1) Arrays are Objects, so declare variable then instantiate 365110 12340 grades type[] variableName ; variableName = new type[ noOfElements ]; int[] grades; grades = new int[5]; Note use of square brackets! Array size cannot be changed after creation!

8 Array Syntax (2) Referring to an individual element variableName[index] examples grades[0]grades[ i] grades[1]grades[ i+1] names[99]names[ FIRST] grades[0] = 10; grades[1] = grades[0] + 2; System.out.println( grades[0]); names[99] = scan.nextLine(); Where index is a literal, named constant, variable, or expression.

9 Processing all elements e.g. Printing contents of array grades for ( int i = 0; i < grades.length; i++) System.out.println( grades[i] ); for each int k in grades array print k for ( int i = 0; i < ___________; i++) System.out.println( grades[i] ); System.out.println( grades[0] ); System.out.println( grades[1] ); : // alternate for syntax for ( int k : grades) System.out.println( k);

10 Easy Problem using arrays! Printing table of differences from average 1. read set of values 2. compute average of set of values 3. print table of differences using average & set of values Steps 2 & 3 are straightforward For step 1 need to know how many values Fixed, e.g. 5 Ask user Use sentinel - but length of array is fixed!

11 Data Requirements: average – double setOfValues – int[] Easy Problem with Methods! Identify method signatures from algorithm 1. read set of values 2. compute average of set of values 3. print table of differences using average & set of values int[] readSetOfValues() double computeAverage( int[] setOfValues) void printTable( double average, int[] setOfValues) Note: Object-type parameters can act as outputs too!

12 Arrays of objects Array contains only references to objects Track[] tracks; tracks = new Track[5]; tracks[0] = new Track( “David”, 100); tracks[1] = new Track( “Gunes”, 200); Still need to create actual objects 12340 tracks David 100 Gunes 200 tracks[0].getTitle() tracks[4].getTitle()


Download ppt "Java Coding 6 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Collections."

Similar presentations


Ads by Google