Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Part 1 - Class file Create a class called Animal with the following fields: type - to contain the name of the type of animal

Part 1 - Class file

Create a class called Animal with the following fields:

type - to contain the name of the type of animal this is (e.g. goat, duck, cow, etc.)

food- to store the animals current food level. This should start at 0.

hunger - an array with the size of 3 to contain the hunger level of the animal. The values for this array should be 1,4,6.

traits - an array with the size of 3 to contain a text description of traits the animal might have. (e.g. large, hairy, cute, etc.)

currentTrait - an int that contains the index of the trait the animal has now. It should start at 0.

friend - to contain an Animal object that is a friend to this animal.

Create a method called eat that takes an int as a parameter and increases the food level of the animal by the number passed. This method should also print that the animal of its trait eats the number of food passed and mutates! The format should be:

The [trait] [type] eats [number] food and mutates!

(e.g. The small fish eats 2 food and mutates!)

Use the currentTrait field to decide the index of the trait to print.

After the animal eats, add one to currentTrait.

If currentTrait is now greater than 2, reset it to 0;

Create a method called hasFriend that returns true or false. If this animal has a friend, have this method print that this trait animal has a trait friend before returning true. It should be in this format:

The [trait] [ type ] has a [friend trait] [friend type] friend.

(e.g. The weak cow has a ripped snake friend.)

Part 2 - Client code

Create a class called Barn.

In the main method create an Animal object.

Assign it any type you want.

Assign any descriptions to all values of the animals trait array.

Create a second Animal object.

Assign the second animal any type, and traits you want.

Make the first Animal a friend of the second Animal.

Call the hasFriend method of the second Animal, and if it returns true, print Friends are great!, otherwise print The animal is lonely..

Write a loop that will loop 4 times and does the following:

then call the eat() method on the animal, passing it the value 2.

*** JAVA *** TWO SEPARATE QUESTIONS! Thank you in advance :)

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

Did Stages have a duty to protect Osborne from Trujillos attack?

Answered: 1 week ago