Java:Language - Optionals - Exercises

From Juneday education
Jump to: navigation, search

Work in progress!

Here, we'll add some exercises on using java.util.Optional.

Exercises

Here are the exercises on Optional<T>.

Q 1

Write a class Contact with the following specification:

Instance variables:

  • name : String
  • email : String
  • phone : String
  • workPhone : Optional<String>

Constructors:

  • Contact(name : String, email : String, phone : String)
    • Set this.workPhone to Optional.empty()
  • Contact(name : String, email : String, phone : String, workPhone : String)
    • Set this.workPhone to Optional.of(workPhone)

Behaviors (public methods):

  • name() : String
  • email() : String
  • phone() : String
  • workPhone() : Optional<String>
  • toString() : String
    • If workPhone exists, it should be included in the String, otherwise not

We apologize for this incapable class - a Contact object seems pretty unable to do interesting things, but the focus of this exercise is for you to practice using java.util.Optional.

Write a small program, ContactDemo, which creates one Contact with a workPhone and one without.

The program should check whether the two (you are free to create more) Contact objects has a workPhone or not. The program should also try out the toString() of the two Contact objects, and you should visually verify that they work as specified.

Expand using link to the right to see a suggested solution.

Here's a minimal suggested solution.

// Contact.java:
import java.util.Optional;

public class Contact {

  private String name;
  private String email;
  private String phone;
  private Optional<String> workPhone;

  public Contact(String name, String email, String phone) {
    this.name = name;
    this.email = email;
    this.phone = phone;
    this.workPhone = Optional.empty();
  }

  public Contact(String name, String email, String phone, String workPhone) {
    this(name, email, phone);
    this.workPhone = Optional.of(workPhone);
  }

  public String name() {
    return name;
  }

  public String email() {
    return email;
  }

  public String phone() {
    return phone;
  }

  public Optional<String> workPhone() {
    return workPhone;
  }

  @Override
  public String toString() {
    return new StringBuilder(name)
      .append(" ")
      .append(email)
      .append(" ")
      .append(phone)
      .append(" ")
      .append(workPhone.orElse(""))
      .toString().trim();
  }

}

//ContactDemo.java:
public class ContactDemo {

  public static void main(String[] args) {
    Contact unemployed = new Contact("Hank", "hank@gais.se", "123456");
    Contact desputado = new Contact("Ozzy", "ozzy@666.com", "13-666-666666", "031-905109");
    System.out.println("Does unemployed have a work phone? " +
                       (unemployed.workPhone().isPresent() ? " Yes: " + unemployed.workPhone().get()
                        : " nope."));
    System.out.println("Does desputado have a work phone? " +
                       (desputado.workPhone().isPresent() ? " Yes: " + desputado.workPhone().get()
                        : " nope."));
    System.out.println(unemployed);
    System.out.println(desputado);
  }
}

Where to go next

Next page is not yet decided.

The TOC (Table Of Contents) go to More_programming_with_Java#Java_Language

The Previous link goes to the page with theory and examples on Optionals.

« PreviousBook TOCNext »