SlideShare a Scribd company logo
1 of 30
Objects First With Java
A Practical Introduction Using BlueJ
Grouping objects
Collections and iterators
2.0
2Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
Main concepts to be covered
• Collections
• Loops
• Iterators
• Arrays
3Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
The requirement to group
objects
• Many applications involve collections of
objects:
– Personal organizers.
– Library catalogs.
– Student-record system.
• The number of items to be stored varies.
– Items added.
– Items deleted.
4Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
A personal notebook
• Notes may be stored.
• Individual notes can be viewed.
• There is no limit to the number of
notes.
• It will tell how many notes are
stored.
• Explore the notebook1 project.
5Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
Class libraries
• Collections of useful classes.
• We don’t have to write everything
from scratch.
• Java calls its libraries, packages.
• Grouping objects is a recurring
requirement.
– The java.util package contains
classes for doing this.
6Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
import java.util.ArrayList;
/**
* ...
*/
public class Notebook
{
// Storage for an arbitrary number of notes.
private ArrayList notes;
/**
* Perform any initialization required for the
* notebook.
*/
public Notebook()
{
notes = new ArrayList();
}
...
}
7Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
Object structures with
collections
8Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
Adding a third note
9Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
Features of the collection
• It increases its capacity as necessary.
• It keeps a private count (size()
accessor).
• It keeps the objects in order.
• Details of how all this is done are
hidden.
– Does that matter? Does not knowing how
prevent us from using it?
10Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
Using the collection
public class Notebook
{
private ArrayList notes;
...
public void storeNote(String note)
{
notes.add(note);
}
public int numberOfNotes()
{
return notes.size();
}
...
}
Adding a new note
Returning the number of notes
(delegation).
11Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
Index numbering
12Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
Retrieving an object
Index validity checks
Retrieve and print the note
public void showNote(int noteNumber)
{
if(noteNumber < 0) {
// This is not a valid note number.
}
else if(noteNumber < numberOfNotes()) {
System.out.println(notes.get(noteNumber));
}
else {
// This is not a valid note number.
}
}
13Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
Removal may affect
numbering
14Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
Review
• Collections allow an arbitrary number
of objects to be stored.
• Class libraries usually contain tried-
and-tested collection classes.
• Java’s class libraries are called
packages.
• We have used the ArrayList class
from the java.util package.
15Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
Review
• Items may be added and removed.
• Each item has an index.
• Index values may change if items are
removed (or further items added).
• The main ArrayList methods are
add, get, remove and size.
16Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
Iteration
• We often want to perform some actions an
arbitrary number of times.
– E.g., print all the notes in the notebook. How
many are there?
• Most programming languages include loop
statements to make this possible.
• Java has three sorts of loop statement.
– We will focus on its while loop.
17Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
While loop pseudo code
while(loop condition) {
loop body
}
while(there is at least one more note to be printed) {
show the next note
}
Boolean test
while keyword
Statements to be repeated
Pseudo-code example to print every note
General form of a while loop
18Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
A Java example
/**
* List all notes in the notebook.
*/
public void listNotes()
{
int index = 0;
while(index < notes.size()) {
System.out.println(notes.get(index));
index++;
}
}
Increment by one
19Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
Iterating over a collection
Iterator it = myCollection.iterator();
while(it.hasNext()) {
call it.next() to get the next object
do something with that object
}
java.util.Iterator
Returns an Iterator
object
public void listNotes()
{
Iterator it = notes.iterator();
while(it.hasNext()) {
System.out.println(it.next());
}
}
20Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
The auction project
• The auction project provides further
illustration of collections and
iteration.
• Two further points to follow up:
– The null value.
– Casting. Used to store the result of get
into a variable:
• String message = (String) notes.get(0);
21Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
Review
• Loop statements allow a block of
statements to be repeated.
• A Java while loop allows the
repetition to be controlled by a
boolean expression.
• Collection classes have special
Iterator objects that simplify
iteration over the whole collection.
22Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
Fixed-size collections
• Sometimes the maximum collection
size can be pre-determined.
• Programming languages usually offer
a special fixed-size collection type:
an array.
• Java arrays can store objects or
primitive-type values.
• Arrays use a special syntax.
23Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
The weblog-analyzer project
• Web server records details of each
access.
• Supports webmaster’s tasks.
– Most popular pages.
– Busiest periods.
– How much data is being delivered.
– Broken references.
• Analyze accesses by hour.
24Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
Creating an array object
public class LogAnalyzer
{
private int[] hourCounts;
private LogfileReader reader;
public LogAnalyzer()
{
hourCounts = new int[24];
reader = new LogfileReader();
}
...
}
Array object creation
Array variable declaration
25Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
The hourCounts array
26Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
Using an array
• Square-bracket notation is used to access
an array element: hourCounts[...]
• Elements are used like ordinary variables.
– On the left of an assignment:
• hourCounts[hour] = ...;
– In an expression:
• adjusted = hourCounts[hour] – 3;
• hourCounts[hour]++;
27Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
The for loop
• Similar to a while loop.
• Often used to iterate a fixed number
of times.
• Often used to iterate over an array.
28Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
For loop pseudo-code
for(initialization; condition; post-body action) {
statements to be repeated
}
General form of a for loop
Equivalent in while-loop form
initialization;
while(condition) {
statements to be repeated
post-body action
}
29Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
A Java example
for(int hour = 0; hour < hourCounts.length; hour++) {
System.out.println(hour + ": " + hourCounts[hour]);
}
int hour = 0;
while(hour < hourCounts.length) {
System.out.println(hour + ": " + hourCounts[hour]);
hour++;
}
for loop version
while loop version
30Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
Review
• Arrays are appropriate where a fixed-
size collection is required.
• Arrays use special syntax.
• For loops offer an alternative to
while loops when the number of
repetitions is known.
• For loops are often used to iterate
over arrays.

More Related Content

What's hot

Groovy DSLs (JavaOne Presentation)
Groovy DSLs (JavaOne Presentation)Groovy DSLs (JavaOne Presentation)
Groovy DSLs (JavaOne Presentation)Jim Driscoll
 
Programming Terminology
Programming TerminologyProgramming Terminology
Programming TerminologyMichael Henson
 
Java 102 intro to object-oriented programming in java - exercises
Java 102   intro to object-oriented programming in java - exercisesJava 102   intro to object-oriented programming in java - exercises
Java 102 intro to object-oriented programming in java - exercisesagorolabs
 
Oop in php lecture 2
Oop in  php lecture 2Oop in  php lecture 2
Oop in php lecture 2Mudasir Syed
 
Projects Valhalla, Loom and GraalVM at JUG Mainz
Projects Valhalla, Loom and GraalVM at JUG MainzProjects Valhalla, Loom and GraalVM at JUG Mainz
Projects Valhalla, Loom and GraalVM at JUG MainzVadym Kazulkin
 
MVC and Entity Framework
MVC and Entity FrameworkMVC and Entity Framework
MVC and Entity FrameworkJames Johnson
 
Session 02 - Elements of Java Language
Session 02 - Elements of Java LanguageSession 02 - Elements of Java Language
Session 02 - Elements of Java LanguagePawanMM
 
Vk.amberfog.com gtug part1_introduction2_javaandroid_gtug
Vk.amberfog.com gtug part1_introduction2_javaandroid_gtugVk.amberfog.com gtug part1_introduction2_javaandroid_gtug
Vk.amberfog.com gtug part1_introduction2_javaandroid_gtugketan_patel25
 
Function-and-prototype defined classes in JavaScript
Function-and-prototype defined classes in JavaScriptFunction-and-prototype defined classes in JavaScript
Function-and-prototype defined classes in JavaScriptHong Langford
 
New Features Of JDK 7
New Features Of JDK 7New Features Of JDK 7
New Features Of JDK 7Deniz Oguz
 
Session 05 - Strings in Java
Session 05 - Strings in JavaSession 05 - Strings in Java
Session 05 - Strings in JavaPawanMM
 
A Tour Through the Groovy Ecosystem
A Tour Through the Groovy EcosystemA Tour Through the Groovy Ecosystem
A Tour Through the Groovy EcosystemLeonard Axelsson
 

What's hot (20)

Sep 15
Sep 15Sep 15
Sep 15
 
Java Day-3
Java Day-3Java Day-3
Java Day-3
 
java review
java reviewjava review
java review
 
Groovy DSLs (JavaOne Presentation)
Groovy DSLs (JavaOne Presentation)Groovy DSLs (JavaOne Presentation)
Groovy DSLs (JavaOne Presentation)
 
Programming Terminology
Programming TerminologyProgramming Terminology
Programming Terminology
 
