Exercises for Lesson 4

Back to Lesson 4

Exercise 1: Checklist

Your goal is to write a class named Checklist, which allows us to keep track of a bunch of tasks we need to complete.

The Checklist class should have the following functionality:

  • Checklist() constructor: takes no actual parameters, but initializes necessary data structures
  • add(s): adds a new task to complete to the checklist, assuming the task is not complete; takes in a String and returns nothing
  • getStatus(s): looks up whether the task specified by the given string is completed; takes in a String and returns a Boolean indicating the status or null if the task is not found
  • setStatus(s, status): updates the task specified by the given string to have the given completion status; takes in a String and a Boolean and returns nothing
  • getSize(): determines the number of tasks; returns that number (an Int)
  • getNumIncomplete(): determines the number of tasks that aren’t yet complete; returns that number (an Int)

Part 1a: starting your class definition

You should create a new file named Checklist.kt and put your class definition there. It should start like this:

class Checklist {
    // TODO: initiailize any necessary data structures

    // TODO: add methods
}

Try to start out just adding empty methods that have the right signature and return garbage values. For example, you could write the following for add and getStatus:

    fun add(s: String) {
        // TODO: implement
    }

    fun getStatus(s: String): Boolean? { // returns true/false or null
        // TODO: implement
    }

You should be able to compile your code now before actually completing the methods. Here’s a simple main function you can use to try the Checklist functions out:

fun main() {
    val checklist = Checklist()

    checklist.add("Finish A2 Question 1")
    checklist.add("Read A2 Question 2")

    println(checklist.getStatus("Finish A2 Question 1"))
    println(checklist.getStatus("read a2 Question 2"))

    checklist.setStatus("Finish A2 Question 1", true)
    println(checklist.getStatus("Finish A2 Question 1"))

    println("Size: ${checklist.getSize()}")
    checklist.add("Prep for Wednesday's class")
    println("Size: ${checklist.getSize()}")

    println("Num left to complete: ${checklist.getNumIncomplete()}")
}

Again, you can use kotlinc and kotlin to compile and execute your code.

Part 1b: filling in some methods

Now try to write the add, getStatus, and setStatus methods.

Think carefully about how to handle the fact that getStatus may need to return null if the string isn’t found. How does this impact its signature and implementation?

Part 1c: finishing it up

Finish your class by writing getSize and getNumIncomplete. Here is what you should see when you run the main function given above:

false
null
true
Size: 2
Size: 3
Num left to complete: 2

Part 1d: food for thought

If you have extra time, try to answer some questions for yourself:

  • Why does the second line say null?
  • What would you expect if you printed the result from calling checklist.setStatus?
  • How would you change your class definition to prevent duplicate tasks from being added to the checklist?
  • Do your implementations of getStatus and setStatus have to look at every task in the checklist? If they do right now, how could you speed them up?

Back to Lesson 4