Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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:

image text in transcribed

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:

image text in transcribed

#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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intelligent Information And Database Systems Asian Conference Aciids 2012 Kaohsiung Taiwan March 2012 Proceedings Part 2 Lnai 7197

Authors: Jeng-Shyang Pan ,Shyi-Ming Chen ,Ngoc-Thanh Nguyen

2012th Edition

3642284892, 978-3642284892

More Books

Students also viewed these Databases questions

Question

2 What supply is and what affects it.

Answered: 1 week ago