Skip to content

Commit 9a3ef93

Browse files
author
Woody
committed
update August 23
1 parent c8bd16a commit 9a3ef93

27 files changed

+713
-7
lines changed

chapter_10/AddressBook.java

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
package chapter_10;
2+
3+
import java.util.ArrayList;
4+
5+
public class AddressBook {
6+
private static final int DEFAULT_SIZE = 25;
7+
private Person[] entry;
8+
private int count;
9+
10+
public AddressBook(){
11+
this(DEFAULT_SIZE);
12+
}
13+
14+
15+
public void expand(){
16+
//create a new array whose size is 150% of the current array
17+
int newLength = (int) (1.5 * entry.length);
18+
Person[] temp = new Person[newLength];
19+
20+
//copy the data to the new array
21+
22+
for(int i = 0 ; i < entry.length ; i++) {
23+
temp[i] = entry[i];
24+
}
25+
26+
//set the variable entry to point to the new array
27+
entry = temp;
28+
29+
System.out.println("Inside the method enlarge."); //TEMP
30+
31+
System.out.println("Size of a new array : " + entry.length);
32+
33+
}
34+
35+
public void add (Person newPerson) {
36+
assert count >= 0 &&
37+
count <= entry.length;
38+
39+
if(count == entry.length) {
40+
expand();
41+
}
42+
43+
entry[count] = newPerson;
44+
count++;
45+
}
46+
47+
public AddressBook(int size){
48+
count = 0;
49+
if(size <= 0) {
50+
throw new IllegalArgumentException("Size must be positive.");
51+
}
52+
entry = new Person[size];
53+
54+
System.out.println("Array of " + size + " is created.");
55+
56+
}
57+
58+
public Person searchName (String searchName) {
59+
Person foundPerson;
60+
int loc = 0 ;
61+
62+
assert count >= 0 && count<= entry.length;
63+
64+
while(loc < count && !searchName.equals(entry[loc].getName())) {
65+
loc++;
66+
};
67+
68+
if(loc == count) {
69+
foundPerson = null;
70+
}else {
71+
foundPerson = entry[loc];
72+
}
73+
74+
return foundPerson;
75+
}
76+
77+
public Person searchAge (int searchAge) {
78+
Person foundPerson;
79+
int loc = 0 ;
80+
81+
assert count >= 0 && count<= entry.length;
82+
83+
while(loc < count && searchAge != entry[loc].getAge()) {
84+
loc++;
85+
};
86+
87+
if(loc == count) {
88+
foundPerson = null;
89+
}else {
90+
foundPerson = entry[loc];
91+
}
92+
93+
return foundPerson;
94+
}
95+
96+
public Person searchGender (char searchGender) {
97+
Person foundPerson;
98+
int loc = 0 ;
99+
100+
assert count >= 0 && count<= entry.length;
101+
102+
while(loc < count && searchGender != entry[loc].getGender()) {
103+
loc++;
104+
};
105+
106+
if(loc == count) {
107+
foundPerson = null;
108+
}else {
109+
foundPerson = entry[loc];
110+
}
111+
112+
return foundPerson;
113+
}
114+
115+
// exercise 14
116+
public ArrayList<Person> searchMultiple(String searchName) {
117+
ArrayList<Person> foundPerson = new ArrayList<Person>();
118+
119+
assert count >= 0 && count < entry.length;
120+
121+
for (int i = 0 ; i< entry.length ; i++) {
122+
if(searchName.equals(entry[i].getName())) {
123+
foundPerson.add(entry[i]);
124+
}
125+
}
126+
127+
return foundPerson;
128+
}
129+
130+
131+
}

chapter_10/ArrayListPerson.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package chapter_10;
2+
import java.util.ArrayList;
3+
import java.util.List;
4+
5+
public class ArrayListPerson {
6+
public static void main(String[] args) {
7+
List<Person> personList = new ArrayList<Person>();
8+
Person person;
9+
person = new Person("Su Su",12,'F');
10+
personList.add(person);
11+
12+
person = new Person("Aung Aung", 13, 'M');
13+
personList.add(person);
14+
15+
for(Person p : personList) {
16+
System.out.println("Name : " + p.getName() + " Age : " + p.getAge() + " Gender : " + p.getGender());
17+
}
18+
19+
}
20+
}

chapter_10/Map_eg.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package chapter_10;
2+
import java.util.*;
3+
4+
public class Map_eg {
5+
public static void main(String[] args) {
6+
Map<String,String> catalog;
7+
8+
catalog = new TreeMap<String,String>();
9+
10+
catalog.put("CS101","Intro Java Programming");
11+
catalog.put("CS301","Database Design");
12+
catalog.put("CS413", "Software Design for Mobile Devices");
13+
14+
if(catalog.containsKey("CS101")) {
15+
System.out.println("We teach Java this semester");
16+
17+
}else {
18+
System.out.println("No Java courses this semester");
19+
20+
}
21+
22+
for(Map.Entry<String,String> entry : catalog.entrySet()) {
23+
System.out.println(entry.getKey() + ":\t" + entry.getValue());
24+
}
25+
26+
}
27+
}

chapter_10/PayScaleTable.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package chapter_10;
2+
3+
public class PayScaleTable {
4+
public static void main(String[] args) {
5+
6+
double payScale[][] = {
7+
{ 10.50, 12.00, 14.50, 16.75, 18.00 },
8+
{ 20.50, 22.25, 24.00, 26.25, 28.00 },
9+
{ 34.00, 36.50, 38.00, 40.35, 43.00 },
10+
{ 50.00, 60.00, 70.00, 80.00, 99.99 }
11+
};
12+
13+
double averageGrade[] = new double[4];
14+
15+
16+
for (int i = 0 ; i< payScale.length; i++) {
17+
18+
for (int j = 0 ; j < payScale[i].length ; j++) {
19+
averageGrade[i] += payScale[i][j];
20+
}
21+
averageGrade[i] /= payScale[i].length;
22+
}
23+
24+
25+
26+
for(int i = 0 ;i < averageGrade.length ; i++) {
27+
System.out.println("Average of Grade Level " + i + " Employees : " + averageGrade[i]);
28+
}
29+
30+
31+
32+
}
33+
34+
}

chapter_10/Person.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,30 @@
22

33
public class Person {
44
String name;
5-
5+
int age;
6+
char gender;
7+
68
public String getName() {
79
return name;
810
}
911
public void setName(String name) {
1012
this.name = name;
1113
}
12-
int age;
1314
public int getAge() {
1415
return age;
1516
}
1617
public void setAge(int age) {
1718
this.age = age;
1819
}
19-
char gender;
20+
21+
public Person(String name) {
22+
this.name = name;
23+
}
24+
public Person(String name, int age, char gender) {
25+
this.name = name;
26+
this.age = age;
27+
this.gender = gender;
28+
}
2029
public char getGender() {
2130
return gender;
2231
}

chapter_10/RaggedArray.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package chapter_10;
2+
3+
public class RaggedArray {
4+
public static void main(String[] args) {
5+
double [][] triangularArray;
6+
triangularArray = new double[4][];
7+
for (int i =0;i<4;i++) {
8+
triangularArray[i] = new double [i+1];
9+
}
10+
11+
}
12+
}

chapter_10/TestAddressBook.java

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package chapter_10;
2+
import java.util.*;
3+
4+
public class TestAddressBook {
5+
AddressBook myBook;
6+
Person person;
7+
8+
public void setupArray(int N){
9+
myBook = new AddressBook(N);
10+
11+
//add N Person objects
12+
for(int i = 0 ;i < N ; i++) {
13+
person = new Person("Ms. X" + i , 10 , 'F');
14+
myBook.add(person);
15+
}
16+
17+
}
18+
19+
public void testSearch() {
20+
//test for the end case
21+
// person = myBook.searchName("Ms. X2");
22+
23+
//test for exercise 15
24+
// person = myBook.searchAge(10);
25+
person = myBook.searchGender('M');
26+
27+
if(person == null) {
28+
System.out.println("Error Didn't find the person it should");
29+
}else {
30+
System.out.println(person.getName() + " is found okay.");
31+
}
32+
}
33+
34+
// test exercise 14
35+
public void testMultipleSearch(){
36+
AddressBook addressBook = new AddressBook(5);
37+
38+
// Add some persons to the address book
39+
addressBook.add(new Person("Alice"));
40+
addressBook.add(new Person("Bob"));
41+
addressBook.add(new Person("Alice"));
42+
addressBook.add(new Person("Charlie"));
43+
addressBook.add(new Person("Alice"));
44+
45+
ArrayList<Person> personList = addressBook.searchMultiple("Alice");
46+
47+
if(personList == null){
48+
System.out.println("No person found");
49+
}else {
50+
for(Person person : personList) {
51+
System.out.print(person.getName() + " , ");
52+
}
53+
}
54+
}
55+
56+
public static void main(String[] args) {
57+
TestAddressBook tester = new TestAddressBook();
58+
tester.setupArray(5);
59+
tester.testSearch();
60+
tester.testMultipleSearch();
61+
}
62+
63+
64+
}

chapter_10/collection_eg1.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package chapter_10;
2+
import java.util.ArrayList;
3+
import java.util.List;
4+
5+
public class collection_eg1 {
6+
public static void main(String [] args) {
7+
List<String> fruits = new ArrayList<String>();
8+
fruits.add("apple");
9+
fruits.add("orange");
10+
fruits.add("banana");
11+
12+
for(String f : fruits) {
13+
System.out.println(f);
14+
}
15+
16+
ArrayList<Integer> num = new ArrayList<Integer>();
17+
num.add(1);
18+
num.add(2);
19+
num.add(3);
20+
21+
for (Integer i : num) {
22+
System.out.println(i);
23+
}
24+
}
25+
}

chapter_10/ex_1.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package chapter_10;
2+
3+
public class ex_1 {
4+
public static int[] searchAccount(int[] number){
5+
number = new int[15];
6+
7+
for(int i = 0 ; i< number.length ; i++){
8+
number[i] = number[i-1] + number[i+1];
9+
}
10+
11+
return number;
12+
13+
}
14+
}

0 commit comments

Comments
 (0)