04 sorting
04 sorting04 sorting
04 sorting
 
Java 102 intro to object-oriented programming in java - exercises
Java 102   intro to object-oriented programming in java - exercisesJava 102   intro to object-oriented programming in java - exercises
Java 102 intro to object-oriented programming in java - exercises
 
Oop in php lecture 2
Oop in  php lecture 2Oop in  php lecture 2
Oop in php lecture 2
 
Java 103
Java 103Java 103
Java 103
 
Projects Valhalla, Loom and GraalVM at JUG Mainz
Projects Valhalla, Loom and GraalVM at JUG MainzProjects Valhalla, Loom and GraalVM at JUG Mainz
Projects Valhalla, Loom and GraalVM at JUG Mainz
 
MVC and Entity Framework
MVC and Entity FrameworkMVC and Entity Framework
MVC and Entity Framework
 
Session 02 - Elements of Java Language
Session 02 - Elements of Java LanguageSession 02 - Elements of Java Language
Session 02 - Elements of Java Language
 
Vk.amberfog.com gtug part1_introduction2_javaandroid_gtug
Vk.amberfog.com gtug part1_introduction2_javaandroid_gtugVk.amberfog.com gtug part1_introduction2_javaandroid_gtug
Vk.amberfog.com gtug part1_introduction2_javaandroid_gtug
 
Object Class
Object Class Object Class
Object Class
 
Function-and-prototype defined classes in JavaScript
Function-and-prototype defined classes in JavaScriptFunction-and-prototype defined classes in JavaScript
Function-and-prototype defined classes in JavaScript
 
Java Day-2
Java Day-2Java Day-2
Java Day-2
 
New Features Of JDK 7
New Features Of JDK 7New Features Of JDK 7
New Features Of JDK 7
 
Session 05 - Strings in Java
Session 05 - Strings in JavaSession 05 - Strings in Java
Session 05 - Strings in Java
 
hibernate
hibernatehibernate
hibernate
 
A Tour Through the Groovy Ecosystem
A Tour Through the Groovy EcosystemA Tour Through the Groovy Ecosystem
A Tour Through the Groovy Ecosystem
 

Similar to Groupingobject

What’s expected in Java 9
What’s expected in Java 9What’s expected in Java 9
What’s expected in Java 9Gal Marder
 
Java 102 intro to object-oriented programming in java
Java 102   intro to object-oriented programming in javaJava 102   intro to object-oriented programming in java
Java 102 intro to object-oriented programming in javaagorolabs
 
JavaScript in Object-Oriented Way
JavaScript in Object-Oriented WayJavaScript in Object-Oriented Way
JavaScript in Object-Oriented WayChamnap Chhorn
 
Scala in the Wild
Scala in the WildScala in the Wild
Scala in the WildTomer Gabel
 
Object Oriented Programming Tutorial.pptx
Object Oriented Programming Tutorial.pptxObject Oriented Programming Tutorial.pptx
Object Oriented Programming Tutorial.pptxethiouniverse
 
First adoption hackathon at BGJUG
First adoption hackathon at BGJUGFirst adoption hackathon at BGJUG
First adoption hackathon at BGJUGIvan Ivanov
 
Java 101 Intro to Java Programming - Exercises
Java 101   Intro to Java Programming - ExercisesJava 101   Intro to Java Programming - Exercises
Java 101 Intro to Java Programming - Exercisesagorolabs
 
java02.ppt
java02.pptjava02.ppt
java02.pptMENACE4
 
lecture-a-java-review.ppt
lecture-a-java-review.pptlecture-a-java-review.ppt
lecture-a-java-review.pptkavitamittal18
 
Java basics with datatypes, object oriented programming
Java basics with datatypes, object oriented programmingJava basics with datatypes, object oriented programming
Java basics with datatypes, object oriented programmingkalirajonline
 
lecture-a-java-review.ppt
lecture-a-java-review.pptlecture-a-java-review.ppt
lecture-a-java-review.pptNadiSarj2
 
lecture-a-java-review.ppt
lecture-a-java-review.pptlecture-a-java-review.ppt
lecture-a-java-review.pptJemarManatad1
 
lecture-a-java-review.ppt
lecture-a-java-review.pptlecture-a-java-review.ppt
lecture-a-java-review.pptSquidTurbo
 

Similar to Groupingobject (20)

4 gouping object
4 gouping object4 gouping object
4 gouping object
 
oop 3.pptx
oop 3.pptxoop 3.pptx
oop 3.pptx
 
ppt_on_java.pptx
ppt_on_java.pptxppt_on_java.pptx
ppt_on_java.pptx
 
What’s expected in Java 9
What’s expected in Java 9What’s expected in Java 9
What’s expected in Java 9
 
Java 102 intro to object-oriented programming in java
Java 102   intro to object-oriented programming in javaJava 102   intro to object-oriented programming in java
Java 102 intro to object-oriented programming in java
 
JavaScript in Object-Oriented Way
JavaScript in Object-Oriented WayJavaScript in Object-Oriented Way
JavaScript in Object-Oriented Way
 
Scala in the Wild
Scala in the WildScala in the Wild
Scala in the Wild
 
Object Oriented Programming Tutorial.pptx
Object Oriented Programming Tutorial.pptxObject Oriented Programming Tutorial.pptx
Object Oriented Programming Tutorial.pptx
 
First adoption hackathon at BGJUG
First adoption hackathon at BGJUGFirst adoption hackathon at BGJUG
First adoption hackathon at BGJUG
 
Java 101 Intro to Java Programming - Exercises
Java 101   Intro to Java Programming - ExercisesJava 101   Intro to Java Programming - Exercises
Java 101 Intro to Java Programming - Exercises
 
3 class definition
3 class definition3 class definition
3 class definition
 
java02.ppt
java02.pptjava02.ppt
java02.ppt
 
lecture-a-java-review.ppt
lecture-a-java-review.pptlecture-a-java-review.ppt
lecture-a-java-review.ppt
 
lecture-a-java-review.ppt
lecture-a-java-review.pptlecture-a-java-review.ppt
lecture-a-java-review.ppt
 
lecture-a-java-review.ppt
lecture-a-java-review.pptlecture-a-java-review.ppt
lecture-a-java-review.ppt
 
Java basics with datatypes, object oriented programming
Java basics with datatypes, object oriented programmingJava basics with datatypes, object oriented programming
Java basics with datatypes, object oriented programming
 
lecture-a-java-review.ppt
lecture-a-java-review.pptlecture-a-java-review.ppt
lecture-a-java-review.ppt
 
lecture-a-java-review.ppt
lecture-a-java-review.pptlecture-a-java-review.ppt
lecture-a-java-review.ppt
 
lecture-a-java-review.ppt
lecture-a-java-review.pptlecture-a-java-review.ppt
lecture-a-java-review.ppt
 
How can your applications benefit from Java 9?
How can your applications benefit from Java 9?How can your applications benefit from Java 9?
How can your applications benefit from Java 9?
 

More from Fajar Baskoro

Generasi Terampil Digital Skill-2023.pptx
Generasi Terampil Digital Skill-2023.pptxGenerasi Terampil Digital Skill-2023.pptx
Generasi Terampil Digital Skill-2023.pptxFajar Baskoro
 
Cara Membuat Kursus Online Wordpress-tutorstarter
Cara Membuat Kursus Online Wordpress-tutorstarterCara Membuat Kursus Online Wordpress-tutorstarter
Cara Membuat Kursus Online Wordpress-tutorstarterFajar Baskoro
 
PPT-Kick Off Double Track 2024 melaksanakan Festival Ramadhan
PPT-Kick Off Double Track 2024 melaksanakan Festival RamadhanPPT-Kick Off Double Track 2024 melaksanakan Festival Ramadhan
PPT-Kick Off Double Track 2024 melaksanakan Festival RamadhanFajar Baskoro
 
Buku Inovasi 2023 - 2024 konsep capaian KUS
Buku Inovasi 2023 - 2024 konsep capaian  KUSBuku Inovasi 2023 - 2024 konsep capaian  KUS
Buku Inovasi 2023 - 2024 konsep capaian KUSFajar Baskoro
 
Pemaparan Sosialisasi Program Dual Track 2024.pptx
Pemaparan Sosialisasi Program Dual Track 2024.pptxPemaparan Sosialisasi Program Dual Track 2024.pptx
Pemaparan Sosialisasi Program Dual Track 2024.pptxFajar Baskoro
 
