Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

using python 3 : I ALWAYS RATE MY ANSWER!!! Lab 9: Class-level Attributres EXAMP

ID: 3604950 • Letter: U

Question

using python 3 : I ALWAYS RATE MY ANSWER!!!

Lab 9: Class-level Attributres EXAMPLE: Class-Level Attributes - type this in and make sure it works. #filename testStudentpy from modStudent import Student #filename : modStudent.py class Student: numStu = 0 definit(selfinitLastName, initFirstName, initGPA): self id = Student.numStu Student_numStu += 1 self ._lastName = initLastName self_firstName = initFirstName self.gpa = initGPA numStudents - StudentgetStuct) print("Initial Number of Students: ", numStudents) 51 = Student"Jones", "Joe",3.5); print("Created si: "S1) numStudents - StudentgetStuct) print("[nNumber of students after s1 created: numStudents) def getStuct): return Student numStu def getID(self): return self_id print(" Last Name of sl: si.getLastName()) print("ID of s: si.getID(); 51setLastName("Smith"); si.setGPA(4.0); print("sl after changing last name and gpa:", s1) def getLastName(self: return self _lastName def getFirstName(self): return self_firstName 52 = Student("Brown", "Rachael",3.6) print(" Created s2: ", 52 ) print("Number of students after creating s: ", I Student getStuct) def getGPAself: return self _gpa def setLastName(self, tmplastName): self. _lastName = tmLastName def setFirstName(self,tmpFirstName). self_firstName = tmpFirstName def setGPA(selftmpGPA): self gpa = tmpGPA def_str_(self): return strself _id) +" "+ self_lastName + | "+ self._firstName + " "+ str(self.gpa) YOUR TURN : Part 1: 1. Add another instance-level attribute called stuld to the Student class 2. Add another class-level variable to the Student class called lastIDUsed. 3. Revise the constructor method Use the class-level attribute to initialize the stuld attribute NOTE: do not add another parameter! The end-user isn't allowed to choose the id!

Explanation / Answer

#filename modStudent.py
class Student:

lastIDUsed=1
_numStu=0
#init method defintion
def __init__(self, initLastName,initFirstName,initGPA):
self._id=Student._numStu
Student._numStu+=1
#set class level attribute lastIDUsed to
#_stuID attribute
self._stuID=Student.lastIDUsed
Student.lastIDUsed+=1
self._lastName=initLastName
self._firstName=initFirstName
self._gpa=initGPA


def setLastName(self,tmpLastName):
self._lastName=tmpLastName

def setFirstName(self,tmpFirstName):
self._firstName=tmpFirstName

def setGPA(self,tmpGPA):
self._gpa=tmpGPA

def getStuCt():
return Student._numStu

def getID(self):
return self._stuID
def getLastName(self ):
return self._lastName
def getFirstName(self ):
return self._firstName
def getGPA(self ):
return self._gpa

def __str__(self):
return str(self._stuID)+" "+self._lastName+
""+self._firstName+" "+str(self._gpa)

------------------ ------------------ ------------------ ------------------ ------------------ ------------------

#save as modPerson.py
class Person:
lastIDUsed=0
totalPopulation=0

#constructor to set first and last name
def __init__(self,firstName, lastName):
Person.lastIDUsed+=1
self._id=Person.lastIDUsed
self._firstName=firstName
self._lastName=lastName

#override __str__ method
def __str__(self):
return "ID : "+str(self._id)+" Name : "+self._lastName+
""+self._firstName+""

#set last name, first name and last id used
def setLastName(self,tmpLastName):
self._lastName=tmpLastName

def setFirstName(self,tmpFirstName):
self._firstName=tmpFirstName

def getLastIDUsed(self):
return Person.lastIDUsed

#id, last name and first name
def getID(self):
return self._stuID
def getLastName(self ):
return self._lastName
def getFirstName(self ):
return self._firstName

-------------- ---------------------------- ---------------------------- ---------------------------- --------------

#testerStudent.py
from modStudent import Student
from modPerson import Person
def main():

#calling methods and print its corresponding values
numStudents=Student.getStuCt()
print("Initial Number of Students",numStudents)
s1= Student("Jones","Joe",3.5)
print("Created s1: ",s1)

numStudents=Student.getStuCt()
print(" Initial Number of Students after s1 created:",numStudents)

print(" Last Name of s1: ",s1.getLastName())
print("ID of s1:",s1.getID())
s1.setLastName("Smith")
s1.setGPA(4.0)

print("s1 after changing last name and gpa:",s1)

s2=Student("Brown","Rachael",3.6)
print(" Created s2:",s2)
print("Number of students afte creating s2:",
Student.getStuCt())

#create Person object
p1= Person("Jones","Joe")
print("Created p1: ",p1)
  
#set last and first name
p1.setLastName("Smith")
p1.setFirstName("Mark")
print("p1 after changing last name and gpa:",p1)
print("Id : ",p1.getLastIDUsed())

#calling main
main()

-------------- ---------------------------- ---------------------------- ---------------------------- --------------

sample Output :

Initial Number of Students 0
Created s1: 1 JonesJoe 3.5

Initial Number of Students after s1 created: 1

Last Name of s1: Jones
ID of s1: 1
s1 after changing last name and gpa: 1 SmithJoe 4.0

Created s2: 2 BrownRachael 3.6
Number of students afte creating s2: 2
Created p1: ID : 1 Name : JoeJones
p1 after changing last name and gpa: ID : 1 Name : SmithMark
Id : 1