Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Write a description of class Person here.
- *
- * @author (your name)
- * @version (a version number or a date)
- */
- class Person
- {
- private String lastName;
- private String firstName;
- private int age;
- public Person(String last, String first, int a){
- lastName = last;
- firstName = first;
- age = a;
- }
- public void displayPerson(){
- System.out.println("Last name:"+ lastName);
- System.out.println("First name:"+ firstName);
- System.out.println("Age:"+ age);
- }
- public String getLast(){
- return lastName;
- }
- }
- class ClassDataArray{
- private Person[] a;
- private int nElems;
- public ClassDataArray(int max){
- a = new Person[max];
- nElems = 0;
- }
- public Person find(String searchName){
- int j;
- for(j=0; j<nElems; j++)
- if(a[j].getLast().equals(searchName))
- break;
- if(j == nElems)
- return null;
- else
- return a[j];
- }
- public void insert(String last,String first,int age){
- a[nElems] = new Person(last,first,age);
- nElems++;
- }
- public boolean delete(String searchName){
- int j;
- for(j=0; j<nElems; j++)
- if(a[j].getLast().equals(searchName))
- break;
- if(j == nElems)
- return false;
- else{
- for(int k=j; k<nElems; k++)
- a[k] = a[k+1];
- nElems--;
- return true;
- }
- }
- public void displayA(){
- for(int j=0; j<nElems; j++)
- a[j].displayPerson();
- }
- }
- public class ClassDataApp{
- public static void main(String[] args){
- int maxSize = 100;
- ClassDataArray arr;
- arr = new ClassDataArray(maxSize);
- //insert items
- arr.insert("Evans", "Patty", 24);
- arr.insert("Smith", "Loraine", 37);
- arr.insert("Yee", "Tom", 43);
- arr.insert("Adams", "Henry", 63);
- arr.insert("Hashimoto", "Sato", 21);
- arr.insert("Stimson", "Henry", 29);
- arr.insert("Velasquez", "Jose", 72);
- arr.insert("Lamarque", "Henry", 54);
- arr.insert("Vang", "Minh", 22);
- arr.insert("Creswell", "Lucinda", 18);
- //display items
- arr.displayA();
- //search items
- String searchKey = "Stimson";
- Person found;
- found = arr.find(searchKey);
- if(found !=null){
- System.out.println("Found ");
- found.displayPerson();
- }
- else
- System.out.println("Can't find "+searchKey);
- //delete items
- System.out.println("Deleting Smith, Yee, and Creswell");
- arr.delete("Smith");
- arr.delete("Yee");
- arr.delete("Creswell");
- //display items
- arr.displayA();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement