• No results found

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling 2

N/A
N/A
Protected

Academic year: 2021

Share "Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling 2"

Copied!
30
0
0

Loading.... (view fulltext now)

Full text

(1)
(2)

"

#

(3)

%

&

'

"

$

(

)

*

+

* #

$

* ,

-

$

'

%

'

(

'

*

'

*

'

(4)

.

/

' $

0

%

'

'

(

0

0 '

$

12

(5)

(

(

3

0

$

( '

4

&

'

* %

java.util

.

(

(6)

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)
(8)
(9)

(

$

$

.

5

size()

6

.

7

(

0

* 7

'

8 7

.

0

0

( '

8

(10)

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

9

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)

9 '

' $ ((

'

(14)

9

0

0

$

'

(

$

-•

3

ArrayList

( '

java.util

.

(15)

9

0

'

' $

'

1

'

2

2

' $

(

'

'

5

(

'

6

%

'

ArrayList

'

(16)

(

0

( '

'

$

'

( '

* 1 4

. : 0

'

$

8

"

' '

' .

(

'

*

0

(

(17)

while(

loop condition

) {

loop body

}

while(

there is at least one more note to be printed

) {

Boolean test

while

keyword

Statements to be repeated

Pseudo-code example to print every note

General form of a while loop

(18)

2 '

/**

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

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

object

Iterator

public void listNotes()

{

Iterator it = notes.iterator();

while(it.hasNext()) {

(20)

%

%

(

(

%0

(

(

0

)

* %

null

*

(

get

)

(21)

9

0

#

'

0

. (

'

0

0

$

2

Iterator

'

($

0

(22)

2

- +

, '

'

' 2 ' '

+

-

'

' '

$ ((

( 2

- +

$

)

$

'

- $

$

$

2

(23)

%

(

,

0

'

3

.

* "

*

* : 0 '

*

.

(

$+

$

(24)

$

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

$

,&

-

.

$

'

)

hourCounts[...]

1 '

.

$

*

(

(

'

)

hourCounts[hour] = ...;

*

2

)

adjusted = hourCounts[hour] – 3;

hourCounts[hour]++;

(27)

%

(

, '

0

(

( 2

'

( '

(28)

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

2 '

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

(30)

9

0

$

0

( 2

-+

&

$

$

2

((

0

0

'

(

.

0

(

$

References

Related documents

Less complicated to continue statement in while loop java tutorial, working with performance and switch statement block inside the program is possible execution flow control.. half

Since Set interface or HashSet class doesn't provide will get method to retrieve elements the gift way your take out elements from schedule Set group to iterate over population by

The java iterator, implement serializable interface adds them need of implementing efficient traversal of linked list collection to iterate through elements using.. This

This document states the business purpose of down payment in Logistics Invoice Verification and an example case of setting up and running one cycle.. What is

N users want to choose among N identical file shares.. The goal is to balance users across

The forget items received no retrieval practice opportunities in any of the retrieval practice schedules, but might be expected to undergo greater forgetting in the maximize

In CRE8 ® Europe students from Norway, Romania, Spain and Sweden come together to help organizations solve authentic problems, while enhancing students.. competitiveness

Structural modifications in throwable exception why concurrent occurs java map while iterating, so may be empty book in iterator to code was not an element from the