Objectives: Complete a simple (silly) class, with constructor and methods, including a ToString method, and a separate testing class.
Make an animal_lab project in your solution, and copy in the files from the example project animal_lab_stub. Then modify the two files as discussed below.
Complete the simple class Animal in your copy of the file animal.cs. The bullets below name and describe the instance variables, constructor, and methods you need to write:
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 list contains worm and fly.
An Animal can Excrete (removing and printing what was first in the gut List). Recall the method RemoveAt in List Syntax. 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 contain only fly.
A ToString method: Remember the override keyword. Make it return a string in the format shown below for Froggy, including the Animals name: Animal: Froggy
Try this first, and note the extra credit version below.
All the methods that print should be void. Which need a parameter, of what type?
Complete the file test_animal.cs with its class TestAnimal containing the Main method, testing the class Animal: 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 of either file, can see that everything works.
EXTRA CREDIT: Elaborate ToString so if Froggy had worm, fly and bug in the gut, the string would be: Animal: Froggy ate 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 ate nothing
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference
and should not be submitted as is. We are not held liable for any misuse of the solutions.
Please see the frequently asked questions page
for further questions and inquiries.
Kindly complete the form.
Please provide a valid email address and we will get back to you within 24 hours.
Payment is through PayPal, Buy me a Coffee
or Cryptocurrency.
We are a nonprofit organization however we need funds to keep this organization operating
and to be able to complete our research and development projects.