COS 260 DAY 9 Tony Gauvin.

Slides:



Advertisements
Similar presentations
Grouping objects Iterators. Iterator and iterator() Collections have an iterator() method. This returns an Iterator object. Iterator has three methods:
Advertisements

1 Features of the collection It increases its capacity as necessary. It keeps a private count: –size() accessor. It keeps the objects in order. Details.
Grouping objects Introduction to collections 5.0.
Grouping Objects 3 Iterators, collections and the while loop.
More sophisticated behavior Using library classes to implement some more advanced functionality 4.0.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Grouping Objects 2 Collections and the for-each loop Collections and the while loop.
Grouping objects Collections and iterators. 04/11/2004Lecture 4: Grouping Objects2 Main concepts to be covered Collections Loops Iterators Arrays.
More sophisticated behavior Using library classes to implement some more advanced functionality 3.0.
Modul 3 Collections af objekter Arraylist Collections Objektorienteret design og Java. 4.0.
Programming with Collections Grouping & Looping - Collections and Iteration Week 7.
Grouping objects Collections and iterators. Main concepts to be covered Collections Loops Iterators.
Objects First With Java A Practical Introduction Using BlueJ Grouping objects Collections and iterators 2.0.
OBJECT INTERACTION CITS1001. Overview Coupling and Cohesion Internal/external method calls null objects Chaining method calls Class constants Class variables.
Grouping objects Introduction to collections 5.0.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
Grouping objects Collections and iterators Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main.
Chapter 4 Grouping Objects. Flexible Sized Collections  When writing a program, we often need to be able to group objects into collections  It is typical.
JAVA COLLECTIONS M. TAIMOOR KHAN (ADAPTED FROM SWINBURNE NOTES)
Grouping objects Iterators. Iterator type Third variation to iterate over a collection Uses a while loop and Iterator object But NO integer index variable.
Objects First With Java A Practical Introduction Using BlueJ Grouping objects Collections and iterators 1.0.
Chapter 4 Grouping Objects. Flexible Sized Collections  When writing a program, we often need to be able to group objects into collections  It is typical.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
Grouping objects Arrays. 2 Fixed-size collections The maximum collection size may be pre-determined with an upper limit Array is an fixed-size collection.
Grouping objects Introduction to collections 5.0.
1 COS 260 DAY 7 Tony Gauvin. 2 Agenda Questions? 3 rd Mini quiz next class (September 28) –Chap 3 concepts Assignment 1 Corrected Assignment 2 posted.
Comparing ArrayLists and Arrays. ArrayLists ArrayLists are one type of flexible-size collection classes supported by Java –ArrayLists increase and decrease.
1 COS 260 DAY 9 Tony Gauvin. 2 Agenda Questions? 3 rd Mini quiz –Good results ->All A’s –Threw out question on name overloading 4 th Mini quiz next class.
Grouping objects Iterators, collections and the while loop Equality and Equality == and equals(…)
Grouping objects Introduction to collections 6.0.
Functional Processing of Collections (Advanced) 6.0.
Coming up ArrayList ArrayList vs Array – Declaration – Insertion – Access – Removal Wrapper classes Iterator object.
1 © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. for-each PROS easy to use access to ALL items one-by-one ability to change the state.
for-each PROS CONS easy to use access to ALL items one-by-one
Objects First with Java CITS1001 week 4
Objects First with Java Introduction to collections
Fixed-sized collections
The switch Statement, and Introduction to Looping
COS 260 DAY 7 Tony Gauvin.
Object INTERACTION CITS1001 week 3.
Functional Processing of Collections (Advanced)
COS 260 DAY 17 Tony Gauvin.
COS 260 DAY 23 Tony Gauvin.
Understanding class definitions
More sophisticated behavior
4 Grouping objects (cont.)
Objects First with Java Introduction to collections
COS 260 DAY 13 Tony Gauvin.
COS 260 DAY 6 Tony Gauvin.
COS 260 DAY 19 Tony Gauvin.
COS 260 DAY 27 Tony Gauvin.
"First things first, but not necessarily in that order " -Dr. Who
COS 260 DAY 11 Tony Gauvin.
COS 260 DAY 16 Tony Gauvin.
COS 260 DAY 10 Tony Gauvin.
COS 260 DAY 2 Tony Gauvin.
COS 260 DAY 18 Tony Gauvin.
COS 260 DAY 8 Tony Gauvin.
COS 260 DAY 8 Tony Gauvin.
Objects First with Java Introduction to collections
Exercise 1 Declare a constant of type int called SIZE and initialize it to value 10 Declare two int arrays of size “SIZE” Assume that those int arrays.
COS 260 DAY 16 Tony Gauvin.
COS 260 DAY 19 Tony Gauvin.
COS 260 DAY 11 Tony Gauvin.
Collections and iterators
COS 260 DAY 23 Tony Gauvin.
COS 260 DAY 14 Tony Gauvin.
COS 260 DAY 4 Tony Gauvin.
COS 260 DAY 23 Tony Gauvin.
Collections and iterators
Presentation transcript:

COS 260 DAY 9 Tony Gauvin

Agenda Finish Grouping Objects Questions Assignment 2 posted Due September 29 Assignment 3 posted soon MiniQuiz 3 Graded Miniquiz 4 is on Friday, September 29 Covers Chap 4 Finish Grouping Objects

Capstone Project Capstone Project Description Fall 2017.pdf October 2 Proposal Due E-mailed to Tony in Blackboard October 27 Progress Report E-mailed to Tony in Blackboard November 16 Progress Report E-mailed to Tony in Blackboard November 30 Progress Report E-mailed to Tony in Blackboard December 12@8AM All Deliverables & Presentation Due Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

Objects First with Java Introduction to collections Grouping objects Introduction to collections 6.0 © David J. Barnes and Michael Kölling

Side note: String equality Objects First with Java Side note: String equality if(input == "bye") { ... } if(input.equals("bye")) { Important: Always use .equals for testing String equality! tests identity Do not use!! tests equality © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

Objects First with Java Identity vs equality 1 Other (non-String) objects: :Person :Person “Fred” “Jill” == is not true here (of course) person1 person2 person1 == person2 ? © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

Objects First with Java Identity vs equality 2 Other (non-String) objects: :Person :Person “Fred” “Fred” == is still not true here (different objects, == tests identity) person1 person2 person1 == person2 ? © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

Objects First with Java Identity vs equality 3 Other (non-String) objects: :Person :Person “Fred” “Fred” == is true now (same object) person1 person2 person1 == person2 ? © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

Identity vs equality (Strings) Objects First with Java Identity vs equality (Strings) String input = reader.getInput(); if(input == "bye") { ... } == tests identity :String :String == ? "bye" "bye" == is still not true here (different objects, == tests identity) input false! © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

Identity vs equality (Strings) Objects First with Java Identity vs equality (Strings) String input = reader.getInput(); if(input.equals("bye")) { ... } equals tests equality :String :String ? equals "bye" "bye" == is still not true here (different objects, == tests identity) input true! © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

While without a collection Objects First with Java While without a collection // Print all even numbers from 2 to 30. int index = 2; while(index <= 30) { System.out.println(index); index = index + 2; } Any boolean expression can be used to control a while loop. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

Do- while Java also has a do-while statement do{ // statements }while(boolean-expression; Guaranteed to perform the statement block at least once © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Moving away from String Our collection of String objects for music tracks is limited. No separate identification of artist, title, etc. A Track class with separate fields: artist title filename © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Objects First with Java Iterator objects Grouping objects Iterator objects also explain: how to pass an ArrayList for testing © David J. Barnes and Michael Kölling

Iterator and iterator() Collections have an iterator() method. This returns an Iterator object. Iterator<E> has methods: boolean hasNext() E next() void remove()

Using an Iterator object Objects First with Java Using an Iterator object returns an Iterator object java.util.Iterator Iterator<ElementType> it = myCollection.iterator(); while(it.hasNext()) { call it.next() to get the next object do something with that object } public void listAllFiles() { Iterator<Track> it = files.iterator(); while(it.hasNext()) { Track tk = it.next(); System.out.println(tk.getDetails()); } © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

Objects First with Java Iterator mechanics © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

Objects First with Java myList:List myList.iterator() :Element :Element :Element :Element :Iterator © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

Objects First with Java myList:List :Element :Element :Element :Element :Iterator :Iterator ✔ hasNext()? next() Element e = iterator.next(); © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

Objects First with Java myList:List :Element :Element :Element :Element :Iterator :Iterator ✔ hasNext()? next() © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

Objects First with Java myList:List :Element :Element :Element :Element :Iterator :Iterator ✔ hasNext()? next() © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

Objects First with Java myList:List :Element :Element :Element :Element :Iterator :Iterator hasNext()? ✔ next() © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

Objects First with Java myList:List :Element :Element :Element :Element :Iterator ✗ hasNext()? © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

Objects First with Java Index versus Iterator Ways to iterate over a collection: for-each loop. Use if we want to process every element. while loop. Use if we might want to stop part way through. Use for repetition that doesn't involve a collection. Iterator object. Often used with collections where indexed access is not very efficient, or impossible. Use to remove from a collection. Iteration is an important programming pattern. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

Removing from a collection Iterator<Track> it = tracks.iterator(); while(it.hasNext()) { Track t = it.next(); String artist = t.getArtist(); if(artist.equals(artistToRemove)) { it.remove(); } Using the Iterator’s remove method. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Removing from a collection – wrong! int index = 0; while(index < tracks.size()) { Track t = tracks.get(index); String artist = t.getArtist(); if(artist.equals(artistToRemove)) { tracks.remove(index); } index++; Can you spot what is wrong? Lets do Exercise 4.39 page 162 © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Objects First with Java Review Loop statements allow a block of statements to be repeated. The for-each loop allows iteration over a whole collection. The while loop allows the repetition to be controlled by a boolean expression. All collection classes provide special Iterator objects that provide sequential access to a whole collection. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

The auction project © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Objects First with Java The auction project The auction project provides further illustration of collections and iteration. Examples of using null. Anonymous objects. Chaining method calls. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

null Used with object types. Used to indicate, 'no object'. We can test if an object variable holds the null value: if(highestBid == null) … Used to indicate ‘no bid yet’. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Anonymous objects Objects are often created and handed on elsewhere immediately: Lot furtherLot = new Lot(…); lots.add(furtherLot); We don’t really need furtherLot: lots.add(new Lot(…)); © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Chaining method calls Methods often return objects. We often immediately call a method on the returned object. Bid bid = lot.getHighestBid(); Person bidder = bid.getBidder(); We can use the anonymous object concept and chain method calls: lot.getHighestBid().getBidder() © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Chaining method calls Each method in the chain is called on the object returned from the previous method call in the chain. String name = lot.getHighestBid().getBidder().getName(); Returns a Bid object from the Lot Returns a Person object from the Bid Returns a String object from the Person © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Objects First with Java Review Collections are used widely in many different applications. The Java library provides many different ‘ready made’ collection classes. Collections are often manipulated using iterative control structures. The while loop is the most important control structure to master. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

Review Some collections lend themselves to index-based access; e.g. ArrayList. Iterator provides a versatile means to iterate over different types of collection. Removal using an Iterator is less error-prone in some circumstance. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.