Advertisement
Guest User

Untitled

a guest
Mar 29th, 2021
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.96 KB | None | 0 0
  1. /**
  2. * Write a description of class Person here.
  3. *
  4. * @author (your name)
  5. * @version (a version number or a date)
  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 items
  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 items
  95. arr.displayA();
  96.  
  97. //search items
  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 items
  109. System.out.println("Deleting Smith, Yee, and Creswell");
  110. arr.delete("Smith");
  111. arr.delete("Yee");
  112. arr.delete("Creswell");
  113.  
  114. //display items
  115. arr.displayA();
  116. }
  117. }
  118.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement