Exercise #9 in Software1
The purpose of this exercise is to practice the java collections framework, nested classes (iterators) and polymorphism.
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
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:
- 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:
- public
void addAnimal(IAnimal animal):
adds a new animal into the farm.
- 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.
- 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).
- public void printStatus(): prints the status of the farm as elaborated in Requirement 2.
* Missing methods in Song:
- 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:
חשוב!! ניתן להוריד קבצי שלד
כאן |
ניתן לכם מנשק SortedSet המייצג אוסף של עצמים ממוינים
ושונים אחד מן השני. מאחר והעצמים נשמרים בצורה ממוינת במימושים של מנשק זה,
איטרטור על האוסף יחזיר אותם אחד-אחד בסדר עולה.
בנוסף ניתן לכם מימוש פשוט של המנשק, מחלקה בשם SimpleSortedSet.
מחלקה זו תשמש אתכם אך ורק כדוגמה לצרכי תכנית הבדיקה המצורפת ושאר בדיקות שתרצו
לעשות על המימושים שלכם.
חשוב לשים לב כי כל ה-Sets שנתעסק איתם הינם בלתי
ניתנים לשינוי, ז"א שהעצמים בתוכם נקבעים בעת היצירה. לא ניתן להוסיף/להוריד
עצמים לאחר מכן מן האוספים. תכונה זו תאפשר לכם לשמור תוצאות של חישובים ארוכים
יחסית (ערך מקס', ערך מינ', גודל) ולא לחשב אותם כל פעם מחדש.
שימו לב לאיטרטור SortedIterator,
הרחבה ריקה של Iterator
שמסמלת כי האיטרטור מחזיר ערכים ממוינים.
א. CompositeSortedSet – בשלב זה תתכננו
ותממשו מחלקה מופשטת. בשלבים הבאים נרחיב מחלקה זו.
מחלקה מופשטת זו הינה אוסף שהוא תוצר של שני אוספים ממוינים (כפי שתראו בהמשך, תוצר כגון איחוד או חיתוך). יש להכניס במחלקה זו את כל הקוד המשותף שניתן. השירותים אותם יש לייצג במחלקה זו הינם:
ב. המחלקה MergedSortedSet שנבנית באמצעות SortedSet A ו-SortedSet B מכילה רק עצמים בחיתוך
של A ו-B (ז"א עצמים הנמצאים
בשניהם). עבור מחלקה זו עליכם לממש:
יצירת איטרטור צריכה להראות כך:
/** *
Returns an Iterator for the elements of this set. */ public
SortedIterator iterator() { return new MergedSortedIterator(setA.iterator(),
setB.iterator()); } |
כמובן שתצטרכו לממש גם את MergedSortedIterator המרחיב את SortedIterator
ג. המחלקה DifferenceSortedSet שנבנית באמצעות SortedSet A ו-SortedSet B מכילה רק עצמים שנמצאים
ב-A אך
לא נמצאים ב-B. ממשו
מחלקה זו (ואת האיטרטור המתאים לה) כמחלקה מרחיבה ל-CompositeSortedSet.
Good Luck!