Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2014
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.55 KB | None | 0 0
  1. /**
  2. * Create an Animal Choir simulator
  3. *
  4. * The task constraints are:
  5. *
  6. * There must be 3 different choir member animals
  7. * (i.e. dogs, cats, mice)
  8. *
  9. * Every animal must have a sing method that returns a string representation of a voice
  10. * (i.e. 'bark', 'meow', 'squeak')
  11. *
  12. * Every animal must have a loudness property which can have 3 settings,
  13. * depending on which the sing method result will be rendered as
  14. * lowercase, first letter uppercase and uppercase.
  15. *
  16. * Singer groups are groups of animals with the same loudness property value.
  17. * Singer group song is represented with a CSV of the group singer sing result in random order.
  18. *
  19. * The choir simulator must have implement the following methods:
  20. * crescendo - the choir start singing from the least loud singer group, and then are being joined
  21. * by more and more loud singer groups until they are singing all together.
  22. * The joining is represented with a new line.
  23. * Example:
  24. * meow, squeak, bark
  25. * Meow, bark, squeak, Bark, meow
  26. * bark, Meow, MEOW, squeak, BARK, meow, Bark
  27. *
  28. * arpeggio - the choir singer groups of the same loudness start singing one by one from
  29. * the least loud to the loudest
  30. * Example:
  31. * meow, squeak, bark
  32. * Meow, Bark
  33. * MEOW, BARK
  34. *
  35. */
  36.  
  37. //TODO: Describe the class hierarchy
  38.  
  39.  
  40. //Choir class
  41. class Choir{
  42.  
  43. public $line_ending = '';
  44. public $line_separator = '';
  45. public $voices = '';
  46.  
  47. public function crescendo(){
  48. $crescendo_song = '';
  49.  
  50. $animals = new Animal();
  51. //We define which animal voices will be included in song
  52. $animals->voices = $this->voices;
  53.  
  54. //First we start with the silent
  55. $animals->loudness = 'silent';
  56. //Call the function
  57. $silent = $animals->sing();
  58. //echo the result
  59. $crescendo_song .= $this->stringForm($silent);
  60.  
  61. //Then we continue with the normal, but also merge with silent
  62. $animals->loudness = 'normal';
  63. $normal = array_merge($animals->sing(),$silent);
  64. $crescendo_song .= $this->stringForm($normal);
  65.  
  66. //And in the end we merge loud with silent and normal
  67. $animals->loudness = 'loud';
  68. $loud = array_merge($animals->sing(),$silent,$normal);
  69. $crescendo_song .= $this->stringForm($loud);
  70.  
  71. return $crescendo_song;
  72. }
  73.  
  74.  
  75. public function arpeggio(){
  76. $arpeggio_song = '';
  77.  
  78. $animals = new Animal();
  79. //We define which animal voices will be included in song
  80. $animals->voices = $this->voices;
  81.  
  82. //First we start with the silent
  83. $animals->loudness = 'silent';
  84. $arpeggio_song .= $this->stringForm($animals->sing());
  85.  
  86. //Then normal
  87. $animals->loudness = 'normal';
  88. $arpeggio_song .= $this->stringForm($animals->sing());
  89.  
  90. //And then the loud
  91. $animals->loudness = 'loud';
  92. $arpeggio_song .= $this->stringForm($animals->sing());
  93.  
  94. return $arpeggio_song;
  95.  
  96. }
  97.  
  98.  
  99. private function stringForm($array){
  100. //Randomize the array
  101. shuffle($array);
  102. //Predefine a song and a separator
  103. $song = '';
  104. $comma = '';
  105. //Form song string
  106. foreach ($array as $slog) {
  107. $song .= $comma.$slog;
  108. $comma = $this->line_separator.' ';
  109. };
  110. return $song.$this->line_ending;
  111. }
  112.  
  113.  
  114. }
  115.  
  116. //Animal class
  117. class Animal{
  118. public $loudness = '';
  119.  
  120. public function sing(){
  121. $song = array();
  122. foreach ($this->voices as $voices) {
  123. switch ($this->loudness) {
  124. case 'silent':
  125. array_push($song,strtolower($voices));
  126. break;
  127. case 'normal':
  128. array_push($song,ucfirst($voices));
  129. break;
  130. case 'loud':
  131. array_push($song,strtoupper($voices));
  132. break;
  133. default:
  134. array_push($song,$voices);
  135. break;
  136. }
  137. }
  138. return $song;
  139. }
  140.  
  141. }
  142.  
  143.  
  144. $choir = new Choir();
  145. $choir->line_ending = PHP_EOL;
  146. //$choir->line_ending = '<br>'; //For cleared viewing in browser use <br> tag
  147. //Define the line separator for CSV
  148. $choir->line_separator = ',';
  149. //Define the voices of animals
  150. $choir->voices = array('bark','meow','squeak');
  151. //Call and echo the functions
  152. echo $choir->crescendo();
  153. echo $choir->arpeggio();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement