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

Slides:



Advertisements
Similar presentations
Java Coding 8 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Object-Oriented Design Example - The.
Advertisements

Java Coding OOP David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Towards Event-driven programming &
Everyday Algorithms David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. lightning introduction to.
Introduction to UML David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. …Unified Modeling Language.
CSE 1341 Honors Professor Mark Fontenot Southern Methodist University Note Set 17.
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
Introduction to arrays Data in economy size packages.
Grouping Objects Arrays and for loops. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Fixed-Size Collections.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
Java Coding 6 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Collections.
Java Coding 2 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Decisions, decisions…!
Introduction to Computers and Programming Lecture 15: Arrays Professor: Evan Korth New York University.
Chapter 7 Arrays. © 2004 Pearson Addison-Wesley. All rights reserved7-2 Arrays Arrays are objects that help us organize large amounts of information Chapter.
1 More on Arrays Passing arrays to or from methods Arrays of objects Command line arguments Variable length parameter lists Two dimensional arrays Reading.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
Lecture 15 Arrays: Part 1 COMP1681 / SE15 Introduction to Programming.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
Java Coding 3 David Davenport Computer Eng. Dept.,
Java Coding 8 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Object-Oriented Design Examples.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
Review: OOP & Arrays David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. …from CS101.
Arrays, Loops weeks 4-6 (change from syllabus for week 6) Chapter 4.
Java Coding David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey.
Java Coding 4 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Method madness.
CS0007: Introduction to Computer Programming Introduction to Arrays.
© 2011 Pearson Education, publishing as Addison-Wesley 1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 6 focuses.
Java Coding 5 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. To object or not…
Java Coding 3 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Over & over again!
Lecture 12 Instructor: Craig Duckett ARRAYS. Announcements Assignment 3 Assignment 3 Revision Assignment 4 (and Final Exam) GRADED! RETURNED! Woot! NEXT.
Java Coding 5 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. To object or not…
From Algorithms to Architecture David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. lightning introduction.
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Pemrograman Dasar Arrays PTIIK - UB. Arrays  An array is a container object that holds a fixed number of values of a single type.  The length of an.
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Computational Algorithms David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. lightning introduction.
Review of ICS 102. Lecture Objectives To review the major topics covered in ICS 102 course Refresh the memory and get ready for the new adventure of ICS.
Java Coding David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Syntax for Variables & Constants Input,
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Java Coding OOP_3 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Some important Java interfaces +
Array - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus 12/19/20151.
Java Coding 8 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Object-Oriented Design Examples.
CSE 1201 Object Oriented Programming ArrayList 1.
Java Coding 5 – Part 2 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. To object or not…
BIT115: Introduction to Programming
Introducing Arrays. Too Many Variables?  Remember, a variable is a data structure that can hold a single value at any given time.  What if I want to.
For Friday Read No quiz Program 6 due. Program 6 Any questions?
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 9: Arrays; Revision Session.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Java Coding 6 David Davenport Computer Eng. Dept.,
Arrays of Objects October 9, 2006 ComS 207: Programming I (in Java)
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
Chapter VII: Arrays.
Java Coding – part 2 David Davenport Computer Eng. Dept.,
Java Coding 3 – part2 David Davenport Computer Eng. Dept.,
Java Coding 3 David Davenport Computer Eng. Dept.,
Java Coding 5 – Part 2 David Davenport Computer Eng. Dept.,
Java Coding 4 David Davenport Computer Eng. Dept.,
Java Coding 6-extra David Davenport Computer Eng. Dept.,
CS2011 Introduction to Programming I Arrays (I)
Java Coding 6 – part2 David Davenport Computer Eng. Dept.,
Suggested self-checks: Section 7.11 #1-11
Java Coding 4 (part2) David Davenport Computer Eng. Dept.,
Java Coding 6_part3 David Davenport Computer Eng. Dept.,
Arrays of Objects October 8, 2007 ComS 207: Programming I (in Java)
Java Coding 6 David Davenport Computer Eng. Dept.,
Java Coding 6 David Davenport Computer Eng. Dept.,
Java Coding 5 – Part 2 David Davenport Computer Eng. Dept.,
Presentation transcript:

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

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 saying where and when it is/was used. I would also appreciate any suggestions you may have for improving it. thank you, David. David

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

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 ]

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 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)

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

Array Syntax (1) Arrays are Objects, so declare variable then instantiate 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!

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.

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);

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!

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!

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 tracks David 100 Gunes 200 tracks[0].getTitle() tracks[4].getTitle()