patternphpModerate
Animal choir simulator
Viewed 0 times
choirsimulatoranimal
Problem
I got a PHP developer interview test to solve for a company. I didn't get the job but I would like to know where I was wrong. Did I understand the test completely?
```
/**
* Create an Animal Choir simulator
*
* The task constraints are:
*
* There must be 3 different choir member animals
* (i.e. dogs, cats, mice)
*
* Every animal must have a sing method that returns a string representation of a voice
* (i.e. 'bark', 'meow', 'squeak')
*
* Every animal must have a loudness property which can have 3 settings,
* depending on which the sing method result will be rendered as
* lowercase, first letter uppercase and uppercase.
*
* Singer groups are groups of animals with the same loudness property value.
* Singer group song is represented with a CSV of the group singer sing result in random order.
*
* The choir simulator must have implement the following methods:
* crescendo - the choir start singing from the least loud singer group, and then are being joined
* by more and more loud singer groups until they are singing all together.
* The joining is represented with a new line.
* Example:
* meow, squeak, bark
* Meow, bark, squeak, Bark, meow
* bark, Meow, MEOW, squeak, BARK, meow, Bark
*
* arpeggio - the choir singer groups of the same loudness start singing one by one from
* the least loud to the loudest
* Example:
* meow, squeak, bark
* Meow, Bark
* MEOW, BARK
*
*/
//TODO: Describe the class hierarchy
//Choir class
class Choir{
public $line_ending = '';
public $line_separator = '';
public $voices = '';
public function crescendo(){
$crescendo_song = '';
$animals = new Animal();
//We define which animal voices will be included in song
$animals->voices = $this->voices;
//Fir
```
/**
* Create an Animal Choir simulator
*
* The task constraints are:
*
* There must be 3 different choir member animals
* (i.e. dogs, cats, mice)
*
* Every animal must have a sing method that returns a string representation of a voice
* (i.e. 'bark', 'meow', 'squeak')
*
* Every animal must have a loudness property which can have 3 settings,
* depending on which the sing method result will be rendered as
* lowercase, first letter uppercase and uppercase.
*
* Singer groups are groups of animals with the same loudness property value.
* Singer group song is represented with a CSV of the group singer sing result in random order.
*
* The choir simulator must have implement the following methods:
* crescendo - the choir start singing from the least loud singer group, and then are being joined
* by more and more loud singer groups until they are singing all together.
* The joining is represented with a new line.
* Example:
* meow, squeak, bark
* Meow, bark, squeak, Bark, meow
* bark, Meow, MEOW, squeak, BARK, meow, Bark
*
* arpeggio - the choir singer groups of the same loudness start singing one by one from
* the least loud to the loudest
* Example:
* meow, squeak, bark
* Meow, Bark
* MEOW, BARK
*
*/
//TODO: Describe the class hierarchy
//Choir class
class Choir{
public $line_ending = '';
public $line_separator = '';
public $voices = '';
public function crescendo(){
$crescendo_song = '';
$animals = new Animal();
//We define which animal voices will be included in song
$animals->voices = $this->voices;
//Fir
Solution
Let's start with class hierarchy. The choir is made up of the animals.
The
Then each animal has a different way of singing but they all sing . That is classic polymorphism. (In fact they sing in such a similar way that the
For a discussion about the value of getter/setters in PHP (coming from a C++/Python background I'm pleased never having to do PHP OOP).
Now for some (e.g. I didn't check for bugs) low-level criticism:
The
Choir class should have a list of its Animal members:$felix = new Cat();
$rex = new Dog();
$choir = new Choir(array($felix, $rex, $bernand));Then each animal has a different way of singing but they all sing . That is classic polymorphism. (In fact they sing in such a similar way that the
sing() method goes into the Animal class).class Dog extends Animal {
public function __construct() {
$noise = 'bark';
....
class Animal {
private $loudness;
private $loudnessLevels = array('silent', 'normal', 'screaming');
private $noise;
public function __set($name, $value) {
if($name == 'loudness') {
if(!in_array($value, $loudnessLevels) {
throw new Excpetion('Unknown loudness');
}
$loudness = $value;
...For a discussion about the value of getter/setters in PHP (coming from a C++/Python background I'm pleased never having to do PHP OOP).
Now for some (e.g. I didn't check for bugs) low-level criticism:
- Use getters and setters
- In your
sing()function there is the default case - you just output the string in this case but actually it is an undefined case and should throw an exception.
Code Snippets
$felix = new Cat();
$rex = new Dog();
$choir = new Choir(array($felix, $rex, $bernand));class Dog extends Animal {
public function __construct() {
$noise = 'bark';
....
class Animal {
private $loudness;
private $loudnessLevels = array('silent', 'normal', 'screaming');
private $noise;
public function __set($name, $value) {
if($name == 'loudness') {
if(!in_array($value, $loudnessLevels) {
throw new Excpetion('Unknown loudness');
}
$loudness = $value;
...Context
StackExchange Code Review Q#57789, answer score: 13
Revisions (0)
No revisions yet.