Advertisement
TheBlackPopeSJ

Untitled

Apr 6th, 2020
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 18.99 KB | None | 0 0
  1. # Here's the entire class featuring __strJobStatus:
  2.  
  3.  
  4. # ==================================================================
  5. # Class Definition
  6. # ==================================================================
  7.  
  8. import clsStudent
  9.  
  10. class clsGraduate(clsStudent.clsStudent):
  11.  
  12.     # class vars
  13.     intEmployedMales = int(0)
  14.     intEmployedFemales = int(0)
  15.     intGraduatedMales = int(0)
  16.     intGraduatedFemales = int(0)
  17.     intCurrentYear = int(2020)
  18.     dblTotalGradGPA = float(0.0)
  19.     intTotalMaleAge = float(0.0)
  20.     intTotalFemaleAge = float(0.0)
  21.  
  22.     # ----------------------------------------------
  23.     # Name:         Class Constructor
  24.     # Purpose:      Initializes class objects and assigns default values upon creation
  25.     # ----------------------------------------------
  26.     def __init__(self, strFirstName, strLastName, strGender, dblGPA, intAge, intGradYear, strJobStatus):
  27.         import clsStudent
  28.         self.intGradYear = intGradYear
  29.         self.strJobStatus = strJobStatus # must be "Y" or "N"
  30.         self.strFirstName = strFirstName
  31.         self.strLastName = strLastName
  32.         self.strGender = strGender
  33.         self.dblGPA = dblGPA
  34.         self.intAge = intAge
  35.  
  36.         # note the number of male and female students with jobs
  37.         if self.strJobStatus == "Y" and self.strGender == "male":
  38.             clsGraduate.intEmployedMales += 1
  39.         elif self.strJobStatus == "Y" and self.strGender == "female":
  40.             clsGraduate.intEmployedFemales += 1
  41.  
  42.         # note number of male and female graduates
  43.         # note age of male and female graduates
  44.         if self.intGradYear < clsGraduate.intCurrentYear and self.strGender == "male":
  45.             clsGraduate.intGraduatedMales += 1
  46.             clsGraduate.dblTotalGradGPA += self.dblGPA
  47.             clsGraduate.intTotalMaleAge += self.intAge
  48.         elif self.intGradYear < clsGraduate.intCurrentYear and self.strGender == "female":
  49.             clsGraduate.intGraduatedFemales += 1
  50.             clsGraduate.dblTotalGradGPA += self.dblGPA
  51.             clsGraduate.intTotalFemaleAge += self.intAge
  52.  
  53.        
  54.  
  55.     # ----------------------------------------------
  56.     # Name:         Graduation Year and Job Status Getters
  57.     # Purpose:      Returns value of Graduation Year and Job Status attributes
  58.     # ----------------------------------------------
  59.     @property
  60.     def intGradYear(self):
  61.         return self.__intGradYear
  62.  
  63.     @property
  64.     def strJobStatus(self):
  65.         return self.__strJobStatus
  66.  
  67.  
  68.  
  69.     # ----------------------------------------------
  70.     # Name:         Graduation Year and Job Status Setters
  71.     # Purpose:      Assign value to Graduation Year and Job Status attributes.
  72.     #               Validate that Grad Year exists, is numeric.
  73.     #               Validate that Job Status is either Y or N.
  74.     # ----------------------------------------------
  75.     @intGradYear.setter
  76.     def intGradYear(self, intGradYear):
  77.         if intGradYear == "":
  78.             raise Exception("Error. Graduation year cannot be empty string. Please enter a value for graduation year.")
  79.         elif type(intGradYear) != int:
  80.             raise Exception("Error. Graduation year contains non-numeric characters. Please enter numbers only.")
  81.  
  82.  
  83.  
  84.     @strJobStatus.setter
  85.     def strJobStatus(self, strJobStatus):
  86.         if strJobStatus == "":
  87.             raise Exception("Error. Job status cannot be empty string. Please enter a value for job status.")
  88.         elif isinstance(strJobStatus, str) == False:
  89.             raise Exception("Error. Invalid value for job status. Job status must be either 'Y' or 'N'.")
  90.  
  91.  
  92.  
  93.     # ----------------------------------------------
  94.     # Name:         FindTotalMaleStudents
  95.     # Purpose:      Counts all student objects with "male" gender attribute who are employed
  96.     # ----------------------------------------------
  97.     def FindTotalMaleStudents(lstStudents):
  98.  
  99.         # declare vars
  100.         intEmployedMales = int(0)
  101.  
  102.         # calculate results
  103.         intEmployedMales = clsStudent.intMalesEmployed
  104.  
  105.         # display results
  106.         return intEmployedMales
  107.  
  108.  
  109.  
  110.     # ----------------------------------------------
  111.     # Name:         FindTotalFemaleStudents
  112.     # Purpose:      Counts all student objects with "female" gender attribute who are employed
  113.     # ----------------------------------------------
  114.     def FindTotalFemaleStudents(lstStudents):
  115.  
  116.         # declare vars
  117.         intEmployedFemales = int(0)
  118.  
  119.         # calculate results
  120.         intEmployedFemales = clsStudent.intEmployedFemales
  121.  
  122.         # return results
  123.         return intEmployedFemales
  124.  
  125.  
  126.     # ----------------------------------------------
  127.     # Name:         GetAverageGradGPA
  128.     # Purpose:      Calculates average GPA of all graduated students
  129.     # ----------------------------------------------
  130.     def GetAverageGradGPA():
  131.  
  132.         # declare var
  133.         dblAverageGradGPA = float(0.0)
  134.  
  135.         # calculate results
  136.         dblAverageGradGPA = clsGraduate.dblTotalGradGPA / (clsGraduate.intGraduatedMales + clsGraduate.intGraduatedFemales)
  137.  
  138.         # return results
  139.         return dblAverageGradGPA
  140.  
  141.  
  142.  
  143.     # ----------------------------------------------
  144.     # Name:         GetAverageMaleGradAge
  145.     # Purpose:      Calculates average age of all graduated male students
  146.     # ----------------------------------------------
  147.     def GetAverageMaleGradAge():
  148.  
  149.         # declare var
  150.         dblAverageMaleGradAge = float(0.0)
  151.  
  152.         # calculate results
  153.         dblAverageMaleGradAge = clsGraduate.intTotalMaleAge / clsGraduate.intGraduatedMales
  154.  
  155.         # return results
  156.         return dblAverageMaleGradAge
  157.  
  158.  
  159.  
  160.     # ----------------------------------------------
  161.     # Name:         GetAverageFemaleGradAge
  162.     # Purpose:      Calculates average age of all graduated female students
  163.     # ----------------------------------------------
  164.     def GetAverageFemaleGradAge():
  165.  
  166.         # declare var
  167.         dblAverageFemaleGradAge = float(0.0)
  168.  
  169.         # calculate results
  170.         dblAverageFemaleGradAge = clsGraduate.intTotalFemaleAge / clsGraduate.intGraduatedFemales
  171.  
  172.         # return results
  173.         return dblAverageFemaleGradAge
  174.  
  175.  
  176.  
  177.     # ----------------------------------------------
  178.     # Name:         GetEmployedMaleGrads
  179.     # Purpose:      Calculates total of male grads who are employed
  180.     # ----------------------------------------------
  181.     def GetEmployedMaleGrads():
  182.  
  183.         # declare var
  184.         intEmployedMaleGrads = float(0.0)
  185.  
  186.         # calculate results
  187.         intEmployedMaleGrads = clsGraduate.intEmployedMales
  188.  
  189.         # return results
  190.         return intEmployedMaleGrads
  191.  
  192.  
  193.  
  194.     # ----------------------------------------------
  195.     # Name:         GetEmployedFemaleGrads
  196.     # Purpose:      Calculates total of female grads who are employed
  197.     # ----------------------------------------------
  198.     def GetEmployedFemaleGrads():
  199.  
  200.         # declare var
  201.         intEmployedFemaleGrads = float(0.0)
  202.  
  203.         # calculate results
  204.         intEmployedFemaleGrads = clsGraduate.intEmployedFemales
  205.  
  206.         # return results
  207.         return intEmployedFemaleGrads
  208.  
  209.  
  210.  
  211.  
  212.  
  213.  
  214.  
  215. # And this is the entire parent class:
  216. # ==================================================================
  217. # Class Definition
  218. # ==================================================================
  219.  
  220. import clsPerson
  221.  
  222. class clsStudent(clsPerson.clsPerson):
  223.  
  224.     # class attributes
  225.     intStudentCount = int(0)
  226.     intMaleCount = int(0)
  227.     intFemaleCount = int(0)
  228.     dblGPATotal = int(0)
  229.     intFemaleAgeTotal = int(0)
  230.     intMaleAgeTotal = int(0)
  231.  
  232.  
  233.  
  234.     # ----------------------------------------------
  235.     # Name:         Class Constructor
  236.     # Purpose:      Instantiates and assigns values to class object upon creation
  237.     # ----------------------------------------------
  238.     def __init__(self, strFirstName, strLastName, strGender, dblGPA, intAge):
  239.         import clsPerson
  240.         self.firstname = firstname
  241.         self.lastname = lastname
  242.         self.strGender = strGender
  243.         self.dblGPA = dblGPA
  244.         self.intAge = intAge
  245.         clsStudent.stuStudentCount += 1
  246.  
  247.         if (gender == 'Male'):
  248.             clsStudent.stuMaleCount += 1
  249.             clsStudent.stuMaleAgeTotal += age
  250.         else:
  251.             clsStudent.stuFemaleCount += 1
  252.             clsStudent.stuFemaleAgeTotal += age
  253.             clsStudent.stuGPATotal += GPA
  254.  
  255.     # ----------------------------------------------
  256.     # Name:         GPA Getter
  257.     # Purpose:      Returns value of GPA attribute
  258.     # ----------------------------------------------
  259.     @property
  260.     def dblGPA(self):
  261.         return self.__dblGPA
  262.  
  263.  
  264.  
  265.     # ----------------------------------------------
  266.     # Name:         GPA Setter
  267.     # Purpose:      Sets value for GPA attribute, validate that input exists, is numeric, is in valid range
  268.     # ----------------------------------------------
  269.     @dblGPA.setter
  270.     def dblGPA(self, dblGPA):
  271.         print(type(dblGPA))
  272.         if dblGPA == "":
  273.             raise Exception("Error. GPA cannot be empty string. Please enter a value for GPA.")
  274.         elif type(dblGPA) != float:
  275.             raise Exception("Error. GPA contains non-numeric characters. Please enter numbers only.")
  276.         elif dblGPA < 0 or dblGPA > 4:
  277.             raise Exception("Error. GPA cannot be less than zero or less than four.")
  278.  
  279.  
  280.  
  281.     # ----------------------------------------------
  282.     # Name: FindTotalMaleStudents
  283.     # Purpose: Counts all student objects with "male" gender attribute
  284.     # ----------------------------------------------
  285.     def FindTotalMaleStudents(lstStudents):
  286.  
  287.         # declare vars
  288.         intTotalMales = int(0)
  289.  
  290.         # calculate results
  291.         for Student in lstStudents:
  292.             if Student.strGender.strip() == "male":
  293.                 intTotalMales += 1
  294.  
  295.         # return results
  296.         return intTotalMales
  297.  
  298.  
  299.  
  300.     # ----------------------------------------------
  301.     # Name: FindTotalFemaleStudents
  302.     # Purpose: Counts all student objects with "female" gender attribute
  303.     # ----------------------------------------------
  304.     def FindTotalFemaleStudents(lstStudents):
  305.  
  306.         # declare vars
  307.         intTotalFemales = int(0)
  308.  
  309.         # calculate results
  310.         for Student in lstStudents:
  311.             if Student.strGender.strip() == "female":
  312.                 intTotalFemales += 1
  313.  
  314.         # return results
  315.         return intTotalFemales
  316.  
  317.  
  318.  
  319.     # ----------------------------------------------
  320.     # Name: AverageGPA
  321.     # Purpose: Adds age attribute of all student objects with "male" gender
  322.     #          attribute
  323.     # ----------------------------------------------
  324.     def AverageGPA(lstStudents):
  325.  
  326.         # declare vars
  327.         dblAverageGPA = float(0.0)
  328.  
  329.         # calculate results
  330.         for Student in lstStudents:
  331.             dblAverageGPA += Student.dblGPA
  332.  
  333.         dblAverageGPA = dblAverageGPA / len(lstStudents)
  334.  
  335.         # return results
  336.         return dblAverageGPA
  337.        
  338.  
  339.  
  340.     # ----------------------------------------------
  341.     # Name: FindAverageAgeMaleStudents
  342.     # Purpose: Adds age attribute of all student objects with "male" gender
  343.     #          attribute
  344.     # ----------------------------------------------
  345.     def FindTotalAgeOfMaleStudents(lstStudents):
  346.  
  347.         # declare vars
  348.         dblAverageMaleAge = float(0.0)
  349.         intTotalMales = int(0)
  350.  
  351.         # calculate results
  352.         for Student in lstStudents:
  353.             if Student.strGender.strip() == "male":
  354.                 dblAverageMaleAge += Student.intAge
  355.                 intTotalMales += 1
  356.  
  357.         dbleAverageMaleAge = dblAverageMaleAge / intTotalMales
  358.  
  359.         # return results
  360.         return dbleAverageMaleAge
  361.  
  362.  
  363.  
  364.     # ----------------------------------------------
  365.     # Name: FindAverageAgeFemaleStudents
  366.     # Purpose: Adds age attribute of all student objects with "female" gender
  367.     #          attribute
  368.     # ----------------------------------------------
  369.     def FindTotalAgeOfFemaleStudents(lstStudents):
  370.  
  371.         # declare vars
  372.         dblAverageFemaleAge = float(0.0)
  373.         intTotalFemales = int(0)
  374.  
  375.         # calculate results
  376.         for Student in lstStudents:
  377.             if Student.strGender == "female":
  378.                 dblAverageFemaleAge += Student.intAge
  379.                 intTotalFemales += 1
  380.  
  381.         dblAverageFemaleAge = dblAverageFemaleAge / intTotalFemales
  382.  
  383.         # return results
  384.         return dblAverageFemaleAge
  385.  
  386.  
  387.  
  388.  
  389.  
  390.  
  391.  
  392.  
  393. # And here is the parent class of the previous parent class:
  394. # ==================================================================
  395. # Class Definition
  396. # ==================================================================
  397. class clsPerson(): # Check how to implement re: params and class attributes
  398.  
  399.     # --------------------------------------------------------------
  400.     # Class attributes with default values
  401.     # --------------------------------------------------------------
  402.     strFirstName = str("")
  403.     strLastName = str("")
  404.     intAge = int(0)
  405.     strGender = str("")
  406.  
  407.     # --------------------------------------------------------------
  408.     # Method definitions
  409.     # --------------------------------------------------------------
  410.  
  411.  
  412.  
  413.     # ----------------------------------------------
  414.     # Name: Class Getters
  415.     # Purpose: Returns value of object property
  416.     # ----------------------------------------------
  417.     @property
  418.     def strFirstName(self):
  419.         return self.__strFirstName
  420.  
  421.     @property
  422.     def strLastName(self):
  423.         return self.__strLastName
  424.  
  425.     @property
  426.     def strGender(self):
  427.         return self.__strGender
  428.  
  429.     @property
  430.     def intAge(self):
  431.         return self.__intAge
  432.  
  433.  
  434.  
  435.     # ----------------------------------------------
  436.     # Name: Class Setters
  437.     # Purpose: Assigns value to object properties, validates data received
  438.     # ----------------------------------------------
  439.     @strFirstName.setter
  440.     def strFirstName(self, strFirstName):
  441.         if strFirstName == "":
  442.             raise Exception("Error. First name cannot be empty string. Please enter a value for first name.")
  443.  
  444.  
  445.  
  446.     @strLastName.setter
  447.     def strLastName(self, strLastName):
  448.         if strLastName == "":
  449.             raise Exception("Error. Last name cannot be empty string. Please enter a value for last name.")
  450.  
  451.  
  452.  
  453.     @strGender.setter
  454.     def strGender(self,strGender):
  455.         if strGender == "":
  456.             raise Exception("Error. Gender cannot be empty string. Please enter a value for gender.")  
  457.  
  458.  
  459.  
  460.     @intAge.setter
  461.     def intAge(self, intAge):
  462.         if intAge == "":
  463.             raise Exception("Error. Age cannot be empty string. Please enter a value for age.")
  464.         elif type(intAge) != int:
  465.             raise Exception("Error. Age contains non-numeric characters. Please enter numbers only.")
  466.         elif intAge < 0:
  467.             raise Exception("Error. Age cannot be less than zero.")
  468.  
  469.  
  470.     # ----------------------------------------------
  471.     # Name:         Class Constructor
  472.     # Purpose:      Initializes class objects
  473.     # ----------------------------------------------
  474.     def __init__(self, fname, lname, intAge, strGender):
  475.         self.firstname = fname
  476.         self.lastname = lname
  477.         self.intAge = intAge
  478.         self.strGender = strGender
  479.  
  480.     # ----------------------------------------------
  481.     # Name:         TotalNumPersons
  482.     # Purpose:      Counts all person objects and returns total
  483.     # ----------------------------------------------
  484.     def TotalNumPersons(lstStudents):
  485.        
  486.         # declare vars
  487.         intTotalPersons = int(0)
  488.  
  489.         # calculate total number of students
  490.         intTotalPersons = len(lstStudents)
  491.  
  492.         #return results
  493.         return intTotalPersons
  494.  
  495.  
  496.  
  497.  
  498.  
  499.  
  500.  
  501.  
  502.  
  503. # And just for completeness' sake, here's the entire module where the code actually gets called:
  504.  
  505. import clsPerson
  506. import clsStudent
  507. import clsGraduate
  508.  
  509. # ==================================================================
  510. # Main Module
  511. # ==================================================================
  512.  
  513. # declare variables
  514. intTotalMaleStudents = int(0)
  515. intTotalFemaleStudents = int(0)
  516. intStudentCount = int(0)
  517. dblAverageGPA = float(0.0)
  518. dblAverageGradGPA = float(0.0)
  519. dblAverageMaleAge = float(0.0)
  520. dblAverageFemaleAge = float(0.0)
  521. dblAverageMaleGradAge = float(0.0)
  522. dblAverageFemaleGradAge = float(0.0)
  523. intIndex = int(0)
  524.  
  525.  
  526.  
  527. # --------------------------------------------------------------
  528. # Create student objects
  529. # --------------------------------------------------------------
  530.  
  531. # NOTE: instructions just said "collect 5 students," but the attributes
  532. #     required that they be instantiated as objects of clsGraduate,
  533. #     since clsStudent doesn't have intGradYear or strJobStatus
  534.  
  535. # create first object from the Student class"
  536. Student1 = clsGraduate.clsGraduate("Bill", "Zara", "male", 3.25, 20, 2018, "Y")
  537.  
  538. # create second object from the Student class"
  539. Student2 = clsGraduate.clsGraduate("Betty", "Tara", "female", 2.25, 25, 2019, "N")
  540.  
  541. # create third object from the Student class"
  542. Student3 = clsGraduate.clsGraduate("Sydney", "Nye", "female", 3.75, 30, 2017, "Y")
  543.  
  544. # create forth object from the Student class"
  545. Student4 = clsGraduate.clsGraduate("Jake", "Leedom", "male", 2.75, 35, 2016, "N")
  546.  
  547. # create fifth object from the Student class"
  548. Student5 = clsGraduate.clsGraduate("Alex", "Jacobs", "male", 3.55, 21, 2018, "Y")
  549.  
  550.  
  551.  
  552. # --------------------------------------------------------------
  553. # Calculate results
  554. # --------------------------------------------------------------
  555.  
  556. # calculate number of graduated students
  557. intGraduatedStudents = clsGraduate.clsGraduate.intGraduatedMales + clsGraduate.clsGraduate.intGraduatedFemales
  558.  
  559. # calculate average GPA of all graduated students
  560. dblAverageGradGPA = clsGraduate.clsGraduate.GetAverageGradGPA()
  561.  
  562. # calculate average age of graduated male students
  563. dblAverageMaleGradAge = clsGraduate.clsGraduate.GetAverageMaleGradAge()
  564.  
  565. # calculate average age of graduated female students
  566. dblAverageFemaleGradAge = clsGraduate.clsGraduate.GetAverageFemaleGradAge()
  567.  
  568. # calculate total number of graduated male students who are employed
  569. intEmployedMaleGrads = clsGraduate.clsGraduate.GetEmployedMaleGrads()
  570.  
  571. # calculate total number of graduated female students who are employed
  572. intEmployedMaleFemaleGrads = clsGraduate.clsGraduate.GetEmployedFemaleGrads()
  573.  
  574.  
  575.  
  576. # --------------------------------------------------------------
  577. # Display results
  578. # --------------------------------------------------------------
  579.  
  580. print("Number of graduated students: ", intGraduatedStudents)
  581. print("Average GPA of all graduated students: ", dblAverageGradGPA)
  582. print("Average age of graduated male students: ", dblAverageMaleGradAge)
  583. print("Average age of graduated female students: ", dblAverageFemaleGradAge)
  584. print("Total number of graduated male students who are employed: ", intEmployedMaleGrads)
  585. print("Total number of graduated female students who are employed: ", intEmployedMaleFemaleGrads)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement