Object-oriented Programming and Data Structures | CS 241

08 Prepare : Checkpoint B - Properties

After completing (or while completing) the preparation material for this week, complete the following exercise.

Overview

This checkpoint is intended to help you practice properties. You will start with the getters and setters you created in checkpoint A and convert them to properties.

Instructions

Refer to Checkpoint 08A for instructions about the GPA class. This assignment will build on that one.

First, make a copy of your previous assignment (e.g., cp check08a.py check08b.py), then make the following changes:

  1. Change the variable that you stored the GPA value in to begin with an underscore. So, if you previously used self.value it should now be self._value. This signifies to others that they should not directly manipulate this variable.

  2. Change your getters and setters to begin with underscores, for example, get_gpa() should now be _get_gpa(). Repeat this for set_gpa, get_letter, and set_letter.

  3. Add a property named gpa for the _get_gpa() and _set_gpa(value) functions using the syntax: property(_get_gpa, _set_gpa) in your class.

  4. Add a property named letter that calls the _get_letter() and _set_letter(value) functions. This time, specify the property using the @property syntax for the getter and @letter.setter syntax for the setter. (Please note that when you use the @property syntax, you need to change the name of your getter and setter functions to be "letter".)

  5. Change your main function to use these properties instead of calling your functions. In other words, all calls to student.get_gpa() should be replaced with student.gpa. All calls to student.set_gpa(xx) should be replaced with student.gpa = xx and then the same thing for calls to the letter getter and setter.

  6. The output of the program should not change.

Sample Output

The following is an example of output for this program:


Initial values:
GPA: 0.00
Letter: F
Enter a new GPA: 3.75
After setting value:
GPA: 3.75
Letter: B
Enter a new letter: C
After setting letter:
GPA: 2.00
Letter: C

Testbed

An auto-grading testbed script is provided for you to help evaluate your program. This same testbed script will be used to grade your program. It is pass/fail, so your program must pass the testbed completely for you to receive credit for this assignment. You may run the testbed as many times as you like.

Recognize that because the output is the same as checkpoint A, you could pass the testbed without making these changes, however, you will not learn how to use properties if you do this. For this reason, You will not receive credit if you do not use properties as explained above.

Helpful commands


testBed cs241/check08b check08b.py
submit check08b.py