Chapter 18 Exercise: Classes (Methods)

I completed the exercise, but I am unsure if my approach is efficient.

The exercise states that we need two methods to update the book’s property if the book needs to be discarded. I made a discard method for each with the different conditions (if over 5 years old for Manual and if over 100 checkouts for Novel).

But, I only used one update method to check the number of times a Novel has been checked out. I did not put any methods in the Book class. I called the methods and printed the objects.

The results are correct, but I’m concerned I still didn’t follow the instructions properly.

Thank you for your feedback.

It seems they phrased the exercise ambiguously or incorrectly:

each class needs two methods that update the book’s property if the book needs to be discarded.

It would’ve made more sense if they had said:

each class needs a method that updates the book’s property if the book needs to be discarded.

Also necessary is a method is to update the number of checkouts. But that method could probably go into the parent class (so, it may not need to be in each of the child classes).

Whether intentionally or not, LaunchCode is illustrating a real life situation:

  • Requirements are not always perfect, simple or clear.
  • They need to be verified and clarified.
  • They are open for interpretation.
  • Class design is more of an art than science.
  • There may be multiple possible answers.
  • “Reasonable people will disagree”.

Maybe I’ll revisit it and put the method in the parent class. Inheritance is a tricky concept for me to understand fully.

The ambiguity of coding is stressful on its own. But, without being in a classroom or team setting, it makes it more difficult for me being in limbo about the quality of my solutions. I can see how the exercises are preparing developers for work situations where requirements are not always clearly relayed to employees.