SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Tony Gaddis
Chapter:
Classes And Object Oriented Programming
Exercise:
Programming Exercises
Question:1 | ISBN:9780132576376 | Edition: 2

Question

Pet Class
Write a class named Pet, which should have the following data attributes:
• __name (for the name of a pet)
• __animal_type (for the type of animal that a pet is. Example values are ‘Dog’, ‘Cat’,and ‘Bird’)
• __age (for the pet’s age)


The Pet class should have an __init__ method that creates these attributes. It should also have the following methods:
• set_name
This method assigns a value to the __name field.
• set_animal_type
This method assigns a value to the __animal_type field.
• set_age
This method assigns a value to the __age field.
• get_name
This method returns the value of the name field.
• get_type
This method returns the value of the type field.
• get_age
This method returns the value of the age field.


Once you have written the class, write a program that creates an object of the class and prompts the user to enter the name, type, and age of his or her pet. This data should be stored as the object’s attributes. Use the object’s accessor methods to retrieve the pet’s name,type, and age and display this data on the screen.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Pet class program code:

# Create a class name, Pet 
class Pet:
    # Pet class should have an __init__ method
    # for create  data attributes  __name,__animal_type,__age
    def __init__(self):
        self.__name = ""
        self.__animal_type = ""
        self.__age = 0
    #set_name method assigns a value to the __name field.
    def set_name(self, name):
        self.__name = name
    # set_animal_type method assigns a value to the __animal_type field.
    def set_animal_type(self, animal_type):
        self.__animal_type = animal_type
    #set_age method assigns a value to the __age field.
    def set_age(self, age):
        self.__age = age
    #get_name method returns the value of the name field.
    def get_name(self):
        return self.__name
    # get_type method returns the value of the type field.
    def get_type(self):
        return self.__animal_type
    #get_age method returns the value of the age field.
    def get_age(self):
        return self.__age

# Create an object of the Pet class
pet = Pet()

# Prompt and read the user to enter pet details
name = input("Enter the name of your pet: ")
animal_type = input("Enter the type of animal your pet is: ")
age = int(input("Enter the age of your pet: "))

# Set the pet's attributes
pet.set_name(name)
pet.set_animal_type(animal_type)
pet.set_age(age)

# Display the pet's details as an output
#Use the object’s accessor methods to retrieve 
#the pet’s name,type, and age and 
#display this data on the screen.
print("Pet's name:", pet.get_name())
print("Pet's type:", pet.get_type())
print("Pet's age:", pet.get_age())

Executed Output:

Enter the name of your pet: Zooly
Enter the type of animal your pet is: dog
Enter the age of your pet: 5     
Pet's name: Zooly
Pet's type: dog
Pet's age: 5

 

0 0

Discussions

Post the discussion to improve the above solution.