Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Objectives : Complete a simple (silly) class, with a constructor setting attributes, and methods, including a __str__ method, and separate testing code. Complete the simple

Objectives: Complete a simple (silly) class, with a constructor setting attributes, and methods, including a __str__ method, and separate testing code.

  1. Complete the simple class Animal. The bullets below name and describe the instance variables, constructor, and methods you need to write. What parameter(s) are needed for each of these methods?

    • An Animal has a name and a gut. In our version the gut is a list of strings describing the contents, in the order eaten. A newly created Animal gets a name from a parameter passed to the constructor, while the gut always starts off empty.

    • An Animal has a greet method, so an animal named "Froggy" would say (that is, print)

      Hello, my name is Froggy. 
    • An Animal can eat a string naming the food, adding the food to the gut. If Froggy eats "worm" and then "fly", its gut then has value ['worm', 'fly']. .

    • An Animal can excrete (removing and printing what was first in the gut List). Recall the method pop (second version) in Mutating Methods. Print the empty string, "", if the gut was already empty. Following the Froggy example above, Froggy could excrete, and "worm" would be printed. Then its gut would then have value ['fly'].

    • A __str__ method: Make it return a string in the format shown below for Froggy, including the Animal's name:

      "Animal: Froggy" 

      Try this first, and note the elaborated version below.

  2. Add code outside the class, testing it: Create a couple of Animals and visibly test all the methods, with enough explanation that someone running the test program, but not looking at the code, can see that everything works.

  3. Possible elaboration: Modify __str__ so if Froggy had "worm", "fly" and "bug" in the gut, the string would be:

    "Animal: Froggy is digesting worm, fly and bug." 

    with a comma separated list of the gut contents, except use proper English, so the last separator is " and ", not ", ". If the gut has nothing in it, list the contents as "nothing":

    "Animal: Froggy is digesting nothing" 

    Extend your tests if necessary.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

How would we like to see ourselves?

Answered: 1 week ago