





















































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!






















































-String name-int id-Gender gender-int age-String city-String province-String country-DateTime lastLogon-??? friends+Person(Scanner in)+String getName( )+int getID( )+Gender getGender( )...
public class PersonList extends Object{ … persons …
/** Swap the person object at index a with the object at index b. */ public void swap(int a, int b){ Person temp = this.persons[a];
this.persons[a] = this.persons[b];this.persons[b] = temp; }
Person temp = this.persons[a];
Person[ ] length
4
[0] [1] [2] [3]
perso ns
Steve,
MALE, 16,
St.
Cat
Ken,
MALE, 18,
Niagara-on
Beth,
FEMALE, 17, St. Cat
Kathleen,
FEMALE, 23,
Tim
temp
this.persons[a] = this.persons[b];
Person[ ] length
4
[0] [1] [2] [3]
perso ns
Steve,
MALE, 16,
St.
Cat
Ken,
MALE, 18,
Niagara-on
Beth,
FEMALE, 17, St. Cat
Kathleen,
FEMALE, 23,
Tim
temp
this.persons[b] = temp;
Person[ ] length
4
[0] [1] [2] [3]
perso ns
Steve,
MALE, 16,
St.
Cat
Ken,
MALE, 18,
Niagara-on
Beth,
FEMALE, 17, St. Cat
Kathleen,
FEMALE, 23,
Tim
temp
// After the swap method finishes
Person[ ] length
4
[0] [1] [2]
perso ns
Steve,
MALE, 16,
St.
Cat
Ken,
MALE, 18,
Niagara-on
Beth,
FEMALE, 17, St. Cat
public class PersonList extends Object{ … persons …
/** Calculate the average number of friends */ public double calcAverageNumberOfFriends(){ int sumFriends = 0;
for( int i = 0; i < this.persons.length; i += 1){ Person p = this.persons[i];
sumFriends = sumFriends + p.getNumFriends(); } return (double) sumFriends / this.persons.length; } /** Calculate the average number of friends using a “foreach loop” */ public double calcAverageNumberOfFriends(){ int sumFriends = 0;
for( Person p : this.persons ){ sumFriends = sumFriends + p.getNumFriends();} return (double) sumFriends / this.persons.length; } }
for (
each element in the array
{ if (
the element meets some criteria
process the element } } public class PersonList extends Object{ … persons …
/** Count the number of minors (persons less than 18 years old). */ public int countMinors(){ int count = 0;
for(int i = 0; i < this.persons.length; i += 1){ if (this.persons[i].getAge() < 18)
{ count += 1;} } return count; } }
public class PersonList extends Object{ … persons …
/** Find the person with the given id; null if not found. Use a traditional loop */ public Person search2(int id){ int i = 0;
while (i < this.persons.length && this.persons[i].getID() != id){ i += 1;} Person answer = null;if (i < this.persons.length){ answer = this.persons[i];} return answer; } }
each remaining element in the array
{ if (
the current element is better than the best seen so far
remember the current element as the best seen so far } } return
best element seen so far
Person[ ] length
7
[0] [1] [2] [3] [4] [5] [6]
perso ns
F aizel,
MALE,
16,
St.
Ca
D oug,
MALE,
18,
Niagara-
B e
th,
FEMALE,
17,
St.
C athy,
FEMALE,
23,
Tim
G reg,
MALE,
21,
Fonth
E llen, FEMALE, 18, Niaga A li,
MALE,
19,
Boston,
Person[ ] length
7
[0] [1] [2] [3] [4] [5] [6]
perso ns
F aizel,
MALE,
16,
St.
Cat
D o
ug,
MALE,
18,
Niagara-on
B e
th,
FEMALE,
17,
St.
C
C athy,
FEMALE,
23,
Tim
G reg,
MALE,
21,
Fonth
E llen, FEMALE, 18, Niaga A l
i, MALE,
19,
Boston, MS
public class PersonList extends Object{ … persons …
/** Sort the list of persons in alphabetical order by name. */^ public void sort(){ for (int firstUnsorted=0; firstUnsorted<this.persons.length-1;
firstUnsorted++)
{ int extremeIndex = this.findExtreme(firstUnsorted);
this.swap(firstUnsorted, extremeIndex); } } /** Swap the elements at indices a and b. */ private void swap(int a, int b){ Person temp = this.persons[a];
this.persons[a] = this.persons[b];this.persons[b] = temp; }
/** Find the extreme element in the unsorted portion of the array.* @param indexToStart the smallest index in the unsorted portion of the array */ private int findExtreme(int indexToStart){ int indexBestSoFar = indexToStart;
String nameBestSoFar = this.persons[indexBestSoFar].getName();for (int i=indexToStart+1; i<this.persons.length; i++){ String currPersonName = this.persons[i].getName();
if (currPersonName.compareTo(nameBestSoFar) < 0){ indexBestSoFar = i;
nameBestSoFar = this.persons[i].getName(); } } return indexBestSoFar; } }