Page 423 - Introduction to Programming with Java: A Problem Solving Approach
P. 423

                Figure 10.10 Class with sequential search method (findStudent)
array. For a more general purpose driver, you might want to replace the initializer with a loop that repeat- edly prompts the user to enter a student id or q to quit. If you choose that option, then you’d need to store the number of filled elements in a filledElements variable and pass the filledElements variable as the third argument in the Course constructor call. This is what the constructor call would look like:
Course course = new Course("CS101", ids, filledElements);
10.7 SearchinganArray 389
 /*********************************************************************
*
Course.java
Dean & Dean
This class represents a particular course in a school.
**********************************************************************/
*
*
*
public class Course
{
}
// end class Course
private String courseName; // name of the course
private int[] ids;
// ids for students in the course
private int filledElements; // number of filled-in elements
//*******************************************************************
public Course(String courseName, int[] ids, int filledElements)
{
this.courseName = courseName;
this.ids = ids;
this.filledElements = filledElements;
} // end constructor
//*******************************************************************
       Apago PDF Enhancer
// This method returns index of found id or -1 if not found.
public int findStudent(int id)
{
}
// end findStudent
for (int i=0; i<filledElements; i++)
{
if (ids[i] == id)
{
}
return i;
} // end for
return -1;
























































   421   422   423   424   425