Question

Python3 : question about object-oriented programming, Please write program in the template(critter.py),specific information is on the graphs

the missing words in the 4th line are 'To save an attribute, attach it to this self keyword'

W11 - Critters Implement the Critter class. Each critter C has attributes species, size, and age The constructor accepts arguwritten in this function. Things to Remember Some notes about classes and how class syntax works in Python: Python 3 1 class

+run.py やcrítter.py 1 # TODO: Write your code up here! 2 3 4 # This wiu not be tested : 5 # (It s j ust for you to test your

a + ф run.py critter.py 1 #### You can igno re this file ### 2 # (Its just here so the Run button on Ed wiïï wo rk) 3 4 fr

W11 - Critters Implement the Critter class. Each critter C has attributes species, size, and age The constructor accepts arguments for the attributes above, in the order above. You can expect size and age to be numeric values Each critter C has a can_eat) method, which: Receives one argument, prey, which you can expect to be another Critter object. Compares prey's size against C's o If C is larger than prey, return True. Otherwise, return False. Each critter C also has a survive_year) method, which: Increases C's size and age by 1, respectively, and returns C's current size. Testing Your Code You can test your function by putting code inside test(). We will not examine any code
written in this function. Things to Remember Some notes about classes and how class syntax works in Python: Python 3 1 class NameOfClass: 2 def_init_(self, argument1): 3 4 5 6 7 8 9 # This is the constructor. self.var"To save an attribute, attach it to this self 6 def instance method (self, something): # This is an instance method. It uses the self keyword. pass def class method: # This is a class method. It does not use the self keywor pass 12
+run.py やcrítter.py 1 # TODO: Write your code up here! 2 3 4 # This wiu not be tested : 5 # (It' s j ust for you to test your own code) 6 def test): 7 8 centipede Critter("centipede", 3, 1) # You can use this function to test your code. milli pede : Critter("millipede", 3, 2) spider Critter("Orb Weaver", 50, 1) 10 12 print (millipede.can_eat(centipede)) 13 print (spider.can_eat(millipede)) 14 15 print(spider.age) 16 print (spider.survive year)) 17
a + ф run.py critter.py 1 #### You can igno re this file ### 2 # (It's just here so the "Run" button on Ed wiïï wo rk) 3 4 from critter import test 5 test()
0 0
Add a comment Improve this question Transcribed image text
Answer #1

# TEXT CODE FOR critter.py

# defining Critter class
class Critter:

   # defining constructor method
   def __init__(self, species, size, age):
      
       # set attribute species to parameter attribute
       self.species = species

       # set attribute size to parameter size
       self.size = size

       # set attribute age to parameter age
       self.age = age

   # defining can_eat() method
   def can_eat(self, prey):

       # if size of Critter is greater then prey return true.
       if self.size > prey.size:
           return True

       # Otherwise return False
       else: return False

   # defining survive_year() method
   def survive_year(self):

       # increase the size and age by 1
       self.size = self.size + 1
       self.age = self.age + 1

       # return the current size
       return self.size


# defining test() method to test the Critter class
def test():

   # creating objects of Critter class
   centipede = Critter("centipede", 3, 1)
   millipede = Critter("millipede", 3, 1)
   spider = Critter("Orb Weaver", 50, 1)

   # check whether millipede can eat centipede or not by calling can_eat() method of critter
   print("millipede can " , end = "")
   if millipede.can_eat(centipede):
       print("eat centipede")

   else: print("not eat centipede")


   # check whether spider can eat millipede or not
   print("spider can " , end = "")
   if spider.can_eat(millipede):
       print("eat millipede")

   else: print("not eat millipede")

   # print the age of spider
   print("Age of spider is: ", spider.age)

   # print the current age of spider by calling survive_year() method
   print("Current age of spider is: ", spider.survive_year())

# TEXT CODE FOR run.py

# importing test function from critter
from critter import test

# call test() method
test()

SCREENSHOTS

critter.py

1 # defining Critter class class Critter # defining constructor method 4 5 6 7 8 9 10 def _init_(self, species, size, age): #36 37 # defining test() method to test the Critter class 38 def test): 39 48 41 42 43 ating objects of Critter class # cre ce

run.py

1 importing test function from critter 2 from critter import test # call test() method test() 5

output (after running run.py):

millipede can not eat centipede spider can eat millipede Age of spider is: 1 Current age of spider is: 51

Add a comment
Know the answer?
Add Answer to:
Python3 : question about object-oriented programming, Please write program in the template(critter.py),specific information is on the graphs the missing words in the 4th line are 'To save an attri...
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
  • Write a python program using Object Oriented and do the following: 1. create class "cat" with...

    Write a python program using Object Oriented and do the following: 1. create class "cat" with the following properties: name, age (create constructor method: def __init__) 2. create class "adopter" with the following properties: name, phone 3. create class "transaction" with these properties: adopter, cat (above objects) cat1 = "puffy, 2" adopter1 = "Joe, 123" Test your program: Joe adopts puffy. Print: "Per Transaction 1 <joe> has adopted <puffy>" this can only be done with object oriented programming, no way...

  • This is my assignment in Python. Please help me thank you Design type 1: # Creating an object of ...

    This is my assignment in Python. Please help me thank you Design type 1: # Creating an object of the class "Burger" theOrder = Burger() # calling the main method theOrder.main() # And the class is like: class Burger: # You may need to have a constructor def __init__(self): self._orderDict = {} self._priceBeforeTax = 0 self._priceAfterTax = 0 # you may have the tax rate also as an instance variable. But as I mentioned, you can use your # own...

  • Java - Object Oriented Programming Declare a class named Customer that has two private fields? Write...

    Java - Object Oriented Programming Declare a class named Customer that has two private fields? Write a set method to make sure that if age > 125 years or less than 0 then it is set to 0 (default value) What does it indicate when declaring a class's instance variables with private access modifier? What is the role of a constructor? The data part of by an object's instance variables is known as? Do all methods have to always return...

  • It's my python homework. Not Java. Design type 1: # Creating an object of the class...

    It's my python homework. Not Java. Design type 1: # Creating an object of the class "DeAnzaBurger" theOrder = DeAnzaBurger() # calling the main method theOrder.main() # And the class is like: class DeAnzaBurger: # You may need to have a constructor def __init__(self): self._orderDict = {} self._priceBeforeTax = 0 self._priceAfterTax = 0 # you may have the tax rate also as an instance variable. But as I mentioned, you can use your # own deign.    .... # That...

  • Write a C++ program for the instructions below. Please read the instructions carefully and make sure they are followed correctly.   and please put comment with code! Problem:2 1. Class Student Create...

    Write a C++ program for the instructions below. Please read the instructions carefully and make sure they are followed correctly.   and please put comment with code! Problem:2 1. Class Student Create a "Hello C++! I love CS52" Program 10 points Create a program that simply outputs the text Hello C++!I love CS52" when you run it. This can be done by using cout object in the main function. 2. Create a Class and an Object In the same file as...

  • Below is the Graph file that needs to be modified(using Python3) : #!/usr/bin/python3 # Simple Vertex...

    Below is the Graph file that needs to be modified(using Python3) : #!/usr/bin/python3 # Simple Vertex class class Vertex: """ Lightweight vertex structure for a graph. Vertices can have the following labels: UNEXPLORED VISITED Assuming the element of a vertex is string type """ __slots__ = '_element', '_label' def __init__(self, element, label="UNEXPLORED"): """ Constructor. """ self._element = element self._label = label def element(self): """ Return element associated with this vertex. """ return self._element def getLabel(self): """ Get label assigned to...

  • Python 3> Make a class called BMW: Parameterized constructor with three instance variables (attributes): Hint: def...

    Python 3> Make a class called BMW: Parameterized constructor with three instance variables (attributes): Hint: def __init__(self, make, model, year) Methods in BMW parent class (can leave as method stubs for now): startEngine() -must print "The engine is now on." stopEngine() -must print "The engine is now off." Make another class called ThreeSeries -Must inherit BMW Hint: class ThreeSeries(BMW): -Also passes the three attributes of the parent class and invoke parent Hint: BMW.__init__(self, cruiseControlEnabled, make, model, year) -Constructor must have...

  • 1. Extending the answer from Python HW #7 for question #1, write the following python code...

    1. Extending the answer from Python HW #7 for question #1, write the following python code to expand on the Car class. Include the python code you developed when answering HW #7. Add class “getter” methods to return the values for model, color, and MPG Add class “setter” methods to change the existing values for model, color, and MPG. The value to be used in the methods will be passed as an input argument. Add class method that will calculate...

  • Python3, write a program, specific information is in the graphs, I got 7 marks out of...

    Python3, write a program, specific information is in the graphs, I got 7 marks out of 10, you may change my codes to let it gives correct output,restrications are also on the graph, for loop, in key word, enumerate,zip,slices, with key word can't be used. All the information is on the graph, is_shakespeare_play(line) may be a function given that can check whether is Shakespeare play Question 2- 10 marks Write the function keep titles short (filename, max charactars) which takes...

  • This is the question about object-oriend programming(java) please show the detail comment and prefect code of...

    This is the question about object-oriend programming(java) please show the detail comment and prefect code of each class, Thank you! This question contain 7 parts(questions) Question 1 Create a class a class Cat with the following UML diagram: (the "-" means private , "+" means public) +-----------------------------------+ | Cat | +-----------------------------------+ | - name: String | | - weight: double | +-----------------------------------+ | + Cat(String name, double weight) | | + getName(): String | | + getWeight(): double | |...

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