OO Programming and Data Structures | CS 241

08 Teach : Team Activity - Getters / Setters

Overview

Practice using getters and setters by creating a class to model a time of day.

Instructions

Core Requirements

  1. Create a Time class that stores three ints for hours, minutes, and seconds. Prefix each variable with an underscore to indicate to others that these are private variables. Then, create getters and setters for each variable (e.g., get_hours(), set_hours(value), get_minutes(), ...).

    Assume that these values will store the data in military time (hours from 0-23). Put logic in the setters so that if the value if new value is higher than acceptable (hours > 23, minutes or seconds > 59) that it should set them to the maximum amount. Similarly, attempt to set values lower than 0, should set them to 0.

  2. Write a main function that creates a new time object. Have it prompt the user for hours, minutes, and seconds, use your setters to set each of these values, then use your getters to display each one. Please note that the input should be obtained in main and passed to a setter. Neither the getter or setter should prompt the user for anything.

    Test your getters and setters by entering values that are valid and also some that are invalid.

  3. Create a property for each component named hours, minutes, and seconds. Connect them to the getters and setters you have made using the hours = property(get_hours, set_hours) syntax.

    Change your main function so that it no longer calls any of the getters or setters directly, but instead uses the property. Then test it with several different values.

Stretch Challenges

After completing the above steps. Make sure that everyone on the entire team is to this point and understands the material. Then, if you have time, move onto the following stretch challenges.

  1. Create a new getter property called "hours_simple" that returns the hours in a simple, non military time, format of 1-12. Also create a new getter property called "period" that returns either "AM" or "PM" depending on the value of the hours.

    Create each of these getters using the @property decorator syntax. Then update main to additionally display these values.

  2. Change your time's internal representation to no longer use three separate integers for the hours, minutes, and seconds, but rather store only the number of seconds since midnight. You can use the mod operator (%) to help you determine the number of minutes and hours. Notice that the external use of your class (i.e., main) should not change.

    First, get the getters to work with this new implementation.

  3. Finally, get the setters to work with the new implementation.

Instructor's Solution

As a part of this team activity, you are expected to look over a solution from the instructor, to compare your approach to that one. One of the questions on the I-Learn submission will ask you to provide insights from this comparison.

Please DO NOT open the solution until you have worked through this activity as a team for the one hour period. At the end of the hour, if you are still struggling with some of the core requirements, you are welcome to view the instructor's solution and use it to help you complete your own code. Even if you use the instructor's code to help you, you are welcome to report that you finished the core requirements, if you code them up yourself.

After working with your team for the one hour activity, click here for the instructor's solution.

Submission

You do not need to submit your program (just make sure that everyone has a copy of it for their reference). Instead, answer the accompanying questions in the I-Learn quiz.