This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
! So far we’ve done projects with a very limited number of objects! What if an unknown number of data records?! What if you had an account management system that had to deal with 1000
transactions?
Couldn’t rely on fixed code segmentto process specific number of moves.
It would be silly to copy and paste code toprocess a transaction 1000 times.
! Definitions:Before considering specifics we define some general terms that apply to any iteration construct:
pass (or iteration) - one complete execution of the bodyloop entry - the point where flow of control passes to the bodyloop test - the point at which the decision is made to (re)enter
the body, or notloop exit - the point at which the iteration ends and control
passes to the next statement after the looptermination condition - the condition that causes the iteration to stop
When designing a loop, it is important to clearly identify each of these.
Understanding the termination condition, and what guarantees it will eventually occur, are particularly vital.
read first itemwhile ( input is OK or item valid ) {
process itemread next item
}
Some observations:
- It is possible that the body of the loop will never be executed.
- It is possible that the loop test condition will become false in the middle of an iteration. Even so, the remainder of the loop body will be executed before the loop test is performed and iteration stops.
- The loop test must involve at least one variable whose value is (potentially) changed by at least one statement within the loop body.
! Just like a mathematical vector stores an ordered collection of numeric values, a Java Vector object stores an ordered collection of objects.String s;
Vector v = new Vector();
s = br.readLine();
while (s != null) {
v.addElement(s);
s.br.readLine();
}
System.out.print(V.size());
This allows us to Read and store an indeterminate number of strings (or any type of object), but how do we then read or process them?
The Vector addElement() method is used for storing elements in the vector.The Vector size() method returns the number of elements in the vector.The Vector isEmpty() method returns false if any elements are in the vector.Many other vector methods exist in the Vector class for use.
! The Vector class has a method, elements() , that returns an Enumeration object.
! The Enumeration class has the methods:boolean hasMoreElements();
someType nextElement();
The enumeration class models the behavior of traversing the elements of the collection, in this case the vector. Other Java container/collection classes also use the enumeration class to provide traversal services for visiting and processing each element of the collection.
The typecast is required to assign the reference returned by nextElement() to a specific reference variable since by default nextElement() returns a generic object type reference.
! Using the Vector and Enumeration classes, we could modify our SongLibraryclass so that we only have to read the database file once! Then we can query our vector object as many times as we want!
! See text, pages 284-285 for example.
! Exercise: plan out a means by which you could read in & query the song database, make additions, and then re-save to an updated file!
! Problem: Create a container class to implement the mathematical concept of a Set.
! We need the following behavior (methods):– Set (constructor)– contains (test for membership in the set)– isEmpty (see if this is the “null set”)– addElement (as name implies)– copy (to make a copy of the set)– size (number of elements in the set)– elements (returns an enumeration)– union (returns a set containing only elements in both)– intersection (returns a set containing all elements of each)– print (as name implies)
Since the Set class will also be a container class consistency should be maintained with the other container classes, (e.g., the Vector class). Thus the method names: addElement(), size(), & elements() were selected.
! … so we’ll only need the followinginstance variable in the Set class:
Vector v;
– Note: the book uses this example, and makes several errors, because sometimes it calls this instance variable “theElements” and sometimes it calls it “vector”. We’ll just call it “v”.
The behavior of the: addElement(), method is one aspect of the the Set class that is different from the vector class. The other behaviors that distinguish Sets from vectors is the union and intersection methods. .
Note: the object class represents all object types. The object class contains a general equals method for comparing any two objects. In reality the general equals method does not compare any two objects. It relies upon the specific classes providing their own equals method which is automagically used. The Java predefined classes, (String, Integer), provides their own equalsmethods. To store user defined objects in the Set class we must provide an appropriate and specific equals method in our classes.