The SDK and java.util.ArrayList


Your CourseSession class tracks the count of students just fine. However, you and I both know that it's just maintaining a counter and not holding onto the students that are being enrolled. For the testEnrollStudents method to be complete, it will need to prove that the CourseSession object is retaining the actual student objects.

One possible solution is to ask the CourseSession for a list of all the enrolled students, then check the list to ensure that it contains the expected students. Modify your test to include the lines shown below in bold.

 public void testEnrollStudents() {    CourseSession session = new CourseSession("ENGL", "101");    Student student1 = new Student("Cain DiVoe");    session.enroll(student1);    assertEquals(1, session.getNumberOfStudents());    java.util.ArrayList<Student> allStudents = session.getAllStudents();    assertEquals(1, allStudents.size());    assertEquals(student1, allStudents.get(0));    Student student2 = new Student("Coralee DeVaughn");    session.enroll(student2);    assertEquals(2, session.getNumberOfStudents());    assertEquals(2, allStudents.size());    assertEquals(student1, allStudents.get(0));    assertEquals(student2, allStudents.get(1)); } 

The first line of newly added test code:

 java.util.ArrayList<Student> allStudents = session.getAllStudents(); 

says that there will be a method on CourseSession named getAllStudents. The method will have to return an object of the type java.util.ArrayList<Student>, since that's the type of the variable to which the result of getAllStudents is to be assigned. A type appearing in this forma class name followed by a parameter type within angle brackets (< and >) is known as a parameterized type. In the example, the parameter Student of java.util.ArrayList indicates that the java.util.ArrayList is bound such that it can only contain Student objects.

The class java.util.ArrayList is one of thousands available as part of the Java SDK class library. You should have downloaded and installed the SDK documentation so that it is available on your machine. You can also browse the documentation online at Sun's Java site. My preference is to have the documentation available locally for speed and accessibility reasons.

Pull up the documentation and navigate to the Java 2 Platform API Specification. You should see something like Figure 2.2.

Figure 2.2. Java 2 Platform API Specification


The Java API documentation will be your best friend, unless you're doing pair programming,[2] in which case it will be your second-best friend. It is divided into three frames. The upper left frame lists all the packages that are available in the library. A package is a group of related classes.

[2] A technique whereby members of a development team work in dynamic pairs of developers who jointly construct code.

The lower left frame defaults to showing all the classes in the library. Once a package is selected, the lower left frame shows only the classes contained within that package. The frame to the right that represents the remainder of the page shows detailed information on the currently selected package or class.

Scroll the package frame until you see the package named java.util and select it. The lower left pane should now show a list of interfaces, classes, and exceptions. I will explain interfaces and exceptions later. For now, scroll down until you see the class named ArrayList and select it.

The package java.util contains several utility classes that you will use often in the course of Java development. The bulk of the classes in the package support something called the Collections Framework. The Collections Framework is a consistent sublibrary of code used to support standard data structures such as lists, linked lists, sets, and hash tables. You will use collections over and over again in Java to work with groupings of related objects.

The main pane (to the right) shows all of the detailed information on the class java.util.ArrayList. Scroll down until you see the Method Summary. The Method Summary shows the methods implemented in the java.util.ArrayList class. Take a few minutes to read through the methods available. Each of the method names can be clicked on for detailed information regarding the method.

You will use three of the java.util.ArrayList methods as part of the current exercise: add, get, and size. One of them, size, is already referenced by the test. The following line of code asserts that there is one object in the java.util.ArrayList object.

 assertEquals(1, allStudents.size()); 

The next line of new code asserts that the first element in allStudents is equal to the student that was enrolled.

 assertEquals(student, allStudents.get(0)); 

According to the API documentation, the get method returns the element at an arbitrary position in the list. This position is passed to the get method as an index. Indexes are zero-based, so get(0) returns the first element in the list.



Agile Java. Crafting Code with Test-Driven Development
Agile Javaв„ў: Crafting Code with Test-Driven Development
ISBN: 0131482394
EAN: 2147483647
Year: 2003
Pages: 391
Authors: Jeff Langr

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net