Question

PYTHON LANGUAGE HW Due Sunday, November 17th at 11:59 PM. Deliverables There is one deliverable for...

PYTHON LANGUAGE HW

Due

Sunday, November 17th at 11:59 PM.

Deliverables

There is one deliverable for this assignment

  • hw10.py

Make sure the script obeys all the rules in the Script Requirements page.

Setup

All work for this assignment must be done on users3.cs.umb.edu.

The hw10.py file must be created in an hw10 directory, inside your hw directory, inside your it117 directory.

From your home directory go to your it117 directory

cd it117

Go to your hwdirectory

cd hw

Create an hw10 directory

mkdir hw10

Enter this new directory

cd hw10

Create the file hw10.sh

nano hw10.py

Specification

Your hw10.py must contain the definition of the class Pet 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.

Test Code

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)

Output

Your output should look something like this

$ ./hw10.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

Be sure to run this script on the Unix machine so you know it works in the environment in which I will run it when I score your homework.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
class Pet:
    def __init__(self, name):
        self.name = name
        self.type = ''
        self.age = 0

    def setName(self, name):
        self.name = name

    def set_type(self, type):
        if type in ['cat', 'dog']:
            self.type = type
        else:
            print('dawg is not a valid pet type')

    def set_age(self, age):
        try:
            self.age = int(age)
        except:
            print(age + ' cannot be converted into an integer')

    def get_name(self):
        return self.name

    def get_type(self):
        return self.type

    def get_age(self):
        return self.age

    def __str__(self):
        return self.name + ', ' + self.type

    def __sub__(self, other):
        return self.age - other.age

    def older(self, p):
        if self.age > p.age:
            print(self.name + ' is older than ' + p.name)
        elif self.age < p.age:
            print(self.name + ' is younger than ' + p.name)
        else:
            print(p.name + ' and ' + self.name + ' are the same age')


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)

Add a comment
Know the answer?
Add Answer to:
PYTHON LANGUAGE HW Due Sunday, November 17th at 11:59 PM. Deliverables There is one deliverable for...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • please help!!!! JAVA I done the project expect one part but I still give you all...

    please help!!!! JAVA I done the project expect one part but I still give you all the detail that you needed... and I will post my code please help me fix the CreateGrid() part in main and make GUI works    List Type Data Structures Overview : You will be implementing my version of a linked list. This is a linked list which has possible sublists descending from each node. These sublists are used to group together all nodes which...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT