Exercise #6 in Software1

 

The purpose of this exercise is to practice the java collections framework, nested classes (iterators) and polymorphism.

 

"Old McDonald had a farm" Song:

Old MacDonald had a farm, E-I-E-I-O

And on his farm he had some chicks, E-I-E-I-O

With a cluck-cluck here and a cluck-cluck there

Here a cluck there a cluck

Everywhere a cluck-cluck

Old MacDonald had a farm, E-I-E-I-O

 

Old MacDonald had a farm, E-I-E-I-O

And on his farm he had some cows, E-I-E-I-O

With a moo-moo here and a moo-moo there

Here a moo there a moo

Everywhere a moo-moo

With a cluck-cluck here and a cluck-cluck there

Here a cluck there a cluck

Everywhere a cluck-cluck

Old MacDonald had a farm, E-I-E-I-O

 

Old MacDonald had a farm, E-I-E-I-O

And on his farm he had some dogs, E-I-E-I-O

With a woof-woof here and a woof-woof there

Here a woof there a woof

Everywhere a woof-woof

With a moo-moo here and a moo-moo there

Here a moo there a moo

Everywhere a moo-moo

With a cluck-cluck here and a cluck-cluck there

Here a cluck there a cluck

Everywhere a cluck-cluck

Old MacDonald had a farm, E-I-E-I-O

 


 

"Old MacDonald's farm" application requirements:

Old MacDonald's farm may contain: dogs, cows, pigs, chicks and horses. In this exercise you will write an application that receives as an input a list of animals in old MacDonald's farm (with possible repetitions). The application prints:

 

Requirement 1.                   The list of animals in old MacDonald's farm with their sounds. The order of the animals in this list is exactly the order in the input list.  For example: if the input was "cow pig chick chick cow" then the output is

     

cow: moo

pig: oink

chick: cluck

chick: cluck

cow: moo

    

Requirement 2.                   The status of old MacDonald's farm: a table of two columns where the first column contains animal names (no repetitions!!!) in alphabetical order, and the second columns contains the number of animals of this type in old MacDonald's farm.  For example: if the input was "cow pig chick chick cow" then the output is:

 

Animal      Count

------      -----

chick       2

cow         2

pig         1

 

Requirement 3.                   The "old MacDonald's had a farm" for the animals in the farm.  For every animal the line "And on his farm he had some " appears at most once. The order of appearance of the animals in the song is as the order of appearance in the input list.  For example: if the input was "cow pig chick chick cow" then the output is:

 

Old MacDonald had a farm, E-I-E-I-O

And on his farm he had some cows, E-I-E-I-O

With a moo-moo here and a moo-moo there

Here a moo there a moo

Everywhere a moo-moo

Old MacDonald had a farm, E-I-E-I-O

 

Old MacDonald had a farm, E-I-E-I-O

And on his farm he had some pigs, E-I-E-I-O

With an oink-oink here and an oink-oink there

Here an oink there an oink

Everywhere an oink-oink

With a moo-moo here and a moo-moo there

Here a moo there a moo

Everywhere a moo-moo

Old MacDonald had a farm, E-I-E-I-O

 

Old MacDonald had a farm, E-I-E-I-O

And on his farm he had some chicks, E-I-E-I-O

With a cluck-cluck here and a cluck-cluck there

Here a cluck there a cluck

Everywhere a cluck-cluck

With an oink-oink here and an oink-oink there

Here an oink there an oink

Everywhere an oink-oink

With a moo-moo here and a moo-moo there

Here a moo there a moo

Everywhere a moo-moo

Old MacDonald had a farm, E-I-E-I-O

 

"Old MacDonald's farm" application design (class diagram):

 

 

 

 

 

The classes: Farm, FarmBuilder and Song belong to package oldMac.

The interface IAnimal and its implementing classes: Pig, Cow, Horse, Chick and Dog belong to package oldMac.animals.

The class Main (not shown in the diagram) is the entry point of the application (i.e. its main method should be run).

Main and all the classes in oldMac.animals are implemented fully (i.e. no change should be done there).

 

The skeleton of the "Old MacDonald's farm" application is given in oldMac.zip.

Your job is to implement the following missing methods in the classes FarmBuilder, Farm and Song:

§         Missing methods in FarmBuilder:

o       public Farm buildFarm(String[] animalNames): receives a list of animal names, builds and returns a new Farm object with these animals.

§         Missing methods in Farm:

o       public void addAnimal(IAnimal animal): adds a new animal into the farm.

o       public Iterator<IAnimal>  iterator(): returns an iterator of all the animal in the farm. The iterator iterates the animals in the farm by the order of their addition to the farm.  

o       public Iterator<IAnimal> iteratorUnique(): returns an iterator of the animals in the farm – without repetitions.  The iterator iterates the animals in the farm by the order of their addition to the farm.   For example if the animals added to the farm were: cow, pig, chick, chick, cow (with this order), then the iterator will iterate cow, pig, chick (with this order).

o       public void printStatus(): prints the status of the farm as elaborated in Requirement 2.

§         Missing methods in Song:

o       public static void printSong(Farm farm): prints the "Old MacDonald had a farm" song as elaborated in Requirement 3.

 

You can add any necessary methods and fields to these three classes (do not change any other class). In your implementation you should use classes (and interfaces) from the java framework collections.

 

You may assume that:

§         The list of arguments to the application is not empty and that every argument is one of the following: "cow", "chick", "horse","dog" or "pig".

§         The method "Iterator.remove" is never called for any of the two iterators of Farm.

 

 

Good Luck!