Question
Need help with Intro to Computer Science 2 (Python) problem: Implement an exception class InvalidAge () for the Animal class implemented in the second lab
Need help with Intro to Computer Science 2 (Python) problem:
Implement an exception class InvalidAge() for the Animal class implemented in the second lab and found in the template file. In addition to writing the InvalidAge class you must modify the setAge() method of the Animal class to check if the age provided as a parameter is negative. If it is, it raises an InvalidAge exception. It should raise the exception before any object variables are modified. If the age is positive or zero, it should set the age of the animal to the specified parameter. The constructor should be modified in a similar way so that Animal objects with negative ages cannot be created.
This is what we are given in the template file:
The following demonstrates how the classes could be used when completed (with appropriate edits to the output to not give away the answer to the problem:
#Modified Animal class with InvalidAge exception for negative ages class InvalidAge (Exception) pa 33 class Animal #modify init def init self newspecies default newLanguage default newAge 0) if newAge 0: raise Invalid Age else: self species new species self. language newLanguage self-age newAge def repr self return Animal ('f)',' H)) format (self. species self. language self.age) def eq (self otherAnimal. return self. species otherAnimal species and self. language other Animal language and self.age other Animal .age def setSpecies (self, new species) self species new Species def setLanguage (self, new Language self language newLanguage #modify setAge def setAge (self, newAge self-age newAge def speak (self) print ("I am a year-old f and I f) format (self.age, self. Species self language))Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started