|
|
|
|
Managing collections of objects. |
|
The fundamental container object is called a list. |
|
Fundamental properties of lists. |
|
Specifications for a typical List class. |
|
How to perform an iterative process with a list. |
|
The “while” statement and “for loop.” |
|
|
|
|
|
A list is a container that holds a finite
sequence of values all of the same type. |
|
List properties: |
|
A list is finite, with zero or more elements. |
|
A list is a sequence. |
|
A list is homogeneous. |
|
container: an object whose purpose is to contain
other objects. |
|
list: a container in which the elements are kept
in sequence. |
|
|
|
|
public class StudentList |
|
A finite list of Students. |
|
public StudentList () |
|
Create an empty StudentList. |
|
public int size () |
|
Number of elements in List. |
|
ensure: |
|
this.size() >= 0 |
|
public boolean isEmpty () |
|
List contains no elements. |
|
this.isEmpty()==(this.size()==0) |
|
|
|
|
|
|
|
(a) StudentList sl; //initially
//null |
|
(b) sl = new StudentList(); |
|
//Empty List |
|
|
|
|
public Student get(int i) |
|
The element at the specified position. |
|
require:0<=i && i<this.size() |
|
public void append (Student s) |
|
Append the specified Student to the end of
this List. |
|
require: |
|
s!=null |
|
ensure: |
|
this.size()==old.size+1 |
|
this.get(this.size()-1)
== s |
|
Anything not mentioned does not change! |
|
|
|
|
public void remove(int i) |
|
Remove the element at the specified
position. |
|
require: |
|
0<=i && i<this.size() |
|
ensure: |
|
this.size()==old.size-1 |
|
for
i <= j < this.size() |
|
this.get(j)
== old.get(j+1) |
|
public void set (int i, Student s) |
|
Replace the element at the specified
position with the specified Student. |
|
require: |
|
0
<= i && i < this.size() |
|
s
!= null |
|
ensure: |
|
this.get(i)
== s |
|
|
|
|
Our specifications are fairly ubiquitous. Just substitute the class name Student
with another class. Change any
reference to an Student object with a reference to an object of another class. |
|
public NewObject get (int i) |
|
… |
|
public void append (NewObject n) |
|
… |
|
public void set (int i, NewObject n) |
|
… |
|
|
|
|
|
|
|
iteration: a process in which an operation is
performed several times. |
|
Syntax: |
|
while ( condition ) |
|
statement |
|
|
|
|
The component statement is called the body. |
|
Executing the body should have the potential of
changing the value of the condition. |
|
It is possible that the condition of a “while”
statement will remain true no matter now many time the body is
executed. This is an “infinite
loop.” |
|
|
|
|
while( more list elements to process ) |
|
process the next element |
|
|
|
e.g. |
|
int index; |
|
index = 0; |
|
while (index < list.size()) { |
|
process list.get(index); |
|
index = index + 1; |
|
} |
|
We must guarantee that the while condition
eventually will become false. |
|
|
|
|
public double finalAverage(StudentList students) |
|
The average (mean) of the final exam grades
of the specified Students. |
|
require: |
|
students.size() > 0 |
|
public int finalExam () |
|
This Student’s grade on the final exam. |
|
|
|
public double finalAverage(StudentList students) |
|
{ |
|
int i, sum, count; |
|
count = students.size(); |
|
sum = 0; |
|
i = 0; |
|
while ( i < count) { |
|
sum = sum + students.get(i).finalExam(); |
|
i = i+1; |
|
} |
|
return (double)sum / (double)count; |
|
} |
|
|
|
|
|
|
Consider what to do if some students did not
take the final exam. |
|
|
|
|
|
/** |
|
* The
lowest final exam grades of the |
|
*
specified Students. |
|
*
require: |
|
* students.size() > 0 |
|
*/ |
|
public int minFinalExam (StudentList students){ |
|
int i; |
|
int
low; |
|
low =
students.get(0).finalExam(); |
|
i=1; |
|
while (
i < students.size()){ |
|
if
(students.get(i).finalExam()<low) |
|
low=students.get(i).finalExam(); |
|
i =
i+1; |
|
} |
|
} |
|
|
|
|
public boolean contains (StudentList s) |
|
{ |
|
int i; |
|
int
length; |
|
length
= this.size(); |
|
i=0; |
|
while (
i<length && get(i)!=s ) |
|
i =
i+1; |
|
return
i < length; |
|
} |
|
|
|
|
Two reference values are equal if they refer to
the same object. |
|
|
|
|
Consider a Date class, that has components day,
month, and year. |
|
If distinct Date objects are created to
represent the same date, would these objects be “equal” ? Must they refer
to the same object? |
|
|
|
|
To determine if they are “equal” dates, we must
compare their day, month, and year. |
|
public boolean equals (Object obj){ |
|
Require.condition(obj |
|
instanceof Date); |
|
Date d
= (Date)obj; |
|
return this.year()==d.year() && |
|
this.month()==d.month() && |
|
this.day()==d.day(); |
|
} |
|
// instanceof returns true if the object is |
|
// an instance of |
|
// the class |
|
|
|
|
|
public int indexOf (Student obj) { |
|
int i; |
|
int length; |
|
length = this.size(); |
|
i = 0; |
|
while (i < length &&
!obj.equals(get(i))) |
|
i = i+1; |
|
if ( i < length) |
|
return i; |
|
else |
|
return -1;// item not found |
|
} |
|
|
|
|
public void removeDuplicates(StudentList l) |
|
{ |
|
int i; //index |
|
Student item; //Check for duplicates |
|
int j; //index |
|
//invariant: i < j |
|
i = 0; |
|
while (i < list.size()) { |
|
item = list.get(i); |
|
j =
i + 1; |
|
while (j < list.size()) |
|
if (item.equals(list.get(j))) |
|
list.remove(j); |
|
else |
|
j = j+1; |
|
i = i+1; |
|
} |
|
} |
|
|
|
|
initialization |
|
while (condition) { |
|
body |
|
} |
|
conclusion |
|
|
|
|
for ( initialization; condition; updateStatement
) |
|
statement |
|
|
|
|
int i; |
|
|
|
for ( i = 0; i < list.size(); i = i+1) |
|
process list.get(i); |
|
|
|
|
|
How to use a List. |
|
summing elements. |
|
finding an element. |
|
removing elements. |
|
while loops. |
|
Equality. |
|
The instanceof
operator. |
|
for statements. |
|
|
|