120 likes | 233 Vues
This program demonstrates how to create a Student class in Python to manage student records. Each student has a last name, first name, test score, and lab score. The program includes methods for sorting a list of students alphabetically by last name, printing their information, and overloading operators to enhance functionality. With clear examples and the demonstration of operator overloading, this code provides insights into object-oriented programming principles and how to manipulate and manage data effectively.
E N D
Sorting a class of students? class Student(object): def __init__(self, lastname, firstname, testscore, labscore): self.lastname = lastname self.firstname = firstname self.testscore = testscore self.labscore = labscore liststu = [Student("baker","tom",132,88), Student("abbey","steven",122,96), Student("lawrence","sarah",144,85), Student("miller","john",132,92), Student("jones","tina",128,94)]
Sorting on Last name: def sortstudentslname(liststu): for i in range(len(liststu)): smallstudent = liststu[i].lastname smallindex = i for j in range(i+1,len(liststu)): if liststu[j].lastname < smallstudent: smallstudent = liststu[j].lastname smallindex = j temp = liststu[i] liststu[i] = liststu[smallindex] liststu[smallindex] = temp return(liststu)
class Student(object): def __init__(self, lastname, firstname, testscore, labscore): self.lastname = lastname self.firstname = firstname self.testscore = testscore self.labscore = labscore def __str__(self): strvar = "Student ( \n“ + "Lastname: " + self.lastname + "\n" strvar += "Firstname: " + self.firstname + "\n" strvar += "Testscore: " + str(self.testscore)+"\n“+"Labscore: "+str(self.labscore)+"\n" strvar += ")\n\n" return(strvar) liststu = [Student("baker","tom",132,88), Student("abbey","steven",122,96), Student("lawrence","sarah",144,85), Student("miller","john",132,92), Student("jones","tina",128,94)] def sortstudentslname(liststu): for i in range(len(liststu)): smallstudent = liststu[i].lastname smallindex = i for j in range(i+1,len(liststu)): if liststu[j].lastname < smallstudent: smallstudent = liststu[j].lastname smallindex = j temp = liststu[i] liststu[i] = liststu[smallindex] liststu[smallindex] = temp return(liststu) def printstu(liststu): for i in range(len(liststu)): print liststu[i] printstu(liststu) sortstudentslname(liststu) printstu(liststu)
Operator Overloading • That was a lot of work! Easier way??? • Of course! • Writing our own definition of what an operator should do • E.g., defining student1<student2 to mean: • Does student1’s last name property come before student 2’s last name property?
In class: class Student(object): def __init__(self, lastname, firstname, testscore, labscore): self.lastname = lastname self.firstname = firstname self.testscore = testscore self.labscore = labscore self.totalscore = self.calcscore(200) def __lt__(self, student2): if self.lastname < student2.lastname: return True else: return False def __add__(self,student2): return(self.totalscore + student2.totalscore) def __str__(self): str1 = self.lastname + " " + self.firstname + " " + self.totalscore return(str1)
Now: class Student(object): def __init__(self, lastname, firstname, testscore, labscore): self.lastname = lastname self.firstname = firstname self.testscore = testscore self.labscore = labscore self.totalscore = self.calcscore(200) def __lt__(self, student2): if self.lastname < student2.lastname: return True else: return False def __str__(self): str1 = self.lastname + " " + self.firstname str += " " + self.totalscore return(str1) def calcscore(self,totalpoints): return((self.testscore + self.labscore)/totalpoints) liststu = [Student("baker","tom",132,88), Student("abbey","steven",122,96), Student("lawrence","sarah",144,85), Student("miller","john",132,92), Student("jones","tina",128,94)] def sortstudentslname(liststu): for i in range(len(liststu)): smallstudent = liststu[i] smallindex = i for j in range(i+1,len(liststu)): if liststu[j] < smallstudent: smallstudent = liststu[j] smallindex = j temp = liststu[i] liststu[i] = liststu[smallindex] liststu[smallindex] = temp return(liststu) def printstu(liststu): for i in range(len(liststu)): print liststu[i] printstu(liststu) sortstudentslname(liststu) printstu(liststu) Even better! liststu.sort() printstu(liststu)
list of Operators you can overload: __lt__ ( less than ) __le__ (less or equal to) __eq__ (equal to) __ne__ (not equal to) __gt__ (greater than) __ge__ (greater or equal to) __cmp__ (compare): This one gets called if the previous ones are not defined. __add__ __sub__ __mul__ __div__
Dorm Room Class? • For a dorm room class, we’ll need the name of the hall, the number of the room, and the first and second student occupying the room. • How? class DormRoom(object): def __init__(self,name, number, studenta,studentb): self.name = name self.number = number self.studenta = studenta self.studentb = studentb
To print the Dorm Room Object: class DormRoom(object): def __init__(self,name, number, studenta,studentb): self.name = name self.number = number self.studenta = studenta self.studentb = studentb def __str__(self): strvar = "Dorm Room " + self.name + str(self.number) + ": {\n" strvar += str(self.studenta) strvar += str(self.studentb) strvar += "}\n\n" return strvar
To create a Dorm Room Object: astudent = Student("baker","tom",132,88) bstudent = Student("miller","john",132,92) aroom = DormRoom("Dickinson",310, astudent,bstudent) print(aroom) # prints the last name of student a in the dorm room print(aroom.studenta.lastname)
Honors Dorm? class DormRoom(object): def __init__(self,name, number, studenta,studentb): self.name = name self.number = number self.studenta = studenta self.studentb = studentb def __str__(self): strvar = "Dorm Room " + self.name + str(self.number) + ": {\n" strvar += str(self.studenta) strvar += str(self.studentb) strvar += "}\n\n" return strvar def upgrade(self,studentc): if self.studenta.testscore < studentc.testscore: if self.studenta.testscore < self.studentb.testscore: self.studenta.lastname = studentc.lastname self.studenta.firstname = studentc.firstname self.studenta.testscore = studentc.testscore self.studenta.labscore = studentc.labscore else: self.studentb.lastname = studentc.lastname self.studentb.firstname = studentc.firstname self.studentb.testscore = studentc.testscore self.studentb.labscore = studentc.labscore elifself.studentb.testscore < studentc.testscore: self.studentb.lastname = studentc.lastname self.studentb.firstname = studentc.firstname self.studentb.testscore = studentc.testscore self.studentb.labscore = studentc.labscore
To Use: liststu = [Student("baker","tom",132,88), Student("abbey","steven",122,96), Student("lawrence","sarah",144,85), Student("miller","john",132,92), Student("jones","tina",128,94)] honorsroom = DormRoom("Dickinson",314,liststu[4],liststu[0]) print honorsroom honorsroom.upgrade(liststu[3]) print honorsroom