Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Python 3 Your hw10.py must contain the definition of the class Pets with hidden fields containing: name type age The constructor should take one argument,

Python 3

Your hw10.py must contain the definition of the class Pets with hidden fields containing:

name

type

age

The constructor should take one argument, the name of the pet. It should set the type to the empty string and the age to None The class should have the following public functions:

set_type

set_age

get_name

get_type

get_age

older

set_type should print an error message if the type is not 'cat' or 'dog'. get_age should use a conversion function to set the age attribute to an integer. It should catch the exception cause by calling set_age with an argument that cannot be turned into an integer and print an error message when this happens. It should also have the following magic methods:

__str__

__sub__

The __sub__ method should return the integer you get from subtracting the age of another Pet object from the age of the object. older should print the messages you will see below in the testing code.

Testing

You MUST add the following test code to your hw10.py script:

if __name__ == '__main__':p1 = Pet('Henry') p1.set_type('dawg') p1.set_type('dog') p1.set_age('seven') p1.set_age('7') print(p1.get_name()) print(p1.get_type()) print(p1.get_age()) print(p1) p2 = Pet('Felix') p2.set_age(2) p2.older(p1) p1.older(p2) p3 = Pet('Angus') p3.set_age(7) p3.older(p1) p1.older(p3)

Your output should look something like this

$ ./hw06.py dawg is not a valid pet type seven cannot be converted into an integer Henry dog 7 Henry, dog Felix is younger than Henry Henry is older than Felix Henry and Angus are the same age Angus and Henry are the same age

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

Entity Alignment Concepts Recent Advances And Novel Approaches

Authors: Xiang Zhao ,Weixin Zeng ,Jiuyang Tang

1st Edition

9819942527, 978-9819942527

More Books

Students also viewed these Databases questions