Lab 05

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Lab 05 – Generics and Collections

Objectives:

● Learn to use generics classes with parametric type


● Learn to apply Java Collections Framework to solve practical problems

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.

Set A: [apple, banana, durian, grape, papaya]


Set B: [banana, mango, papaya, tomato, watermelon]

Discuss the following items with your classmates:


a. Compare the difference between using HashSet and TreeSet in the program above.
b. Why is it a good practice to immediately assign a newly created collection (e.g. HashSet) to its
correspondingly interface type (e.g 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;

public class Person {

public enum Gender {


MALE, FEMALE
}

private String givenName;


private String surName;
private int age;
private Gender gender;
private String eMail;
private String phone;
private String address;

public static class Builder {

private String givenName = "";


private String surName = "";
private int age = 0;
private Gender gender = Gender.FEMALE;
private String email = "";
private String phone = "";
private String address = "";

public Person.Builder givenName(String givenName) {


this.givenName = givenName;
return this;
}

public Person.Builder surName(String surName) {


this.surName = surName;
return this;
}

public Person.Builder age(int val) {


age = val;
return this;
}

public Person.Builder gender(Gender val) {


gender = val;
return this;
}

public Person.Builder email(String val) {


email = val;
return this;
}

public Person.Builder phoneNumber(String val) {


phone = val;
return this;
}

public Person.Builder address(String val) {


address = val;
return this;
}

public Person build() {


return new Person(this);
}
}
private Person() {
super();
}

private Person(Person.Builder builder) {


givenName = builder.givenName;
surName = builder.surName;
age = builder.age;
gender = builder.gender;
eMail = builder.email;
phone = builder.phone;
address = builder.address;

public String getName() {


return givenName + " " + surName;
}

public String getGivenName() {


return givenName;
}

public String getSurName() {


return surName;
}

public int getAge() {


return age;
}

public Gender getGender() {


return gender;
}

public String getEmail() {


return eMail;
}

public void setAge(int age) {


this.age = age;
}

public void print() {


System.out.println(
"\nName: " + givenName + " " + surName + "\n"
+ "Age: " + age + "\n"
+ "Gender: " + gender + "\n"
+ "eMail: " + eMail + "\n"
+ "Phone: " + phone + "\n"
+ "Address: " + address + "\n"
);
}

@Override
public String toString() {
return "Name: " + givenName + " " + surName + "\n" + "Age: " + age + " Gender: " + gender +
"\n" + "eMail: " + eMail + "\n" + "Address: " + address + "\n";
}

public static List<Person> createShortList() {


List<Person> people = new ArrayList<>();

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;

public class Card {

// spades, hearts, clubs, diamonds


public static final String[] SUITS = new String[]{"\u2660", "\u2665", "\u2663", "\u2666"};
public static final String[] RANKS = new String[]{"2", "3", "4", "5", "6", "7", "8", "9", "10",
"J", "Q", "K", "A"};
public final String suit;
public final String rank;

public Card(String suit, String rank) {


this.suit = suit;
this.rank = rank;
}

@Override
public String toString() {
return suit + rank;
}
}

You might also like