Question

In: Computer Science

Please, how to construct a class template in Python? Class name is animal: give it a...

Please, how to construct a class template in Python?

Class name is animal:

give it a set of class variables called: gender, status

give it a set of class private variables: __heart, __lungs, __brain_size

construct methods to write and read the private class variables

in addition initiate in the constructor the following instance variables: name, location as well as initiate the variables status and gender all from constructor parameters

The variables meaning is as follows:

name is name

status is wild or domestic

location is location

gender is gender (M/F)

Private variables have all values Yes or NO only __brain_size should be a number

Be sure to provide a check on every variable that requires it.. Yes/No variables can only get values Yes/NO, Numeric variables only number. The rest does not need a check.

Finally provide a method that will print the class state on the output such as

Animal Name: Name

Animal Location: location

etc (print all parameters with the appropriate description)

All variables that you are initializing will depend on the user

Solutions

Expert Solution

Python code pasted below.

#class Definition
class animal:
#Constructor
def __init__(self,name,location,gender,status):
self.name=name
self.location=location
self.gender=gender
self.status=status
#display animal details   
def displayAnimal(self):
print("Animal name:",self.name)
print("Location:",self.location)
print("Gender:",self.gender)
print("Status:",self.status)
#method for getting values of private variables
#private variables are declared with preceeding 2 double underscore   
def getParam(self,heart,lungs,brain):
self.__heart=heart
self.__lungs=lungs
self.__brain_size=brain
#method for displaying values of private variables
#private variables are declared with preceeding 2 double underscore
def displayParam(self):
print("Heart:",self.__heart)
print("Lungs:",self.__lungs)
print("Brain size:",self.__brain_size)
  
#main program
#creating animal object and invoking the constructor
a=animal("Lion","Africa","M","wild")
#calling displayAnimal() method for displaying the animal attributes
a.displayAnimal()
#calling getParam to pass values to private variables in animal class
a.getParam("Yes","Yes",20)
#calling displayParam() to display the values of private variables in animal class
a.displayParam()

Python code in IDLE pasted for better understanding of the indent.

Output Screen


Related Solutions

Given: You are given a Python Class template. In this class there is a class variable...
Given: You are given a Python Class template. In this class there is a class variable vector, is a list of N non-negative integers and are stored (in positions 0, 1, 2, ... (N-1)), where at least one integer is 0. Task: Write a recursive function "findAllPaths" to find all possible path through V starting at position 0, and ending at the location of 0, in accordance with the Rule below. If no such path exists, "paths" should be an...
Animal class Create a simple class called Animal instantiated with a name and a method toString...
Animal class Create a simple class called Animal instantiated with a name and a method toString which returns the name. Cat class Create a simple class Cat which extends Animal, but adds no new instance variable or methods. RedCat class Create a simple class RedCat which extends Cat, but adds no new instance variable or methods. WildCardTester class Create a class with the main method and methods addCat, deleteCat and printAll as follows. addCat method Has two parameters, an ArrayList...
PYTHON PLEASE: Construct a class “Monster” with the following attributes: self.name (a string) self.type (a string,...
PYTHON PLEASE: Construct a class “Monster” with the following attributes: self.name (a string) self.type (a string, default is ‘Normal’) self.current_hp (int, starts out equal to max_hp) self.max_hp (int, is given as input when the class instance is created, default is 20) self.exp (int, starts at 0, is increased by fighting) self.attacks (a dict of all known attacks) self.possible_attacks (a dictionary of all possible attacks) The dictionary of possible_attacks will map the name of an attack (the key) to how many...
Do it in python please! also please use this template please I have provided and below...
Do it in python please! also please use this template please I have provided and below is the activity def main(): # import the module random try: # asking the user to enter a number between 1 and 100 #loop time while if elif #for loop # generates that number of random integers and stores them in a list for x in # computations # displays the results on the screen # call try_Again to give the user the opportunity...
C++ Please write a exmaple of class template RedBlackTree.h.(It must be template!). I do not mind...
C++ Please write a exmaple of class template RedBlackTree.h.(It must be template!). I do not mind about details but please include the basic functions that are necessary for a RedBlackTree.
please answer this in a simple python code 1. Write a Python program to construct the...
please answer this in a simple python code 1. Write a Python program to construct the following pattern (with alphabets in the reverse order). It will print the following if input is 5 that is, print z one time, y two times … v five times. The maximum value of n is 26. z yy xxx wwww vvvvvv
Write a template class Number with the following features Overload following operators for the template class...
Write a template class Number with the following features Overload following operators for the template class + - < > Overload << and >> operators for the ostream and istream against this class. Write a main function to demonstrate the functionality of each operator.
Write a template class Number with the following features Overload following operators for the template class...
Write a template class Number with the following features Overload following operators for the template class + - < > Overload << and >> operators for the ostream and istream against this class. Write a main function to demonstrate the functionality of each operator.
Description: Create a class in Python 3 named Animal that has specified attributes and methods. Purpose:...
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...
Using python class: Design a class called Account CLASS NAME:    Account ATTRIBUTES: -nextAcctID: int    #NOTE-...
Using python class: Design a class called Account CLASS NAME:    Account ATTRIBUTES: -nextAcctID: int    #NOTE- class-level attribute initialized to 1000 -acctID: int -bank: String -acctType: String (ex: checking, savings) -balance: Float METHODS: <<Constructor>>Account(id:int, bank: String, type:String, bal:float) +getAcctID(void): int                        NOTE: retrieving a CLASS-LEVEL attribute! -setAcctID(newID: int): void           NOTE: PRIVATE method +getBank(void): String +setBank(newBank: String): void +getBalance(void): float +setBalance(newBal: float): void +str(void): String NOTE: Description: prints the information for the account one item per line. For example: Account #:        ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT