Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Here's the entire class featuring __strJobStatus:
- # ==================================================================
- # Class Definition
- # ==================================================================
- import clsStudent
- class clsGraduate(clsStudent.clsStudent):
- # class vars
- intEmployedMales = int(0)
- intEmployedFemales = int(0)
- intGraduatedMales = int(0)
- intGraduatedFemales = int(0)
- intCurrentYear = int(2020)
- dblTotalGradGPA = float(0.0)
- intTotalMaleAge = float(0.0)
- intTotalFemaleAge = float(0.0)
- # ----------------------------------------------
- # Name: Class Constructor
- # Purpose: Initializes class objects and assigns default values upon creation
- # ----------------------------------------------
- def __init__(self, strFirstName, strLastName, strGender, dblGPA, intAge, intGradYear, strJobStatus):
- import clsStudent
- self.intGradYear = intGradYear
- self.strJobStatus = strJobStatus # must be "Y" or "N"
- self.strFirstName = strFirstName
- self.strLastName = strLastName
- self.strGender = strGender
- self.dblGPA = dblGPA
- self.intAge = intAge
- # note the number of male and female students with jobs
- if self.strJobStatus == "Y" and self.strGender == "male":
- clsGraduate.intEmployedMales += 1
- elif self.strJobStatus == "Y" and self.strGender == "female":
- clsGraduate.intEmployedFemales += 1
- # note number of male and female graduates
- # note age of male and female graduates
- if self.intGradYear < clsGraduate.intCurrentYear and self.strGender == "male":
- clsGraduate.intGraduatedMales += 1
- clsGraduate.dblTotalGradGPA += self.dblGPA
- clsGraduate.intTotalMaleAge += self.intAge
- elif self.intGradYear < clsGraduate.intCurrentYear and self.strGender == "female":
- clsGraduate.intGraduatedFemales += 1
- clsGraduate.dblTotalGradGPA += self.dblGPA
- clsGraduate.intTotalFemaleAge += self.intAge
- # ----------------------------------------------
- # Name: Graduation Year and Job Status Getters
- # Purpose: Returns value of Graduation Year and Job Status attributes
- # ----------------------------------------------
- @property
- def intGradYear(self):
- return self.__intGradYear
- @property
- def strJobStatus(self):
- return self.__strJobStatus
- # ----------------------------------------------
- # Name: Graduation Year and Job Status Setters
- # Purpose: Assign value to Graduation Year and Job Status attributes.
- # Validate that Grad Year exists, is numeric.
- # Validate that Job Status is either Y or N.
- # ----------------------------------------------
- @intGradYear.setter
- def intGradYear(self, intGradYear):
- if intGradYear == "":
- raise Exception("Error. Graduation year cannot be empty string. Please enter a value for graduation year.")
- elif type(intGradYear) != int:
- raise Exception("Error. Graduation year contains non-numeric characters. Please enter numbers only.")
- @strJobStatus.setter
- def strJobStatus(self, strJobStatus):
- if strJobStatus == "":
- raise Exception("Error. Job status cannot be empty string. Please enter a value for job status.")
- elif isinstance(strJobStatus, str) == False:
- raise Exception("Error. Invalid value for job status. Job status must be either 'Y' or 'N'.")
- # ----------------------------------------------
- # Name: FindTotalMaleStudents
- # Purpose: Counts all student objects with "male" gender attribute who are employed
- # ----------------------------------------------
- def FindTotalMaleStudents(lstStudents):
- # declare vars
- intEmployedMales = int(0)
- # calculate results
- intEmployedMales = clsStudent.intMalesEmployed
- # display results
- return intEmployedMales
- # ----------------------------------------------
- # Name: FindTotalFemaleStudents
- # Purpose: Counts all student objects with "female" gender attribute who are employed
- # ----------------------------------------------
- def FindTotalFemaleStudents(lstStudents):
- # declare vars
- intEmployedFemales = int(0)
- # calculate results
- intEmployedFemales = clsStudent.intEmployedFemales
- # return results
- return intEmployedFemales
- # ----------------------------------------------
- # Name: GetAverageGradGPA
- # Purpose: Calculates average GPA of all graduated students
- # ----------------------------------------------
- def GetAverageGradGPA():
- # declare var
- dblAverageGradGPA = float(0.0)
- # calculate results
- dblAverageGradGPA = clsGraduate.dblTotalGradGPA / (clsGraduate.intGraduatedMales + clsGraduate.intGraduatedFemales)
- # return results
- return dblAverageGradGPA
- # ----------------------------------------------
- # Name: GetAverageMaleGradAge
- # Purpose: Calculates average age of all graduated male students
- # ----------------------------------------------
- def GetAverageMaleGradAge():
- # declare var
- dblAverageMaleGradAge = float(0.0)
- # calculate results
- dblAverageMaleGradAge = clsGraduate.intTotalMaleAge / clsGraduate.intGraduatedMales
- # return results
- return dblAverageMaleGradAge
- # ----------------------------------------------
- # Name: GetAverageFemaleGradAge
- # Purpose: Calculates average age of all graduated female students
- # ----------------------------------------------
- def GetAverageFemaleGradAge():
- # declare var
- dblAverageFemaleGradAge = float(0.0)
- # calculate results
- dblAverageFemaleGradAge = clsGraduate.intTotalFemaleAge / clsGraduate.intGraduatedFemales
- # return results
- return dblAverageFemaleGradAge
- # ----------------------------------------------
- # Name: GetEmployedMaleGrads
- # Purpose: Calculates total of male grads who are employed
- # ----------------------------------------------
- def GetEmployedMaleGrads():
- # declare var
- intEmployedMaleGrads = float(0.0)
- # calculate results
- intEmployedMaleGrads = clsGraduate.intEmployedMales
- # return results
- return intEmployedMaleGrads
- # ----------------------------------------------
- # Name: GetEmployedFemaleGrads
- # Purpose: Calculates total of female grads who are employed
- # ----------------------------------------------
- def GetEmployedFemaleGrads():
- # declare var
- intEmployedFemaleGrads = float(0.0)
- # calculate results
- intEmployedFemaleGrads = clsGraduate.intEmployedFemales
- # return results
- return intEmployedFemaleGrads
- # And this is the entire parent class:
- # ==================================================================
- # Class Definition
- # ==================================================================
- import clsPerson
- class clsStudent(clsPerson.clsPerson):
- # class attributes
- intStudentCount = int(0)
- intMaleCount = int(0)
- intFemaleCount = int(0)
- dblGPATotal = int(0)
- intFemaleAgeTotal = int(0)
- intMaleAgeTotal = int(0)
- # ----------------------------------------------
- # Name: Class Constructor
- # Purpose: Instantiates and assigns values to class object upon creation
- # ----------------------------------------------
- def __init__(self, strFirstName, strLastName, strGender, dblGPA, intAge):
- import clsPerson
- self.firstname = firstname
- self.lastname = lastname
- self.strGender = strGender
- self.dblGPA = dblGPA
- self.intAge = intAge
- clsStudent.stuStudentCount += 1
- if (gender == 'Male'):
- clsStudent.stuMaleCount += 1
- clsStudent.stuMaleAgeTotal += age
- else:
- clsStudent.stuFemaleCount += 1
- clsStudent.stuFemaleAgeTotal += age
- clsStudent.stuGPATotal += GPA
- # ----------------------------------------------
- # Name: GPA Getter
- # Purpose: Returns value of GPA attribute
- # ----------------------------------------------
- @property
- def dblGPA(self):
- return self.__dblGPA
- # ----------------------------------------------
- # Name: GPA Setter
- # Purpose: Sets value for GPA attribute, validate that input exists, is numeric, is in valid range
- # ----------------------------------------------
- @dblGPA.setter
- def dblGPA(self, dblGPA):
- print(type(dblGPA))
- if dblGPA == "":
- raise Exception("Error. GPA cannot be empty string. Please enter a value for GPA.")
- elif type(dblGPA) != float:
- raise Exception("Error. GPA contains non-numeric characters. Please enter numbers only.")
- elif dblGPA < 0 or dblGPA > 4:
- raise Exception("Error. GPA cannot be less than zero or less than four.")
- # ----------------------------------------------
- # Name: FindTotalMaleStudents
- # Purpose: Counts all student objects with "male" gender attribute
- # ----------------------------------------------
- def FindTotalMaleStudents(lstStudents):
- # declare vars
- intTotalMales = int(0)
- # calculate results
- for Student in lstStudents:
- if Student.strGender.strip() == "male":
- intTotalMales += 1
- # return results
- return intTotalMales
- # ----------------------------------------------
- # Name: FindTotalFemaleStudents
- # Purpose: Counts all student objects with "female" gender attribute
- # ----------------------------------------------
- def FindTotalFemaleStudents(lstStudents):
- # declare vars
- intTotalFemales = int(0)
- # calculate results
- for Student in lstStudents:
- if Student.strGender.strip() == "female":
- intTotalFemales += 1
- # return results
- return intTotalFemales
- # ----------------------------------------------
- # Name: AverageGPA
- # Purpose: Adds age attribute of all student objects with "male" gender
- # attribute
- # ----------------------------------------------
- def AverageGPA(lstStudents):
- # declare vars
- dblAverageGPA = float(0.0)
- # calculate results
- for Student in lstStudents:
- dblAverageGPA += Student.dblGPA
- dblAverageGPA = dblAverageGPA / len(lstStudents)
- # return results
- return dblAverageGPA
- # ----------------------------------------------
- # Name: FindAverageAgeMaleStudents
- # Purpose: Adds age attribute of all student objects with "male" gender
- # attribute
- # ----------------------------------------------
- def FindTotalAgeOfMaleStudents(lstStudents):
- # declare vars
- dblAverageMaleAge = float(0.0)
- intTotalMales = int(0)
- # calculate results
- for Student in lstStudents:
- if Student.strGender.strip() == "male":
- dblAverageMaleAge += Student.intAge
- intTotalMales += 1
- dbleAverageMaleAge = dblAverageMaleAge / intTotalMales
- # return results
- return dbleAverageMaleAge
- # ----------------------------------------------
- # Name: FindAverageAgeFemaleStudents
- # Purpose: Adds age attribute of all student objects with "female" gender
- # attribute
- # ----------------------------------------------
- def FindTotalAgeOfFemaleStudents(lstStudents):
- # declare vars
- dblAverageFemaleAge = float(0.0)
- intTotalFemales = int(0)
- # calculate results
- for Student in lstStudents:
- if Student.strGender == "female":
- dblAverageFemaleAge += Student.intAge
- intTotalFemales += 1
- dblAverageFemaleAge = dblAverageFemaleAge / intTotalFemales
- # return results
- return dblAverageFemaleAge
- # And here is the parent class of the previous parent class:
- # ==================================================================
- # Class Definition
- # ==================================================================
- class clsPerson(): # Check how to implement re: params and class attributes
- # --------------------------------------------------------------
- # Class attributes with default values
- # --------------------------------------------------------------
- strFirstName = str("")
- strLastName = str("")
- intAge = int(0)
- strGender = str("")
- # --------------------------------------------------------------
- # Method definitions
- # --------------------------------------------------------------
- # ----------------------------------------------
- # Name: Class Getters
- # Purpose: Returns value of object property
- # ----------------------------------------------
- @property
- def strFirstName(self):
- return self.__strFirstName
- @property
- def strLastName(self):
- return self.__strLastName
- @property
- def strGender(self):
- return self.__strGender
- @property
- def intAge(self):
- return self.__intAge
- # ----------------------------------------------
- # Name: Class Setters
- # Purpose: Assigns value to object properties, validates data received
- # ----------------------------------------------
- @strFirstName.setter
- def strFirstName(self, strFirstName):
- if strFirstName == "":
- raise Exception("Error. First name cannot be empty string. Please enter a value for first name.")
- @strLastName.setter
- def strLastName(self, strLastName):
- if strLastName == "":
- raise Exception("Error. Last name cannot be empty string. Please enter a value for last name.")
- @strGender.setter
- def strGender(self,strGender):
- if strGender == "":
- raise Exception("Error. Gender cannot be empty string. Please enter a value for gender.")
- @intAge.setter
- def intAge(self, intAge):
- if intAge == "":
- raise Exception("Error. Age cannot be empty string. Please enter a value for age.")
- elif type(intAge) != int:
- raise Exception("Error. Age contains non-numeric characters. Please enter numbers only.")
- elif intAge < 0:
- raise Exception("Error. Age cannot be less than zero.")
- # ----------------------------------------------
- # Name: Class Constructor
- # Purpose: Initializes class objects
- # ----------------------------------------------
- def __init__(self, fname, lname, intAge, strGender):
- self.firstname = fname
- self.lastname = lname
- self.intAge = intAge
- self.strGender = strGender
- # ----------------------------------------------
- # Name: TotalNumPersons
- # Purpose: Counts all person objects and returns total
- # ----------------------------------------------
- def TotalNumPersons(lstStudents):
- # declare vars
- intTotalPersons = int(0)
- # calculate total number of students
- intTotalPersons = len(lstStudents)
- #return results
- return intTotalPersons
- # And just for completeness' sake, here's the entire module where the code actually gets called:
- import clsPerson
- import clsStudent
- import clsGraduate
- # ==================================================================
- # Main Module
- # ==================================================================
- # declare variables
- intTotalMaleStudents = int(0)
- intTotalFemaleStudents = int(0)
- intStudentCount = int(0)
- dblAverageGPA = float(0.0)
- dblAverageGradGPA = float(0.0)
- dblAverageMaleAge = float(0.0)
- dblAverageFemaleAge = float(0.0)
- dblAverageMaleGradAge = float(0.0)
- dblAverageFemaleGradAge = float(0.0)
- intIndex = int(0)
- # --------------------------------------------------------------
- # Create student objects
- # --------------------------------------------------------------
- # NOTE: instructions just said "collect 5 students," but the attributes
- # required that they be instantiated as objects of clsGraduate,
- # since clsStudent doesn't have intGradYear or strJobStatus
- # create first object from the Student class"
- Student1 = clsGraduate.clsGraduate("Bill", "Zara", "male", 3.25, 20, 2018, "Y")
- # create second object from the Student class"
- Student2 = clsGraduate.clsGraduate("Betty", "Tara", "female", 2.25, 25, 2019, "N")
- # create third object from the Student class"
- Student3 = clsGraduate.clsGraduate("Sydney", "Nye", "female", 3.75, 30, 2017, "Y")
- # create forth object from the Student class"
- Student4 = clsGraduate.clsGraduate("Jake", "Leedom", "male", 2.75, 35, 2016, "N")
- # create fifth object from the Student class"
- Student5 = clsGraduate.clsGraduate("Alex", "Jacobs", "male", 3.55, 21, 2018, "Y")
- # --------------------------------------------------------------
- # Calculate results
- # --------------------------------------------------------------
- # calculate number of graduated students
- intGraduatedStudents = clsGraduate.clsGraduate.intGraduatedMales + clsGraduate.clsGraduate.intGraduatedFemales
- # calculate average GPA of all graduated students
- dblAverageGradGPA = clsGraduate.clsGraduate.GetAverageGradGPA()
- # calculate average age of graduated male students
- dblAverageMaleGradAge = clsGraduate.clsGraduate.GetAverageMaleGradAge()
- # calculate average age of graduated female students
- dblAverageFemaleGradAge = clsGraduate.clsGraduate.GetAverageFemaleGradAge()
- # calculate total number of graduated male students who are employed
- intEmployedMaleGrads = clsGraduate.clsGraduate.GetEmployedMaleGrads()
- # calculate total number of graduated female students who are employed
- intEmployedMaleFemaleGrads = clsGraduate.clsGraduate.GetEmployedFemaleGrads()
- # --------------------------------------------------------------
- # Display results
- # --------------------------------------------------------------
- print("Number of graduated students: ", intGraduatedStudents)
- print("Average GPA of all graduated students: ", dblAverageGradGPA)
- print("Average age of graduated male students: ", dblAverageMaleGradAge)
- print("Average age of graduated female students: ", dblAverageFemaleGradAge)
- print("Total number of graduated male students who are employed: ", intEmployedMaleGrads)
- print("Total number of graduated female students who are employed: ", intEmployedMaleFemaleGrads)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement