forked from biblelamp/JavaExercises
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclassDataArray.java
More file actions
115 lines (100 loc) · 3.89 KB
/
classDataArray.java
File metadata and controls
115 lines (100 loc) · 3.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/**
* Book: Data Structures and Algorithms in Java, by Robert LaFore
* Chapter 2:
* classDataArray.java
* data items as class objects
* to compile this code: javac classDataArray.java
* to run this program: java ClassDataApp
*/
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.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
// put person into array
public void insert(String last, String first, int age) {
a[nElems] = new Person(last, first, age);
nElems++; // increment size
}
// delete person from array
public boolean delete(String searchName) {
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
class ClassDataApp {
public static void main(String[] args) {
int maxSize = 100; // array size
ClassDataArray arr; // reference to array
arr = new ClassDataArray(maxSize); // create the array
// insert 10 items
arr.insert("Evans", "Patty", 24);
arr.insert("Smith", "Lorraine", 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);
arr.displayA(); // display items
String searchKey = "Stimson"; // search for item
Person found;
found = arr.find(searchKey);
if(found != null) {
System.out.print("Found ");
found.displayPerson();
} else
System.out.println("Can't find " + searchKey);
System.out.println("Deleting Smith, Yee, and Creswell");
arr.delete("Smith"); // delete 3 items
arr.delete("Yee");
arr.delete("Creswell");
arr.displayA(); // display items again
} // end main()
} // end class ClassDataApp