Answered step by step
Verified Expert Solution
Question
1 Approved Answer
class Student: def _ _ init _ _ ( self , name, student _ id ) : # Initialize attributes self. _ _ name =
class Student:
def initself name, studentid:
# Initialize attributes
self.name self.validatenamename
self.id self.validateidstudentid
self.coursesenrolled set
self.coursestaken
# Property for name attribute
@property
def nameself:
return self.name
# Setter for name attribute
@name.setter
def nameself newname:
self.name self.validatenamenewname
# Property for id attribute
@property
def idself:
return self.id
# Setter for id attribute
@idsetter
def idself newid:
self.id self.validateidnewid
# Method to get a copy of enrolled courses
def getcoursesenrolledself:
return self.coursesenrolled.copy
# Method to enroll in a course
def enrollincourseself course:
if not isinstancecourse Course:
raise TypeErrorCourse must be an instance of Course."
self.coursesenrolled.addcourse
# Method to record grade for a course
def recordgradeself course, grade:
if not isinstancecourse Course:
raise TypeErrorCourse must be an instance of Course."
if grade not in ABCDF:
raise ValueErrorGrade must be one of ABCDF
self.coursestakencourse grade
# Method to calculate GPA
def getgpaself:
totalcredits
totalpoints
for course, grade in self.coursestaken.items:
totalcredits course.credits
totalpoints A: B: C: D: F: grade course.credits
if totalcredits :
return
return totalpoints totalcredits
# Private method to validate name
def validatenameself name:
if not isinstancename str or lennamestrip:
raise ValueErrorInvalid name."
return name.strip
# Private method to validate ID
def validateidself studentid:
if not isinstancestudentid int:
raise TypeErrorStudent ID must be an integer."
if not studentid :
raise ValueErrorStudent ID must be in the range
return studentid
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started