Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

Code Challenge Java

Hi. I have to complete a code challenge in Java to demonstrate OOP. Can anyone please explain what the code should look like or possibly provide a solution? Any assistance on the matter is highly appreciated :) Thank you. The following is the code challenge:

Humans have a name and a Pet. Humans have two important instance methods: makePetMakeNoise: this method calls makeNoise on the Human's pet and passes a random integer as a parameter feedPet: this method calls the eat method of the Human's pet

Humans also have a class method called populationCount that will return the total amount of Humans created (hint: use a class variable to implement this!)

Pets have a name and a noise stored as strings as well as a boolean canMakeNoise. Our program has two types of Pets: Cats and Dogs. They also have a makeNoise method that accepts a parameter number that prints their noise along with their name to the console number times unless they can't make a noise, in which case they print "(pet's name) remains silent" to the console.

Finally all Pets have an eat method. When a Pet eats it prints "(pet's name) is eating...". A cat will additionally print "I'm still hungry, meow".

Create a program that creates multiple Humans and creates a Pet for each of them. Make sure to create at least one of each type of Pet. There should be at least one Pet for which canMakeNoise is set to FALSE.

Store these multiple Humans in a list and iterate through this list once, calling makePetMakeNoise and feedPet on each Human. After iterating through the list print the total population of Humans to the console.

As a result you should see each of these messages at least once in the console:

  1. "(pet's name) is eating"
  2. "I'm still hungry, meow"
  3. "(pet's name) remains silent"
  4. "(pet's name) (pet's noise)"
  5. "The total number of humans is: (populationSize)"

1 Answer

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Hema;

This looks/sounds a lot like homework or a job interview "test". If either is the case that is not really the purpose of the forum.

Ken