Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Use python 3.6 some files you may need: lab13.py:https://www.cse.msu.edu/~cse231

ID: 3851289 • Letter: U

Question

Use python 3.6

some files you may need:

lab13.py:https://www.cse.msu.edu/~cse231/Labs/Lab13/lab13.py

pets.py:https://www.cse.msu.edu/~cse231/Labs/Lab13/pets.py

l. Examine the file named "lab13.py", which contains a simple test bed for the classes in "pets.py", and then execute the test bed 2. Examine the file named "pets.py", which contains an outline of four classes to support the handling of pets within Python programs. Please note that class "PetError" is a subclass of class "Exception" and is complete as-is. You will complete the remaining three classes as described below. Class "Pet" (which is a subclass of class "Object") should include the following methods: Accepts three arguments: self, species (default None), and name (default Stores init species and name as data attributes. Raises PetError, if species is not one of the following (case insensitive): 'dog', 'cat', 'horse', 'gerbil', hamster, ferret'. Returns a string which depends on whether the pet is named or not. Str if named: "Species of Xxx, named Yyy", where Xxx is the species data attribute and Yyy is the name data attribute, both in "title-case" (first letter upper ca and rest lower case hint: title0) otherwise: "Species o Xxx, unnamed", where Xxx is the species data attribute in "title-case". Class "Dog" (which is a subclass of class "Pet") should include the following methods: Accepts three arguments: self, name (default and chases (default "Cats"). Uses the init constructor for class "Pet" to store species ("Dog") and name as data attributes. Stores chases as a data attribute. Hint: check out how MassParticle calls the Particle init in Code Listing 12.11 Returns a string which depends on whether the dog is named or not str Hint: call the Pet class str (see MassParticle's Str in Code Listing 12.11) if named: "Species of Dog, named Yyy, chases zzz where Yyy is the name data attribute (as above) and zzz is the chases data attribute. otherwise: "Species of Dog, unnamed, chases zzz", where zzz is the chases data attribute

Explanation / Answer

#### START OF FILE pets.py

#### Class PetError--complete##


class PetError(ValueError):
pass


#### Class Pet--complete##
class Pet(object):
  
def __init__(self, species=None, name=""):
valid_species = [
'dog',
'cat',
'horse',
'gerbil',
'hamster',
'ferret',
]
if species is not None and species.lower() in valid_species:
self.species = species.title()
self.name = name.title()
else:
raise PetError()
  
def getName(self):
return self.name

def getSpecies(self):
return self.species

def __str__(self):
species_str =
'Species of {:s}'.format(self.species.lower().capitalize())
if self.name == '':
name_str = ', unnamed'
else:
name_str =
', named {:s}'.format(self.name.lower().capitalize())
return species_str + name_str


#### Class Dog--complete##

class Dog(Pet):
  
def __init__(self, name='', chases='Cats'):
Pet.__init__(self, 'Dog', name)
self.chases = chases

def __str__(self):
chases_str = ', chases {:s}'.format(self.chases.lower())
return Pet.__str__(self) + chases_str

#### Class Cat--complete##

class Cat(Pet):
  
def __init__(self, name='', hates='Dogs'):
Pet.__init__(self, "Cat", name)
self.hates = hates

def __str__(self):
hates_str = ', hates {:s}'.format(self.hates.lower())
return Pet.__str__(self) + hates_str

#### END OF FILE pets.py

#### START OF FILE lab13.py

#### Demonstrate some of the operations of the Pet classes##

import pets


def main():

# PetError from invalid species

try:
pets.Pet('Monkey')
except pets.PetError:
print ('Got a pet error.')

# PetError from None

try:
pets.Pet()
except pets.PetError:
print ('Got a pet error.')


# Hamster
try:
A = pets.Pet('Hamster')
print (A)
except pets.PetError:
print ('Got a pet error.')

  
# Dog named Fido who chases Cats
try:
B = pets.Dog('Fido')
print (B)
except pets.PetError:
print ('Got a pet error.')

  
# Cat named Fluffy who hates everything
try:
C = pets.Cat('Fluffy', 'everything')
print (C)
except pets.PetError:
print ('Got a pet error.')

# Horse named myHorse
try:
D = pets.Pet('Horse', 'myHorse')
print (D)
except pets.PetError:
print ('Got a pet error.')

# Dog named myDog who chases cars
try:
E = pets.Dog('myDog', 'cars')
print (E)
except pets.PetError:
print ('Got a pet error.')

# Dog who chases burgers
try:
F = pets.Dog()
print (F)
except pets.PetError:
print ('Got a pet error.')

# Cat named Alligator who bluffs
try:
G = pets.Cat('Alligator')
print (G)
except pets.PetError:
print ('Got a pet error.')
#### END OF FILE lab13.py