A typical application of arrays with strings is the search of a string value in an array of strings. The most critical statements are the string comparisons. The general array techniques for searching were discussed in Chapter 9.
This string comparison with the equals operator only tests if two strings are equal; it is useful in carrying out linear searches. For a binary search of strings, these need to be in alphabetical order. For this search, the comparison needed is more complete in the sense that if the strings are not equal, the order of the first string with respect to the second string needs to be known.
The following problem sets up an array of objects of class Person. Then, it carries out a linear search of the name of the objects, looking for a specific string value (the target string). The KJP implementation consists of two classes, Person and Marrayperson. Class Person was described in Chapter 5 (in Section 5.7).
The array of objects is declared in function main of class Marrayperson. The KJP statement for this declaration is:
object parray array [NUM_PERSONS] of class Person
The objects of class Person are created in a for loop—not all objects of the array are created. The KJP statement is:
create parray[j] of class Person using lage, lname
The string search on the name of an object is carried out in two steps within the loop. First, function main accesses and gets a copy of the attribute name of the object (of class Person) with index j. This is done by calling the accessor function get_name of the object and assigning it to string variable tname. Second, the function compares the target string target_name with the name attribute (in tname) of the object. The equals operator is used in an if statement for the string comparison.
set tname = call get_name of parray[j] if target_name equals tname // comparison
The general structure of the problem is the same as the linear search of a numeric array, which was discussed in Chapter 9. The complete KJP implementation of class Marrayperson follows.
description This is the main class in the program. It declares an array of objects of class Person. It creates objects of class Person and then manipulates the objects. */ class Marrayperson is public description This is the control function. */ function main is constants integer NUM_PERSONS = 15 // array capacity variables integer n // number elements integer lage // current age string lname // current name string target_name // name to search string tname integer j integer result_ind // element found boolean found = false objects object parray array [NUM_PERSONS] of class Person // body of function starts here begin display "Type number of objects to process: " read n for j = 0 to n - 1 do display "Type age of person: " read lage display "Type name of person: " read lname create parray[j] of class Person using lage, lname endfor display "Type target name: " read target_name // // linear search for target name // result is index of array element with // object with the name found, or -1 // set j = 0 while j < n and found not equal true do set tname = call get_name of parray[j] if target_name equals tname then set result_ind = j set found = true else increment j endif endwhile if found not equal true then set result_ind = -1 // name not found endif endfun main endclass Marrayperson
On the CD | The KJP implementation for this application is stored in several files. Class Marrayperson is stored in the file Marrayperson.kpl. Class Person is stored in the file Person.kpl. |