Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 16 Online
OpenStudy (anonymous):

Problem 2-3 : 5.0 points Modify the EdxStudent class so that it passes all the unit tests, without throwing any exceptions. Paste your entire definition of the class into this box.

OpenStudy (anonymous):

Consider the following class definition: class EdxStudent(object): def __init__(self,first,last): self.setName("given",first) self.setName("family",last) def setName(self,which,name): if which == "given" or which == "first": self.firstName = name elif which == "family" or which == "last": self.lastName = name else: raise ChoiceError(which) def getName(self,which): if which == "given" or which == "first": return firstName elif which == "family" or which == "last": return lastName raise ChoiceError(which) class ChoiceError(Exception): def __init__(self, value): self.value = value def __str__(self): return repr("Invalid value for name field:"+self.value) firstName = "Raquel" lastName = "Pomplun" try: student = EdxStudent(firstName,lastName) initialFirstName = student.getName("first") initialLastName = student.getName("last") newFirstName = student.getName("given") newLastName = student.getName("family") if initialFirstName == newFirstName and initialLastName == newLastName: print "Passed unit test 1" else: print "Failed unit test 1" student.setName("given","LeBron") student.setName("family","James") if "LeBron"+"James" == student.getName("first")+student.getName("last"): print "Passed unit test 2" else: print "Failed unit test 2" except ChoiceError as e: print "Failed unit test. Exception is:", print e

OpenStudy (anonymous):

This might help: class EdxStudent(object): http://pythontutor.com/visualize.html#code=class+EdxStudent(object)%3A%0A++++++def+__init__(self,first,last)%3A%0A+++++++++++++self.setName(%22given%22,first)%0A+++++++++++++self.setName(%22family%22,last)%0A%0A++++++def+setName(self,which,name)%3A%0A++++++++++++if+which+%3D%3D+%22given%22+or+which+%3D%3D+%22first%22%3A%0A++++++++++++++++++self.firstName+%3D+name%0A++++++++++++elif+which+%3D%3D+%22family%22+or+which+%3D%3D+%22last%22%3A%0A++++++++++++++++++self.lastName+%3D+name%0A++++++++++++else%3A%0A++++++++++++++++++raise+ChoiceError(which)%0A%0A++++++def+getName(self,which)%3A%0A++++++++++++if+which+%3D%3D+%22given%22+or+which+%3D%3D+%22first%22%3A%0A++++++++++++++++++return+firstName%0A++++++++++++elif+which+%3D%3D+%22family%22+or+which+%3D%3D+%22last%22%3A%0A++++++++++++++++++return+lastName%0A++++++++++++raise+ChoiceError(which)%0A%0Aclass+ChoiceError(Exception)%3A%0A+++++def+__init__(self,+value)%3A%0A+++++++++self.value+%3D+value%0A+++++def+__str__(self)%3A%0A+++++++++return+repr(%22Invalid+value+for+name+field%3A%22%2Bself.value)%0A%0A%0AfirstName+%3D+%22Raquel%22%0AlastName+%3D+%22Pomplun%22%0A%0Atry%3A%0A++++++++student+%3D+EdxStudent(firstName,lastName)%0A%0A++++++++initialFirstName+%3D+student.getName(%22first%22)%0A++++++++initialLastName+%3D+student.getName(%22last%22)%0A%0A%0A++++++++newFirstName+%3D+student.getName(%22given%22)%0A++++++++newLastName+%3D+student.getName(%22family%22)%0A%0A++++++++if+initialFirstName+%3D%3D+newFirstName+and+initialLastName+%3D%3D+newLastName%3A%0A+++++++++++++++print+%22Passed+unit+test+1%22%0A++++++++else%3A%0A+++++++++++++++print+%22Failed+unit+test+1%22%0A%0A++++++++student.setName(%22given%22,%22LeBron%22)%0A++++++++student.setName(%22family%22,%22James%22)%0A%0A++++++++if+%22LeBron%22%2B%22James%22+%3D%3D+student.getName(%22first%22)%2Bstudent.getName(%22last%22)%3A%0A+++++++++++++++print+%22Passed+unit+test+2%22%0A++++++++else%3A%0A+++++++++++++++print+%22Failed+unit+test+2%22%0A%0Aexcept+ChoiceError+as+e%3A%0A+++++++print+%22Failed+unit+test.+Exception+is%3A%22,%0A+++++++print+e&mode=display&cumulative=false&heapPrimitives=false&drawParentPointers=false&textReferences=false&showOnlyOutputs=false&py=2&curInstr=0

OpenStudy (anonymous):

Basically all you have to do is make it so this part is passable: if "LeBron"+"James" == student.getName("first")+student.getName("last"): print "Passed unit test 2" else: print "Failed unit test 2"

OpenStudy (anonymous):

class EdxStudent(object): def __init__(self,first,last): self.setName("given",first) self.setName("family",last) def setName(self,which,name): if which == "given" or which == "first": self.firstName = name elif which == "family" or which == "last": self.lastName = name else: raise ChoiceError(which) def getName(self,which): if which == "given" or which == "first": return firstName elif which == "family" or which == "last": return lastName raise ChoiceError(which) class ChoiceError(Exception): def __init__(self, value): self.value = value def __str__(self): return repr("Invalid value for name field:"+self.value) firstName = "LeBron" lastName = "James" try: student = EdxStudent(firstName,lastName) initialFirstName = student.getName("first") initialLastName = student.getName("last") newFirstName = student.getName("given") newLastName = student.getName("family") if initialFirstName == newFirstName and initialLastName == newLastName: print "Passed unit test 1" else: print "Failed unit test 1" student.setName("given","LeBron") student.setName("family","James") if "LeBron"+"James" == student.getName("first")+student.getName("last"): print "Passed unit test 2" else: print "Failed unit test 2" except ChoiceError as e: print "Failed unit test. Exception is:", print e

OpenStudy (anonymous):

fault :(

OpenStudy (anonymous):

help

OpenStudy (anonymous):

No it should be fine. I got it right myself. Anyways, visit this site- http://www.getstudyroom.com/course/40429 I can help there.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!