Professional Documents
Culture Documents
Lab 05
Lab 05
Lab 05
Objectives:
1. Write three methods to perform the following actions. Each method receives a List of Person objects as input.
a. Sort the list of persons by their surname.
b. Group the persons into two lists by their gender. The function returns a Map object that map the gender to
the corresponding list of persons.
c. Calculate the average age of each gender for the list of persons. The function returns a list with two Entry
objects. Each entry associate the gender to the average age.
2. Given two sets of fruits, A and B, write a program to find out their union, intersection and complements of each
set.
3. Given the class Card.java, write a program to do the following tasks using JCF and print out the result of each
task.
a. Initialize a normal 52-card deck in its natural order as shown below.
b. Shuffle the deck
c. Deal four hands of cards and sort each hand in its natural order
The natural order (first line) and a sample output is shown as below.
Initial: [♠2, ♠3, ♠4, ♠5, ♠6, ♠7, ♠8, ♠9, ♠10, ♠J, ♠Q, ♠K, ♠A, ♥2, ♥3, ♥4, ♥5, ♥6, ♥7, ♥8, ♥9,
♥10, ♥J, ♥Q, ♥K, ♥A, ♣2, ♣3, ♣4, ♣5, ♣6, ♣7, ♣8, ♣9, ♣10, ♣J, ♣Q, ♣K, ♣A, ♦2, ♦3, ♦4, ♦5, ♦6, ♦7, ♦8,
♦9, ♦10, ♦J, ♦Q, ♦K, ♦A]
After shuffle: [♥3, ♦4, ♠Q, ♥7, ♣2, ♥10, ♠9, ♦3, ♣8, ♣3, ♦6, ♦7, ♦Q, ♦8, ♣J, ♥6, ♠3, ♦2, ♠7, ♠8, ♥4,
♥2, ♠10, ♦10, ♥K, ♠A, ♦9, ♦5, ♥J, ♣Q, ♣10, ♣5, ♣K, ♠J, ♠K, ♥9, ♥5, ♥8, ♠2, ♣6, ♠6, ♥A, ♣A, ♠5, ♣4, ♦K,
♣7, ♥Q, ♦J, ♣9, ♦A, ♠4]
Hand 1: [♠9, ♠Q, ♥3, ♥7, ♥10, ♣2, ♣3, ♣8, ♦3, ♦4, ♦6, ♦7, ♦Q]
Hand 2: [♠3, ♠7, ♠8, ♠10, ♠A, ♥2, ♥4, ♥6, ♥K, ♣J, ♦2, ♦8, ♦10]
Hand 3: [♠2, ♠J, ♠K, ♥5, ♥8, ♥9, ♥J, ♣5, ♣10, ♣Q, ♣K, ♦5, ♦9]
Hand 4: [♠4, ♠5, ♠6, ♥Q, ♥A, ♣4, ♣6, ♣7, ♣9, ♣A, ♦J, ♦K, ♦A]
- END –
Appendix – Person and Card Classes
package lab05;
import java.util.ArrayList;
import java.util.List;
@Override
public String toString() {
return "Name: " + givenName + " " + surName + "\n" + "Age: " + age + " Gender: " + gender +
"\n" + "eMail: " + eMail + "\n" + "Address: " + address + "\n";
}
people.add(
new Person.Builder()
.givenName("Bob")
.surName("Baker")
.age(21)
.gender(Gender.MALE)
.email("bob.baker@example.com")
.phoneNumber("201-121-4678")
.address("44 4th St, Smallville, KS 12333")
.build()
);
people.add(
new Person.Builder()
.givenName("Jane")
.surName("Jones")
.age(25)
.gender(Gender.FEMALE)
.email("jane.doe@example.com")
.phoneNumber("202-123-4678")
.address("33 3rd St, Smallville, KS 12333")
.build()
);
people.add(
new Person.Builder()
.givenName("John")
.surName("Baker")
.age(25)
.gender(Gender.MALE)
.email("john.doe@example.com")
.phoneNumber("202-123-4678")
.address("33 3rd St, Smallville, KS 12333")
.build()
);
people.add(
new Person.Builder()
.givenName("James")
.surName("Johnson")
.age(45)
.gender(Gender.MALE)
.email("james.johnson@example.com")
.phoneNumber("333-456-1233")
.address("201 2nd St, New York, NY 12111")
.build()
);
people.add(
new Person.Builder()
.givenName("Joe")
.surName("Baker")
.age(67)
.gender(Gender.MALE)
.email("joebob.bailey@example.com")
.phoneNumber("112-111-1111")
.address("111 1st St, Town, CA 11111")
.build()
);
people.add(
new Person.Builder()
.givenName("Phil")
.surName("Smith")
.age(55)
.gender(Gender.MALE)
.email("phil.smith@examp;e.com")
.phoneNumber("222-33-1234")
.address("22 2nd St, New Park, CO 222333")
.build()
);
people.add(
new Person.Builder()
.givenName("Betty")
.surName("Jones")
.age(85)
.gender(Gender.FEMALE)
.email("betty.jones@example.com")
.phoneNumber("211-33-1234")
.address("22 4th St, New Park, CO 222333")
.build()
);
return people;
}
package lab05;
@Override
public String toString() {
return suit + rank;
}
}