Executive Millennial Entrepreneur Award 2023-1a-1.pdf
Executive Millennial Entrepreneur Award  2023-1a-1.pdfExecutive Millennial Entrepreneur Award  2023-1a-1.pdf
Executive Millennial Entrepreneur Award 2023-1a-1.pdfFajar Baskoro
 
1-Executive Millennial Entrepreneur Award 2023-1-cetak.pptx
1-Executive Millennial Entrepreneur Award  2023-1-cetak.pptx1-Executive Millennial Entrepreneur Award  2023-1-cetak.pptx
1-Executive Millennial Entrepreneur Award 2023-1-cetak.pptxFajar Baskoro
 
Executive Millennial Entrepreneur Award 2023-1.pptx
Executive Millennial Entrepreneur Award  2023-1.pptxExecutive Millennial Entrepreneur Award  2023-1.pptx
Executive Millennial Entrepreneur Award 2023-1.pptxFajar Baskoro
 
Pemrograman Mobile - JetPack Compose1.pptx
Pemrograman Mobile - JetPack Compose1.pptxPemrograman Mobile - JetPack Compose1.pptx
Pemrograman Mobile - JetPack Compose1.pptxFajar Baskoro
 
Evaluasi KPP Program Dual Track Provinsi Kaltim
Evaluasi KPP Program Dual Track Provinsi KaltimEvaluasi KPP Program Dual Track Provinsi Kaltim
Evaluasi KPP Program Dual Track Provinsi KaltimFajar Baskoro
 
foto tenda digital skill program dari sekolah
foto tenda digital skill program dari sekolahfoto tenda digital skill program dari sekolah
foto tenda digital skill program dari sekolahFajar Baskoro
 
Meraih Peluang di Gig Economy yang cocok bagi remaja
Meraih Peluang di Gig Economy yang cocok bagi remajaMeraih Peluang di Gig Economy yang cocok bagi remaja
Meraih Peluang di Gig Economy yang cocok bagi remajaFajar Baskoro
 
Membangun aplikasi mobile dengan Appsheet
Membangun aplikasi mobile dengan AppsheetMembangun aplikasi mobile dengan Appsheet
Membangun aplikasi mobile dengan AppsheetFajar Baskoro
 
Transition education to employment.pdf
Transition education to employment.pdfTransition education to employment.pdf
Transition education to employment.pdfFajar Baskoro
 

More from Fajar Baskoro (20)

Generasi Terampil Digital Skill-2023.pptx
Generasi Terampil Digital Skill-2023.pptxGenerasi Terampil Digital Skill-2023.pptx
Generasi Terampil Digital Skill-2023.pptx
 
Cara Membuat Kursus Online Wordpress-tutorstarter
Cara Membuat Kursus Online Wordpress-tutorstarterCara Membuat Kursus Online Wordpress-tutorstarter
Cara Membuat Kursus Online Wordpress-tutorstarter
 
PPT-Kick Off Double Track 2024 melaksanakan Festival Ramadhan
PPT-Kick Off Double Track 2024 melaksanakan Festival RamadhanPPT-Kick Off Double Track 2024 melaksanakan Festival Ramadhan
PPT-Kick Off Double Track 2024 melaksanakan Festival Ramadhan
 
Buku Inovasi 2023 - 2024 konsep capaian KUS
Buku Inovasi 2023 - 2024 konsep capaian  KUSBuku Inovasi 2023 - 2024 konsep capaian  KUS
Buku Inovasi 2023 - 2024 konsep capaian KUS
 
Pemaparan Sosialisasi Program Dual Track 2024.pptx
Pemaparan Sosialisasi Program Dual Track 2024.pptxPemaparan Sosialisasi Program Dual Track 2024.pptx
Pemaparan Sosialisasi Program Dual Track 2024.pptx
 
Executive Millennial Entrepreneur Award 2023-1a-1.pdf
Executive Millennial Entrepreneur Award  2023-1a-1.pdfExecutive Millennial Entrepreneur Award  2023-1a-1.pdf
Executive Millennial Entrepreneur Award 2023-1a-1.pdf
 
1-Executive Millennial Entrepreneur Award 2023-1-cetak.pptx
1-Executive Millennial Entrepreneur Award  2023-1-cetak.pptx1-Executive Millennial Entrepreneur Award  2023-1-cetak.pptx
1-Executive Millennial Entrepreneur Award 2023-1-cetak.pptx
 
Executive Millennial Entrepreneur Award 2023-1.pptx
Executive Millennial Entrepreneur Award  2023-1.pptxExecutive Millennial Entrepreneur Award  2023-1.pptx
Executive Millennial Entrepreneur Award 2023-1.pptx
 
Pemrograman Mobile - JetPack Compose1.pptx
Pemrograman Mobile - JetPack Compose1.pptxPemrograman Mobile - JetPack Compose1.pptx
Pemrograman Mobile - JetPack Compose1.pptx
 
Evaluasi KPP Program Dual Track Provinsi Kaltim
Evaluasi KPP Program Dual Track Provinsi KaltimEvaluasi KPP Program Dual Track Provinsi Kaltim
Evaluasi KPP Program Dual Track Provinsi Kaltim
 
foto tenda digital skill program dari sekolah
foto tenda digital skill program dari sekolahfoto tenda digital skill program dari sekolah
foto tenda digital skill program dari sekolah
 
Meraih Peluang di Gig Economy yang cocok bagi remaja
Meraih Peluang di Gig Economy yang cocok bagi remajaMeraih Peluang di Gig Economy yang cocok bagi remaja
Meraih Peluang di Gig Economy yang cocok bagi remaja
 
Membangun aplikasi mobile dengan Appsheet
Membangun aplikasi mobile dengan AppsheetMembangun aplikasi mobile dengan Appsheet
Membangun aplikasi mobile dengan Appsheet
 
epl1.pdf
epl1.pdfepl1.pdf
epl1.pdf
 
user.docx
user.docxuser.docx
user.docx
 
Dtmart.pptx
Dtmart.pptxDtmart.pptx
Dtmart.pptx
 
DualTrack-2023.pptx
DualTrack-2023.pptxDualTrack-2023.pptx
DualTrack-2023.pptx
 
BADGE.pptx
BADGE.pptxBADGE.pptx
BADGE.pptx
 
womenatwork.pdf
womenatwork.pdfwomenatwork.pdf
womenatwork.pdf
 
Transition education to employment.pdf
Transition education to employment.pdfTransition education to employment.pdf
Transition education to employment.pdf
 

Recently uploaded

KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationkaushalgiri8080
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - InfographicHr365.us smith
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataBradBedford3
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...Christina Lin
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyFrank van der Linden
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number SystemsJheuzeDellosa
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 
What are the features of Vehicle Tracking System?
What are the features of Vehicle Tracking System?What are the features of Vehicle Tracking System?
What are the features of Vehicle Tracking System?Watsoo Telematics
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureDinusha Kumarasiri
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样umasea
 
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝soniya singh
 

Recently uploaded (20)

KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanation
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - Infographic
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The Ugly
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number Systems
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 
What are the features of Vehicle Tracking System?
What are the features of Vehicle Tracking System?What are the features of Vehicle Tracking System?
What are the features of Vehicle Tracking System?
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with Azure
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
 
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
 

Groupingobject

  • 1. Objects First With Java A Practical Introduction Using BlueJ Grouping objects Collections and iterators 2.0
  • 2. 2Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main concepts to be covered • Collections • Loops • Iterators • Arrays
  • 3. 3Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling The requirement to group objects • Many applications involve collections of objects: – Personal organizers. – Library catalogs. – Student-record system. • The number of items to be stored varies. – Items added. – Items deleted.
  • 4. 4Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling A personal notebook • Notes may be stored. • Individual notes can be viewed. • There is no limit to the number of notes. • It will tell how many notes are stored. • Explore the notebook1 project.
  • 5. 5Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Class libraries • Collections of useful classes. • We don’t have to write everything from scratch. • Java calls its libraries, packages. • Grouping objects is a recurring requirement. – The java.util package contains classes for doing this.
  • 6. 6Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling import java.util.ArrayList; /** * ... */ public class Notebook { // Storage for an arbitrary number of notes. private ArrayList notes; /** * Perform any initialization required for the * notebook. */ public Notebook() { notes = new ArrayList(); } ... }
  • 7. 7Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Object structures with collections
  • 8. 8Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Adding a third note
  • 9. 9Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Features of the collection • It increases its capacity as necessary. • It keeps a private count (size() accessor). • It keeps the objects in order. • Details of how all this is done are hidden. – Does that matter? Does not knowing how prevent us from using it?
  • 10. 10Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Using the collection public class Notebook { private ArrayList notes; ... public void storeNote(String note) { notes.add(note); } public int numberOfNotes() { return notes.size(); } ... } Adding a new note Returning the number of notes (delegation).
  • 11. 11Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Index numbering
  • 12. 12Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Retrieving an object Index validity checks Retrieve and print the note public void showNote(int noteNumber) { if(noteNumber < 0) { // This is not a valid note number. } else if(noteNumber < numberOfNotes()) { System.out.println(notes.get(noteNumber)); } else { // This is not a valid note number. } }
  • 13. 13Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Removal may affect numbering
  • 14. 14Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Review • Collections allow an arbitrary number of objects to be stored. • Class libraries usually contain tried- and-tested collection classes. • Java’s class libraries are called packages. • We have used the ArrayList class from the java.util package.
  • 15. 15Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Review • Items may be added and removed. • Each item has an index. • Index values may change if items are removed (or further items added). • The main ArrayList methods are add, get, remove and size.
  • 16. 16Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Iteration • We often want to perform some actions an arbitrary number of times. – E.g., print all the notes in the notebook. How many are there? • Most programming languages include loop statements to make this possible. • Java has three sorts of loop statement. – We will focus on its while loop.
  • 17. 17Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling While loop pseudo code while(loop condition) { loop body } while(there is at least one more note to be printed) { show the next note } Boolean test while keyword Statements to be repeated Pseudo-code example to print every note General form of a while loop
  • 18. 18Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling A Java example /** * List all notes in the notebook. */ public void listNotes() { int index = 0; while(index < notes.size()) { System.out.println(notes.get(index)); index++; } } Increment by one
  • 19. 19Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Iterating over a collection Iterator it = myCollection.iterator(); while(it.hasNext()) { call it.next() to get the next object do something with that object } java.util.Iterator Returns an Iterator object public void listNotes() { Iterator it = notes.iterator(); while(it.hasNext()) { System.out.println(it.next()); } }
  • 20. 20Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling The auction project • The auction project provides further illustration of collections and iteration. • Two further points to follow up: – The null value. – Casting. Used to store the result of get into a variable: • String message = (String) notes.get(0);
  • 21. 21Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Review • Loop statements allow a block of statements to be repeated. • A Java while loop allows the repetition to be controlled by a boolean expression. • Collection classes have special Iterator objects that simplify iteration over the whole collection.
  • 22. 22Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Fixed-size collections • Sometimes the maximum collection size can be pre-determined. • Programming languages usually offer a special fixed-size collection type: an array. • Java arrays can store objects or primitive-type values. • Arrays use a special syntax.
  • 23. 23Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling The weblog-analyzer project • Web server records details of each access. • Supports webmaster’s tasks. – Most popular pages. – Busiest periods. – How much data is being delivered. – Broken references. • Analyze accesses by hour.
  • 24. 24Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Creating an array object public class LogAnalyzer { private int[] hourCounts; private LogfileReader reader; public LogAnalyzer() { hourCounts = new int[24]; reader = new LogfileReader(); } ... } Array object creation Array variable declaration
  • 25. 25Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling The hourCounts array
  • 26. 26Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Using an array • Square-bracket notation is used to access an array element: hourCounts[...] • Elements are used like ordinary variables. – On the left of an assignment: • hourCounts[hour] = ...; – In an expression: • adjusted = hourCounts[hour] – 3; • hourCounts[hour]++;
  • 27. 27Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling The for loop • Similar to a while loop. • Often used to iterate a fixed number of times. • Often used to iterate over an array.
  • 28. 28Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling For loop pseudo-code for(initialization; condition; post-body action) { statements to be repeated } General form of a for loop Equivalent in while-loop form initialization; while(condition) { statements to be repeated post-body action }
  • 29. 29Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling A Java example for(int hour = 0; hour < hourCounts.length; hour++) { System.out.println(hour + ": " + hourCounts[hour]); } int hour = 0; while(hour < hourCounts.length) { System.out.println(hour + ": " + hourCounts[hour]); hour++; } for loop version while loop version
  • 30. 30Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Review • Arrays are appropriate where a fixed- size collection is required. • Arrays use special syntax. • For loops offer an alternative to while loops when the number of repetitions is known. • For loops are often used to iterate over arrays.