Advertisement
TheBlackPopeSJ

Untitled

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