Chapter04 - courses.cs.vt.educourses.cs.vt.edu/~cs1054/spring2005/notes/4/Chapter04.2up.pdf · 5...
Transcript of Chapter04 - courses.cs.vt.educourses.cs.vt.edu/~cs1054/spring2005/notes/4/Chapter04.2up.pdf · 5...
1
�������������� �����
������������������������ ��� �����
� ��������������
�����������������������
� !
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 2
" �������������������������
• ����������
• #����
• ���������
• ����$�
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������� ����������� �������
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();}
...}
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
��������� �������
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).
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.}
}
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 ���.���
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 ���(������������ �� �� ���
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
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);
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
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
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]++;
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
}
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 �
• ��������������(����������������������������$