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...

  • ***IN PYTHON 2.7****Augment the following code with a new class named 'Coin'. Coin should inherit from...

    ***IN PYTHON 2.7****Augment the following code with a new class named 'Coin'. Coin should inherit from Die, with the following modifications; The constructor should not take any arguments; a coin always has two sides add a flip() method that uses the roll() method from the parent class. If roll returns 1; flip should return "HEADS". If roll returns a 2, flip should return "TAILS" Do not override the roll or rollMultiple methods from the parent class #!/usr/bin/python # your class...

  • 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...

  • PYTHON 3.6 Overview In this assignment we implement a class called TripleString. It consists of a...

    PYTHON 3.6 Overview In this assignment we implement a class called TripleString. It consists of a few instance attribute and a few instance methods to support those attributes. In the next assignment, the TripleString class will help us create a more involved application. Before we do that though, we have to thoroughly test our TripleString implementation. Specifications The class TripleString will contain symbolic constants, instance attributes, and instance methods. ▶ Class symbolic constants This class has 3 symbolic constants, which...

  • Objectives: Develop an object-oriented program Develop a test program that is separate from the class under...

    Objectives: Develop an object-oriented program Develop a test program that is separate from the class under development ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Define a class where one object of the class represents a playing card. If you are not familiar with a deck of playing cards, you will need to spend some time understanding the rank and suit that is on each playing card. You can look it up in wikipedia and see an image of the whole deck of 52 playing cards here:...

  • 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...

  • 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...

  • 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...

  • Write a program that does the following in Python Code: Write a new Class called Famous_Day_Born...

    Write a program that does the following in Python Code: Write a new Class called Famous_Day_Born which is a subclass of Famous_Person. Add a method to calculate the day of the week the person was born and print out all of the corresponding information using the overridden print method. Use the following code below as a starting point. ////////////////////////////////////////////////////// from datetime import datetime class Famous_Person(object): def __init__(self, last, first, month,day, year): self.last = last self.first = first self.month = month...

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