Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Case Class

Exercise 1: Defining and Instantiating Case Classes

Task: Define a case class named Book with two fields: title (String) and authors (List[String]). Create instances of this class for three different books.

Exercise 2: Pattern Matching on Case Classes

Task: Write a function describeBook that accepts a Book and returns a string describing the book. If the book has more than one author, the description should note that it's written by multiple authors.

Exercise 3: Copying and Modifying Case Classes

Task: Using the Book case class from Exercise 1, create a copy of book1 but with an additional author "Venners Bill". Use the copy method provided by case classes.

Exercise 4: Case Classes in Collections

Task: Given a list of books, write a function titlesByAuthor that takes an author's name and returns a list of titles by that author.