0% found this document useful (0 votes)
2 views15 pages

Chapter04

The document covers object-oriented programming principles focusing on collections, loops, and iterators in Java. It discusses the importance of grouping objects, the use of class libraries, and how to manage collections with the ArrayList class. Additionally, it explains iteration techniques using while and for loops, and the application of arrays for fixed-size collections.

Uploaded by

alyaqein
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views15 pages

Chapter04

The document covers object-oriented programming principles focusing on collections, loops, and iterators in Java. It discusses the importance of grouping objects, the use of class libraries, and how to manage collections with the ArrayList class. Additionally, it explains iteration techniques using while and for loops, and the application of arrays for fixed-size collections.

Uploaded by

alyaqein
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

SW08 - Object-Oriented Programming and Principles

Grouping objects

Collections and iterators

2.0

Main concepts to be covered

• Collections
• Loops
• Iterators
• Arrays

© M. Kölling, University of Southern Denmark 1


SW08 - Object-Oriented Programming and Principles

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.

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.

© M. Kölling, University of Southern Denmark 2


SW08 - Object-Oriented Programming and Principles

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 [Link] package contains
classes for doing this.

import [Link];

/**
* ...
*/
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();
}

...
}

© M. Kölling, University of Southern Denmark 3


SW08 - Object-Oriented Programming and Principles

Object structures with


collections

Adding a third note

© M. Kölling, University of Southern Denmark 4


SW08 - Object-Oriented Programming and Principles

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?

Using the collection


public class Notebook
{
private ArrayList notes;
...

public void storeNote(String note)


{
[Link](note); Adding a new note
}

public int numberOfNotes()


{
return [Link](); Returning the number of notes
} (delegation).

...
}
10

© M. Kölling, University of Southern Denmark 5


SW08 - Object-Oriented Programming and Principles

Index numbering

11

Retrieving an object
Index validity checks
public void showNote(int noteNumber)
{
if(noteNumber < 0) {
// This is not a valid note number.
}
else if(noteNumber < numberOfNotes()) {
[Link]([Link](noteNumber));
}
else {
// This is not a valid note number.
}
}
Retrieve and print the note

12

© M. Kölling, University of Southern Denmark 6


SW08 - Object-Oriented Programming and Principles

Removal may affect


numbering

13

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 [Link] package.

14

© M. Kölling, University of Southern Denmark 7


SW08 - Object-Oriented Programming and Principles

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.

15

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.

16

© M. Kölling, University of Southern Denmark 8


SW08 - Object-Oriented Programming and Principles

While loop pseudo code


General form of a while loop

while keyword
Boolean test
while(loop condition) {
loop body Statements to be repeated
}

Pseudo-code example to print every note

while(there is at least one more note to be printed) {


show the next note
}

17

A Java example
/**
* List all notes in the notebook.
*/
public void listNotes()
{
int index = 0;
while(index < [Link]()) {
[Link]([Link](index));
index++;
}
}
Increment by one

18

© M. Kölling, University of Southern Denmark 9


SW08 - Object-Oriented Programming and Principles

Iterating over a collection


Returns an Iterator
[Link] object

Iterator it = [Link]();
while([Link]()) {
call [Link]() to get the next object
do something with that object
}

public void listNotes()


{
Iterator it = [Link]();
while([Link]()) {
[Link]([Link]());
}
}
19

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) [Link](0);

20

© M. Kölling, University of Southern Denmark 10


SW08 - Object-Oriented Programming and Principles

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.

21

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.

22

© M. Kölling, University of Southern Denmark 11


SW08 - Object-Oriented Programming and Principles

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.

23

Creating an array object

public class LogAnalyzer


{
private int[] hourCounts; Array variable declaration
private LogfileReader reader;

public LogAnalyzer()
{
hourCounts = new int[24]; Array object creation
reader = new LogfileReader();
}
...
}

24

© M. Kölling, University of Southern Denmark 12


SW08 - Object-Oriented Programming and Principles

The hourCounts array

25

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]++;

26

© M. Kölling, University of Southern Denmark 13


SW08 - Object-Oriented Programming and Principles

The for loop


• Similar to a while loop.
• Often used to iterate a fixed number
of times.
• Often used to iterate over an array.

27

For loop pseudo-code


General form of a for loop

for(initialization; condition; post-body action) {


statements to be repeated
}

Equivalent in while-loop form

initialization;
while(condition) {
statements to be repeated
post-body action
}

28

© M. Kölling, University of Southern Denmark 14


SW08 - Object-Oriented Programming and Principles

A Java example
for loop version

for(int hour = 0; hour < [Link]; hour++) {


[Link](hour + ": " + hourCounts[hour]);
}

while loop version

int hour = 0;
while(hour < [Link]) {
[Link](hour + ": " + hourCounts[hour]);
hour++;
}

29

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.

30

© M. Kölling, University of Southern Denmark 15

You might also like