Java Guild Meeting 5/2018

Gi11i4m
2,090 views

Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content

Exercises

The exercises for optionals will focus around a PhoneBook class. The phone book already has some initial values:

  • Jos de Vos
  • An de Toekan
  • Kris de Vis

Exercise 1:

Implement findPhoneNumberByName and findNameByPhoneNumber in PhoneBook class that returns an optional. An empty optional must be returned if nothing is found.

Write the implementation
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
package optionals;
import java.util.*;
public class PhoneBook {
private static final HashMap<String, String> PHONE_NUMBERS = new HashMap<String, String>() {
{
put("Jos de Vos", "016/161616");
put("An de Toekan", "016/161617");
put("Kris de Vis", "016/161618");
}
};
private HashMap<String, String> phoneBookEntries = PHONE_NUMBERS;
PhoneBook() { }
public HashMap<String, String> getPhoneBookEntries() {
return phoneBookEntries;
}
public Optional<String> findPhoneNumberByName(String name){
return null;
}
public Optional<String> findNameByPhoneNumber(String phoneNumber){
return null;
}
@Override
public String toString() {
System.out.println("Hello from PhoneBook's toString method");
return "PhoneBook{" +
"phoneBookEntries=" + phoneBookEntries +
'}';
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
1
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Exercise 2:

Implement findPhoneNumberByNameAndPunishIfNothingFound in PhoneBookCrawler that uses an implementation of the PhoneBook class. Punish with an IllegalArgumentException and message "No phone number found".

Write the implementation
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
package optionals;
public class PhoneBookCrawler {
private PhoneBook2 phoneBook;
public PhoneBookCrawler(PhoneBook2 phoneBook) {
this.phoneBook = phoneBook;
}
public String findPhoneNumberByNameAndPunishIfNothingFound(String name){
return null;
}
public String findPhoneNumberByNameAndPrintPhoneBookIfNothingFound(String name){
return null;
}
public String findPhoneNumberByNameOrNameByPhoneNumber(String name, String phoneNumber){
return null;
}
public PhoneBook2 getPhoneBook(){
return phoneBook;
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
1
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Exercise 3:

Implement findPhoneNumberByNameAndPrintPhoneBookIfNothingFound in PhoneBookCrawler that uses the implementation from exercise 1

Write the implementation
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
package optionals;
public class PhoneBookCrawler {
private PhoneBook2 phoneBook;
public PhoneBookCrawler(PhoneBook2 phoneBook) {
this.phoneBook = phoneBook;
}
public String findPhoneNumberByNameAndPunishIfNothingFound(String name){
return null;
}
public String findPhoneNumberByNameAndPrintPhoneBookIfNothingFound(String name){
return null;
}
public String findPhoneNumberByNameOrNameByPhoneNumber(String name, String phoneNumber){
return null;
}
public PhoneBook2 getPhoneBook(){
return phoneBook;
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
1
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Exercise 4

Did you receive 1 or 2 Hello messages from the PhoneBook's toString method when you ran the tests? If you received 2 Hello messages, reimplement it so that you receive only 1 Hello message after running the tests. If you received just 1 Hello message, reimplement exercise 3 to have it actually printed twice.

Write the implementation
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
package optionals;
public class PhoneBookCrawler {
private PhoneBook2 phoneBook;
public PhoneBookCrawler(PhoneBook2 phoneBook) {
this.phoneBook = phoneBook;
}
public String findPhoneNumberByNameAndPunishIfNothingFound(String name){
return null;
}
public String findPhoneNumberByNameAndPrintPhoneBookIfNothingFound(String name){
return null;
}
public String findPhoneNumberByNameOrNameByPhoneNumber(String name, String phoneNumber){
return null;
}
public PhoneBook2 getPhoneBook(){
return phoneBook;
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
1
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Exercise 5

After the 2 previous exercises, you should now know the difference between using the orElse and orElseGet methods. Optionals also work nicely with streams Can you reimplement exercise 2 using streams instead of using PhoneBook's findPhoneNumberByName

Write the implementation
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
package optionals;
public class PhoneBookCrawler {
private PhoneBook2 phoneBook;
public PhoneBookCrawler(PhoneBook2 phoneBook) {
this.phoneBook = phoneBook;
}
public String findPhoneNumberByNameAndPunishIfNothingFound(String name){
return null;
}
public String findPhoneNumberByNameAndPrintPhoneBookIfNothingFound(String name){
return null;
}
public String findPhoneNumberByNameOrNameByPhoneNumber(String name, String phoneNumber){
return null;
}
public PhoneBook2 getPhoneBook(){
return phoneBook;
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
1
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Exercise 6

Implement findPhoneNumberByNameOrNameByPhoneNumber in PhoneBookCrawler class. First search the phone book by name. If that returns nothing search the phone book by phone number. If that still returns nothing return the phone number of Jos de Vos.

Write the implementation
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
package optionals;
public class PhoneBookCrawler {
private PhoneBook2 phoneBook;
public PhoneBookCrawler(PhoneBook2 phoneBook) {
this.phoneBook = phoneBook;
}
public String findPhoneNumberByNameAndPunishIfNothingFound(String name){
return null;
}
public String findPhoneNumberByNameAndPrintPhoneBookIfNothingFound(String name){
return null;
}
public String findPhoneNumberByNameOrNameByPhoneNumber(String name, String phoneNumber){
return null;
}
public PhoneBook2 getPhoneBook(){
return phoneBook;
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
1
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

The exercises focused on Java 8 Optional features. Java 9 has introduced the or method that should allow an easier implementation for the last exercise.

Open Source Your Knowledge: become a Contributor and help others learn. Create New Content