+ All Categories
Home > Documents > Chapter04 - courses.cs.vt.educourses.cs.vt.edu/~cs1054/spring2005/notes/4/Chapter04.2up.pdf · 5...

Chapter04 - courses.cs.vt.educourses.cs.vt.edu/~cs1054/spring2005/notes/4/Chapter04.2up.pdf · 5...

Date post: 27-Sep-2020
Category:
Upload: others
View: 6 times
Download: 0 times
Share this document with a friend
15
1 ! Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 2 " # $
Transcript
Page 1: Chapter04 - courses.cs.vt.educourses.cs.vt.edu/~cs1054/spring2005/notes/4/Chapter04.2up.pdf · 5 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes,

1

�������������� �����

������������������������ ��� �����

� ��������������

�����������������������

� !

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

" �������������������������

• ����������

• #����

• ���������

• ����$�

Page 2: Chapter04 - courses.cs.vt.educourses.cs.vt.edu/~cs1054/spring2005/notes/4/Chapter04.2up.pdf · 5 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes,

2

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

% ����&���' �������������

�������• " ��$�������������������������������(�

�������)

* ��������������+���

* #����$���������

* ,������-��������$���'

• % ����' �����(���' ��������������������

* ���' �������

* ���' ���������

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

������������������.

• /�����' �$����������

• ������������������������0 ��

• % ����������' ������ ����' �����(�

�����

• ���0 �������� �0 �' ��$�����������

������

• 12������� ����������� �������

Page 3: Chapter04 - courses.cs.vt.educourses.cs.vt.edu/~cs1054/spring2005/notes/4/Chapter04.2up.pdf · 5 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes,

3

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

�������������

• ������������(����(����������

• ������3�� �������0 ��������$� ���(��' �������

• ���������������������4������

• � ������������������������������&���' ��� * % ��java.util ���.������������

��������(��������� �

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 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();}

...}

Page 4: Chapter04 - courses.cs.vt.educourses.cs.vt.edu/~cs1054/spring2005/notes/4/Chapter04.2up.pdf · 5 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes,

4

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

������������������0 � �

����������

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

��������� �������

Page 5: Chapter04 - courses.cs.vt.educourses.cs.vt.edu/~cs1054/spring2005/notes/4/Chapter04.2up.pdf · 5 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes,

5

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

���������(�� �����������

• ���������������������$������������$

• ���.��������������������5size()��������6

• ���.������ �����������������

• 7�������(� �0 ������ ������������� ����

* 7����� ���' �����8�7��������.��0 ��� �0 ������������(��' �������8

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 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).

Page 6: Chapter04 - courses.cs.vt.educourses.cs.vt.edu/~cs1054/spring2005/notes/4/Chapter04.2up.pdf · 5 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes,

6

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

����2���' �����

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 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.}

}

Page 7: Chapter04 - courses.cs.vt.educourses.cs.vt.edu/~cs1054/spring2005/notes/4/Chapter04.2up.pdf · 5 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes,

7

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

9�' �����' �$��((����

��' �����

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

9���0

• ���������������0 �����������$���' �����(���������������������

• ��������������������$������������-���-������������������������

• ����3��������������������������������

• ��� ���������� ��ArrayList ������(��' �� ��java.util ���.���

Page 8: Chapter04 - courses.cs.vt.educourses.cs.vt.edu/~cs1054/spring2005/notes/4/Chapter04.2up.pdf · 5 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes,

8

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

9���0

• ���' ��' �$����������������' ����

• 1�� ���' � ���������2

• ����2��������' �$�� �����(���' ������

��' �����5���(��� �����' �������6

• % ��' ���ArrayList ' �� ��������

add4�get4�remove ����size

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

��������

• ����(����0 ����������(��' ���' ������������

�������$���' �����(��' ��

* 1 � 4����������� ����������� ���������. �: �0 �

' ��$������ ���8

• " ����������' ' �������������������� ����

����� ���� ���' �.��� ���������

• ����� ���� �����������(�����������' ���

* ���0 ���(������������ �� �� ���

Page 9: Chapter04 - courses.cs.vt.educourses.cs.vt.edu/~cs1054/spring2005/notes/4/Chapter04.2up.pdf · 5 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes,

9

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

� �������������������

while(loop condition) {loop body

}

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

}

Boolean testwhile keyword

Statements to be repeated

Pseudo-code example to print every note

General form of a while loop

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 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

Page 10: Chapter04 - courses.cs.vt.educourses.cs.vt.edu/~cs1054/spring2005/notes/4/Chapter04.2up.pdf · 5 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes,

10

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

�������������������������

Iterator it = myCollection.iterator();while(it.hasNext()) {

call it.next() to get the next objectdo something with that object

}

java.util.IteratorReturns an Iterator

object

public void listNotes(){

Iterator it = notes.iterator();while(it.hasNext()) {

System.out.println(it.next());}

}

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

% ������� �������

• % ������� ����������������(��� ���

������������(����������������

�������

• %0 ��(��� ������������(����0 ���)

* % ��null �����

* ������ �� �������������� ����������(�get�������������)• String message = (String) notes.get(0);

Page 11: Chapter04 - courses.cs.vt.educourses.cs.vt.edu/~cs1054/spring2005/notes/4/Chapter04.2up.pdf · 5 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes,

11

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

9���0

• #���������' ���������0 �������.��(������' �������������������

• �������0 ������������0 ��� �����������������������������$������������2�������

• ������������������ �����������Iterator ��������� ����' ��($��������������� ��0 �������������

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

2��-�+������������

• ,�' ��' ���� ��' �2' �' ������������+������������-�����' ���

• ������' ' �������������������$��((������������(2��-�+�������������$��)�������

• ���������$�������������������������' ���-�$���������

• ����$����������������$���2

Page 12: Chapter04 - courses.cs.vt.educourses.cs.vt.edu/~cs1054/spring2005/notes/4/Chapter04.2up.pdf · 5 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes,

12

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

% ��� �� ���� ���� �������

• ���������������������������(���� �������

• ,��������0 ��' �����3�����.�

* " �����������������

* �������������

* : �0 �' �� ���������������������

* ���.�����(�������

• ����$+������������$� ���

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 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

Page 13: Chapter04 - courses.cs.vt.educourses.cs.vt.edu/~cs1054/spring2005/notes/4/Chapter04.2up.pdf · 5 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes,

13

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

% ��hourCounts ����$

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

� �����������$

• ,&����-����.����������������������������

�������$����' ���)�hourCounts[...]

• 1��' ���������������.��������$���������

* ���� ����(���(���������' ���)• hourCounts[hour] = ...;

* �������2�������)• adjusted = hourCounts[hour] – 3;• hourCounts[hour]++;

Page 14: Chapter04 - courses.cs.vt.educourses.cs.vt.edu/~cs1054/spring2005/notes/4/Chapter04.2up.pdf · 5 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes,

14

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

% ��(�������

• ,' ���������0 �������

• �(���������������������(2�����' ����

�(��' ��

• �(�������������������������������$

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 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 repeatedpost-body action

}

Page 15: Chapter04 - courses.cs.vt.educourses.cs.vt.edu/~cs1054/spring2005/notes/4/Chapter04.2up.pdf · 5 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes,

15

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 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

while loop version

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

9���0

• ����$�����������������0 ������(2��-�+����������������&����

• ����$��������������$���2

• ����������((��������������������0 ���������0 ���� ����' �����(�������������.��0 �

• ��������������(����������������������������$�


Recommended