Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

class Student: def _ _ init _ _ ( self , name, student _ id ) : # Initialize attributes self. _ _ name =

class Student:
def __init__(self, name, student_id):
# Initialize attributes
self.__name = self.__validate_name(name)
self.__id = self.__validate_id(student_id)
self.__courses_enrolled = set()
self.__courses_taken ={}
# Property for name attribute
@property
def name(self):
return self.__name
# Setter for name attribute
@name.setter
def name(self, new_name):
self.__name = self.__validate_name(new_name)
# Property for id attribute
@property
def id(self):
return self.__id
# Setter for id attribute
@id.setter
def id(self, new_id):
self.__id = self.__validate_id(new_id)
# Method to get a copy of enrolled courses
def get_courses_enrolled(self):
return self.__courses_enrolled.copy()
# Method to enroll in a course
def enroll_in_course(self, course):
if not isinstance(course, Course):
raise TypeError("Course must be an instance of Course.")
self.__courses_enrolled.add(course)
# Method to record grade for a course
def record_grade(self, course, grade):
if not isinstance(course, Course):
raise TypeError("Course must be an instance of Course.")
if grade not in ('A','B','C','D','F'):
raise ValueError("Grade must be one of 'A','B','C','D','F'.")
self.__courses_taken[course]= grade
# Method to calculate GPA
def get_gpa(self):
total_credits =0
total_points =0
for course, grade in self.__courses_taken.items():
total_credits += course.credits
total_points +={'A': 4.0,'B': 3.0,'C': 2.0,'D': 1.0,'F': 0.0}[grade]* course.credits
if total_credits ==0:
return 0
return total_points / total_credits
# Private method to validate name
def __validate_name(self, name):
if not isinstance(name, str) or len(name.strip())==0:
raise ValueError("Invalid name.")
return name.strip()
# Private method to validate ID
def __validate_id(self, student_id):
if not isinstance(student_id, int):
raise TypeError("Student ID must be an integer.")
if not (1<= student_id <=9999999):
raise ValueError("Student ID must be in the range [1-9999999].")
return student_id

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle 10g SQL

Authors: Joan Casteel, Lannes Morris Murphy

1st Edition

141883629X, 9781418836290

More Books

Students also viewed these Databases questions