Object-oriented Programming and Data Structures | CS 241

08 Prepare : Checkpoint A - Getters and Setters

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

Overview

This checkpoint is intended to help you practice getter and setter functions. You will create a class to represent a GPA and then make getters and setters to get and set both the value and also the letter grade equivalent.

Instructions

Create a class to hold a student's GPA. This GPA should be in the range of 0.0 to 4.0. Your class should only store the numeric gpa value, NOT a letter. It will have getters and setters that work with the letter, but do so indirectly, as described below.

Create an __init__() function to set the initial value to 0.0.

Create a function for get_gpa() and set_gpa(value) that get and set the GPA stored in this class. If a GPA less that 0 is entered, you should not set the internal value to that, but instead, set it to 0. Similarly, if a value greater than 4 is entered, you should instead set the value to 4.0.

You should also create a function for get_letter() and set_letter(letter). But you should NOT store the letter internally. Instead, when someone calls the get_letter() function, you should determine the correct letter for the stored gpa, and return it. Similarly, when the set_letter() function is called, you should determine the appropriate gpa value and store that.

For simplicity, you need not worry about "+" or "-" grades, but instead, return the following letters depending on the range:

Similarly, the set_letter function should set the following values for each letter:

Main Function

To help test your GPA, include the following main function at the bottom:


def main():
    student = GPA()

    print("Initial values:")
    print("GPA: {:.2f}".format(student.get_gpa()))
    print("Letter: {}".format(student.get_letter()))

    value = float(input("Enter a new GPA: "))

    student.set_gpa(value)

    print("After setting value:")
    print("GPA: {:.2f}".format(student.get_gpa()))
    print("Letter: {}".format(student.get_letter()))

    letter = input("Enter a new letter: ")

    student.set_letter(letter)

    print("After setting letter:")
    print("GPA: {:.2f}".format(student.get_gpa()))
    print("Letter: {}".format(student.get_letter()))

if __name__ == "__main__":
    main()

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.

Helpful commands


testBed cs241/check08a check08a.py
submit check08a.py