CIS-3030 Homework #3: Case Classes

Due: Friday, February 15, 2019

Read the whole of Chapter 15 in PiS.

  1. Define a case class named Person that holds information about a person's name, gender (use the character 'M' or 'F'... for purposes of this assignment we'll use a simple model of gender identity), and a list of that person's children. If a person has no children, the list should be empty (but not null). Notice that you can define a case class in terms of itself.

  2. Assume John (a male) has three children, Eloise (female), Terrance (male), and Chris (female). Assume that Eloise has a son named Thomas. Define a val for John that includes all the information above. You could define the various children separately, but can you also do it all at once?

  3. The following method computes how many male children a person has. The method does not consider grandchildren, etc. Implement it. If you call this method on John you should get 1.

            def maleChildren(p: Person): Int = ???
  4. The following method generalizes the one above by computing how many male descendants a person has (including grandchildren, etc. Implement it. If you call this method on John you should get 2 (because of Eloise's son).

            def maleDescendants(p: Person): Int = ???
  5. Write more general methods based on the two above that work for either males or females by accepting the gender as a parameter rather than having it hard coded.

            def countChildren(p: Person, gender: Char): Int = ???
            def countDescendants(p: Person, gender: Char): Int = ???

    The following should be true: countChildren(John, 'F') == 2 && countDescendants(John, 'F') == 2

Submit your file Hw03.scala (put your name in comments).

Last Revised: 2019-02-06
© Copyright 2019 by Peter C. Chapin <>