Question

Challenge: Animal Class Description: Create a class in Python 3 named Animal that has specified attributes...

Challenge: Animal Class

Description: Create a class in Python 3 named Animal that has specified attributes and methods.

Purpose: The purpose of this challenge is to provide experience creating a class and working with OO concepts in Python 3.

Requirements:

Write a class in Python 3 named Animal that has the following attributes and methods and is saved in the file Animal.py.

Attributes

__animal_type is a hidden attribute used to indicate the animal’s type. For example: gecko, walrus, tiger, etc.

__name is a hidden attribute used to indicate the animal’s name.

__mood is a hidden attribute used to indicate the animal’s mood. For example: happy, hungry, or sleepy.

Methods

__init__ is to define the three attributes above and assign their default values.

The value of __mood is to be set randomly. Generate a random number between 1 and 3. Then:

If the number is 1, the __mood attribute is to be set to a value of happy.
If the number is 2, the __mood attribute is to be set to a value of hungry.
If the number is 3, the __mood attribute is to be set to a value of sleepy.

get_animal_type should return the value of the __animal_type field.

get_name should return the value of the __name field.

check_mood should return the value of the __mood field.

Animal Generator Program

Once you have created the Animal class, create another Python file called animalGenerator.py. This program is to use Animal.py as a module.

In animalGenerator.py, prompt the user to enter the name and type of an animal. Create a new Animal object instance to store this data. Then, ask the user if they would like to repeat the process. They should be able to create as many Animal object instances as they would like.

After the user is done creating animals, the program is to use the object’s accessor methods (get_animal_type, get_name, and check_mood) to retrieve the name, type, and mood of each animal. This information is to be formatted and displayed as shown in the sample program output below.

Welcome to the animal generator!
This program creates Animal objects.

What type of animal would you like to create? Gecko
What is the animal’s name? Gordon

Would you like to add more animals (y/n)? y

What type of animal would you like to create? Walrus
What is the animal’s name? Wally

Would you like to add more animals (y/n)? y

What type of animal would you like to create? Tiger
What is the animal’s name? Truman

Would you like to add more animals (y/n)? n

Animal List:
Gordon the Gecko is hungry
Wally the Walrus is sleepy
Truman the Tiger is hungry

Homework Answers

Answer #1

SOLUTION-
I have solve the problem in python code with comments and screenshot for easy understanding :)

CODE-

Animal.py


Python version : 3.6
Python program to create class Animal
'''
class Animal:
  
   # constructor to initialize the values of animal type, name and mood
   def __init__(self, animal_type , name):
       self.__animal_type = animal_type
       self.__name = name
      
       value = random.randint(1,3)
       if value == 1:
           self.__mood = "happy"
       elif value == 2:
           self.__mood = "hungry"
       else:
           self.__mood = "sleepy"
          
   # function to return the animal type
   def get_animal_type (self):
       return self.__animal_type
  
   # function to return the name   
   def get_name (self):
       return self.__name
  
   # function to rteurn the mood  
   def check_mood(self):
       return self.__mood
#end of class Animal  

#end of Animal.py

Code Screenshot:

'''
animalGenerator.py
Python version : 3.6
Python program to implement class Animal
'''

from Animal from Animal
import random

def main():
  
   # display the welcome header
   print('Welcome to the animal generator!')
   print('This program creates Animal objects.')
   animalList = [] # list to store the animals
   # loop continues till the user wants
   while True:
       # input of animal type and name
       animal_type = input('What type of animal would you like to create? ')
       name = input("What is the animal's name? ")
       animal = Animal(animal_type,name) # create an animal
       animalList.append(animal) # add the animal to the list
       choice = input('Would you like to add more animals (y/n)? ')
       if choice.lower() == 'n':
           break
   # display the animal information      
   if(len(animalList) == 0):
       print('Empty Animal list')
   else:
       print('Animal List:')
       for animal in animalList:
           print(animal.get_animal_type()+" the "+animal.get_name()+" is "+animal.check_mood())
#call the main function
main()          
#end of program
#end of animalGenerator.py

Code Screenshot:

Output:

IF YOU HAVE ANY DOUBT PLEASE COMMENT DOWN BELOW I WILL SOLVE IT FOR YOU:)
----------------PLEASE RATE THE ANSWER-----------THANK YOU!!!!!!!!----------

Know the answer?
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
Similar Questions
How to write a Python program that has a base class with methods and attributes ,...
How to write a Python program that has a base class with methods and attributes , subclasses with methods and attributes. Please use any example you'd like.
write code using python or repl.it 1. List 4 attributes that you'd create for class "student"....
write code using python or repl.it 1. List 4 attributes that you'd create for class "student". 2. List 2 setters and 2 getters for class student including their parameters 3. Write the complete definition for class student including attributes and methods created above using the correct Python syntax. 4. Create one object of class student and print the values of all its attributes. You can either call the getter method or access the attribute directly using the dot notation.
CREATE AN ERD FOR THE CASE BELOW The county animal shelter has hired you to design...
CREATE AN ERD FOR THE CASE BELOW The county animal shelter has hired you to design a database to track the animals at the shelter along with the people who adopt them. You have worked with the staff at the shelter, analyzed how they perform the work now, performed user requirements gathering and you have created a DFD. Below you will find the description of the entities and attributes defined during this process. Animal: animal_id, species, breed, age, gender, size,...
The county animal shelter has hired you to design a database to track the animals at...
The county animal shelter has hired you to design a database to track the animals at the shelter along with the people who adopt them. You have worked with the staff at the shelter, analyzed how they perform the work now, performed user requirements gathering and you have to create an ERD. Below you will find the description of the entities and attributes defined during this process. Animal: animal_id, species, breed, age, gender, size, spay/neuter, intake_date, location, cost Adopter: name,...
The county animal shelter has hired you to design a database to track the animals at...
The county animal shelter has hired you to design a database to track the animals at the shelter along with the people who adopt them. You have worked with the staff at the shelter, analyzed how they perform the work now, performed user requirements gathering and you have created a DFD. Below you will find the description of the entities and attributes defined during this process. Animal: animal_id, species, breed, age, gender, size, spay/neuter, intake_date, location, cost Adopter: name, phone,...
Write a Python class, Flower, that has 3 instance variables name, num_petals and price which have...
Write a Python class, Flower, that has 3 instance variables name, num_petals and price which have types str, int and float. Your class must have a constructor method to initialize the variables to an appropriate value, and methods for setting the value of each instance variable (set methods) and for retrieving the instance variable values (get methods). You can use the credit card code we looked at for assistance.
URGENT (In Python) Create specified classes with following requirements. Class PizzaOrder Ability to add/remove pizza(s) An...
URGENT (In Python) Create specified classes with following requirements. Class PizzaOrder Ability to add/remove pizza(s) An order can have more than one pizza. Ability to specify the store for which the order is made Ability to apply special promotion code Ability to check the order status Possible statuses are ORDER_CREATED, ORDER_CANCELED, ORDER_READY, ORDER_ON_DELIVERY, ORDER_COMPLETE Has customer information Class Pizza Ability to specify toppings Ability to add/remove toppings Ability to specify price Ability to specify crust type (thin/thick) Class Store Ability...
Create your own date class. Create a MyDate class with these methods. A constructor that accepts...
Create your own date class. Create a MyDate class with these methods. A constructor that accepts a month, day, and year A method that determines if the year is a leap year A method that returns the date in this format, "1/9/2020" A method that returns the date in this format, "January 9, 2020" A method that returns the day A method that returns the name of the month A method that returns the number of days in the month...
Each part of this lab will use the same input file, named “lab3-in.dat.” This file will...
Each part of this lab will use the same input file, named “lab3-in.dat.” This file will be located in the same directory as the executables (do not specify a path for the file in the Select statements). This file is a roster of animals in a travelling carnival. The record format for the file is as follows: Field Description Length Data Type Name 12 String Gender 1 String Species 15 String The end result of each part of this assignment...
Create a class called Employee that should include four pieces of information as instance variables—a firstName...
Create a class called Employee that should include four pieces of information as instance variables—a firstName (type String), a lastName (type String), a mobileNumber (type String) and a salary (type int). Your class (Employee) should have a full argument constructor that initializes the four instance variables. Provide a set and a get method for each instance variable. The validation for each attribute should be like below: mobileNumber should be started from “05” and the length will be limited to 10...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT