Back
Close

Java Guild Meeting 5/2018

Gi11i4m
4,379 views

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
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");
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
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
package optionals;
public class PhoneBookCrawler {
private PhoneBook2 phoneBook;
public PhoneBookCrawler(PhoneBook2 phoneBook) {
this.phoneBook = phoneBook;
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Exercise 3:

Implement findPhoneNumberByNameAndPrintPhoneBookIfNothingFound in PhoneBookCrawler that uses the implementation from exercise 1

Write the implementation
package optionals;
public class PhoneBookCrawler {
private PhoneBook2 phoneBook;
public PhoneBookCrawler(PhoneBook2 phoneBook) {
this.phoneBook = phoneBook;
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
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
package optionals;
public class PhoneBookCrawler {
private PhoneBook2 phoneBook;
public PhoneBookCrawler(PhoneBook2 phoneBook) {
this.phoneBook = phoneBook;
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
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
package optionals;
public class PhoneBookCrawler {
private PhoneBook2 phoneBook;
public PhoneBookCrawler(PhoneBook2 phoneBook) {
this.phoneBook = phoneBook;
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
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
package optionals;
public class PhoneBookCrawler {
private PhoneBook2 phoneBook;
public PhoneBookCrawler(PhoneBook2 phoneBook) {
this.phoneBook = phoneBook;
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
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.

Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io
codingame x discord
Join the CodinGame community on Discord to chat about puzzle contributions, challenges, streams, blog articles - all that good stuff!
JOIN US ON DISCORD
Online Participants