afiqakraam

classdataarray

Mar 27th, 2021
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.95 KB | None | 0 0
  1. /**
  2.  * Class Data Array
  3.  *
  4.  * @afiqakraam
  5.  * @24/03/2021
  6.  */
  7. class Person
  8. {
  9.     private String lastName;
  10.     private String firstName;
  11.     private int age;
  12.    
  13.     public Person(String last, String first, int a){
  14.         lastName = last;
  15.         firstName = first;
  16.         age = a;
  17.     }
  18.    
  19.     public void displayPerson(){
  20.         System.out.println("Last name : "+lastName);
  21.         System.out.println("First name : "+firstName);
  22.         System.out.println("Age : "+age);
  23.     }
  24.    
  25.     public String getLast(){
  26.         return lastName;
  27.     }
  28. }
  29. class ClassDataArray{
  30.     private Person[] a;
  31.     private int nElems;
  32.    
  33.     public ClassDataArray(int max){
  34.         a = new Person[max];
  35.         nElems = 0;
  36.     }
  37.    
  38.     public Person find(String searchName){
  39.         int j;
  40.         for(j = 0; j < nElems; j++)
  41.             if(a[j].getLast().equals(searchName))
  42.             break;
  43.         if(j == nElems)
  44.             return null;
  45.         else
  46.             return a[j];
  47.         }
  48.    
  49.     public void insert(String last,String first,int age){
  50.         a[nElems] = new Person(last,first,age);
  51.         nElems++;
  52.     }
  53.    
  54.     public boolean delete(String searchName){
  55.         int j;
  56.         for(j = 0; j < nElems; j++)
  57.             if(a[j].getLast().equals(searchName))
  58.                 break;
  59.                
  60.         if(j == nElems)
  61.             return false;
  62.         else{
  63.             for(int k=j; k < nElems; k++)
  64.                 a[k] = a[k+1];
  65.                 nElems--;
  66.             return true;
  67.         }
  68.     }
  69.    
  70.     public void displayA(){
  71.         for(int j = 0; j < nElems; j++)
  72.             a[j].displayPerson();
  73.         }
  74.     }
  75.    
  76. public class ClassDataApp{
  77.     public static void main(String[] args){
  78.         int maxSize = 100;
  79.         ClassDataArray arr;
  80.         arr = new ClassDataArray(maxSize);
  81.    
  82.         //Insert Elements
  83.         arr.insert("Evans", "Patty", 24);
  84.         arr.insert("Smith", "Loraine", 37);
  85.         arr.insert("Yee", "Tom", 43);
  86.         arr.insert("Adams", "Henry", 63);
  87.         arr.insert("Hashimoto", "Sato", 21);
  88.         arr.insert("Stimson", "Henry", 29);
  89.         arr.insert("Velasquez", "Jose", 72);
  90.         arr.insert("Lamarque", "Henry", 54);
  91.         arr.insert("Vang", "Minh", 22);
  92.         arr.insert("Creswell", "Lucinda", 18);
  93.            
  94.         //Display Elements
  95.         arr.displayA();
  96.        
  97.         //Search Element
  98.         String searchKey = "Stimson";
  99.         Person found;
  100.         found = arr.find(searchKey);
  101.         if(found !=null){
  102.             System.out.println("Found ");
  103.             found.displayPerson();
  104.         }
  105.         else
  106.             System.out.println("Can't find "+searchKey);
  107.        
  108.         //Delete Elements
  109.         System.out.println("Deleting Smith, Yee, and Creswell");
  110.         arr.delete("Smith");
  111.         arr.delete("Yee");
  112.         arr.delete("Creswell");
  113.        
  114.         //Display Elements
  115.         arr.displayA();
  116.     }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment