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:
Change the variable that you stored the GPA value in to begin with an underscore. So, if you previously used
self.valueit should now beself._value. This signifies to others that they should not directly manipulate this variable.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.Add a property named
gpafor the_get_gpa()and_set_gpa(value)functions using the syntax:property(_get_gpa, _set_gpa)in your class.Add a property named
letterthat calls the_get_letter()and_set_letter(value)functions. This time, specify the property using the@propertysyntax for the getter and@letter.settersyntax for the setter. (Please note that when you use the@propertysyntax, you need to change the name of your getter and setter functions to be "letter".)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 withstudent.gpa. All calls tostudent.set_gpa(xx)should be replaced withstudent.gpa = xxand then the same thing for calls to the letter getter and setter.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