This document defines a Person class with properties for name, first name, and age. It also defines a ClassDataArray class to store Person objects in an array. ClassDataArray contains methods to insert, find, and delete Person objects from the array. The main method creates a ClassDataArray, inserts sample Person data, displays the array, finds a Person by name, deletes 3 Persons, and redisplays the updated array.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
60 views3 pages
Listing 2.5 Source Code Text
This document defines a Person class with properties for name, first name, and age. It also defines a ClassDataArray class to store Person objects in an array. ClassDataArray contains methods to insert, find, and delete Person objects from the array. The main method creates a ClassDataArray, inserts sample Person data, displays the array, finds a Person by name, deletes 3 Persons, and redisplays the updated array.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3
//Name: Cololot, John Lloyd M.
//Course-Yr. & Section: BSIT 2A-Day
//Subject: Data Structures and Algorithms //Instructor: Sir Almendras
public class Person {
private String lastName;
private String firstName; private int age;
public Person(String last, String first, int a) {
lastName = last; //constructor firstName = first; age = a; } public void displayPerson() { System.out.print(" Last name: " + lastName); System.out.print(", First name: " + firstName); System.out.println(", Age: " + age); } public String getLast() //get last name { return lastName; } } //end class Person class ClassDataArray{ private Person[] a; //reference to array private int nElems; //number of data items
public ClassDataArray(int max) { //constructor
a = new Person[max]; //create the array
nElems = 0; // no items yet }
public Person find (String searchName) { //find specified value
int j; for (j = 0; j < nElems; j++) //for each element, if ( a[j].getLast().equals(searchName) ) //found item? break; //exit loop before end if (j == nElems) //gone to end? return null; //yes, can't find it else return a[j]; //no, found it }// end find()
//public person into array
public void insert(String last, String first, int age) { a[nElems] = new Person(last, first, age); nElems++; //increment size }
public boolean delete(String searchName) { //delete person from
array int j; for (j = 0; j < nElems; j++) //look for it if ( a[j].getLast().equals(searchName) ) break; if (j == nElems) //can't find it return false; else { // found it for (int k = j; k < nElems; k++) //shift down a[k] = a[k + 1]; nElems--; //decrement size return true; } } //end delete()
public void displayA() { //displays array
contents for (int j = 0; j <nElems; j++) //for each element, a[j].displayPerson(); //display it } } // end class ClassDataArray