0

What would be the best way to approach this problem?

We have a sheep farm, with two wool collector machines.

  • The sheep have the following attributes: Age: Young, and Old. Color: Black and White.

    Both age and color values are random.

  • Both machines only collect wool if the sheep are old. If not will echo:

This sheep is not ready to be sheared.

  • The animals should be processed in groups, for example 20 sheep in a row. And there should be a counter of total wool collected in each machine, both black & white.

I just need some guidance with syntaxis to get started. Thanks!

4
  • Also I'm not sure how to work with those random values, should I make them before the function starts, or inside the array? Commented Jul 15, 2011 at 2:52
  • 4
    do your own homework assignments. Commented Jul 15, 2011 at 2:52
  • If your doing homework tag it as homework and state it in your question. Commented Jul 15, 2011 at 2:54
  • Besides the homework sign, you in fact need to approach this problem with object-oriented programming logic? the question is poor of details yet Commented Jul 15, 2011 at 3:10

4 Answers 4

2

Sheep and machines are separate objects. Here's a start:

class Sheep{

    const COLOR_WHITE = 'white';
    const COLOR_BLACK = 'black';
    const AGE_YOUNG = 0;
    const AGE_OLD = 1;

    private $_color;
    private $_age;

    public static function makeRandom(){
        $color = rand(0, 1)
            ? self::COLOR_WHITE
            : self::COLOR_BLACK;
        $age = rand(0, 1);
        return new self($color, $age);
    }

    public function __construct($color, $age){
        $this->_color = $color;
        $this->_age = $age;
    }

}

$sheep = Sheep::makeRandom();

Let us know where you're at further on.


Swapping out the ternary operator:

// ...
if(rand(0, 1)){
    $color = self::COLOR_WHITE;
}else{
    $color = self::COLOR_BLACK;
{
// ...
Sign up to request clarification or add additional context in comments.

2 Comments

Looks quite clear to me, except for the statement: >? self::COLOR_WHITE >: self::COLOR_BLACK; Why there is ? in the first one and a : in the second.
@Gabriel - Its a ternary operator, see ca.php.net/ternary. You can just as easily swap it out for if/else, I'll update my answer accordingly. Basically if the condition provided (in this case rand(0, 1) evaluates to true it returns the value after ?, otherwise the value after ':', in this case storing it in the variable $color.
1

Well, there's a few things to thing about here, but you know that you need $age and $color properties, and ways to read those properties. Chances are, you don't want to be able to write them though.

So, I would probably have:

getAge(){ return $this->age; }
getColor(){ return $this->color; }

Now, you want to assign the color and age randomly, which means you need the rand function (there are other options there, but rand will do you well). Now, if I were to do this, I would put something like this in the constructor:

// assuming we're testing for male or female
// you really should look into why this works.
$this->gender = ( rand( 0, 1 ) )? self::MALE: self::FEMALE;
// notice the self::MALE and self::FEMALE? Those are class constants.
// http://php.net/manual/en/language.oop5.constants.php
// if you want to get this question *right* you'll need to look at those

Your machines are actually pretty simple. They only test whether each sheep is old enough to be shorn and then they increment a counter based on that.

// assuming they are looking for a count of female sheep
// and state variables 'male' and 'female' which are initialized at 0
function processSheep( $sheep )
{
    foreach( $sheep as $test )// stupid self-pluralizing nouns.
    {
        if( $sheep->getGender() == Sheep::MALE ) $this->males++;
        else $this->females++; // obviously, you're going to need to swap
                               // out one of these increments for an echo.
    }
}

function getNumberOfMales(){ return $this->males; }

With two machines, to calculate the number of males:

$mach1->getNumberOfMales() + $mach2->getNumberOfMales();

With an array of n machines, the number of males:

$males = 0;
foreach( $machs as $mach ){ $males += $mach->getNumberOfMales(); }

Comments

0

What types of things are there in your program? Sheep and machines.

Should sheep and machines be the same class? Well, do they share the same attributes? Do machines have age? No. Do machines have color? No. Then they're not the same class.

Make a sheep class, give it age and color properties. The class constructor should randomly assign the values of the age and color properties so they're set when each sheep object is created.

Make a machine class. It needs properties to hold how much black wool and how much white wool it has collected.

Create setters and getters for the properties in both classes.

Now write the program which creates the sheep and machine objects, and performs the required task.

Comments

0

Expanding on TomcatExodus:

<?php 

class Sheep{

    const COLOR_WHITE = 'white';
    const COLOR_BLACK = 'black';
    const AGE_YOUNG = 0;
    const AGE_OLD = 1;

    private $_color;
    private $_age;
    private $_sheered;

    public function makeRandom() {
        $this->_color = rand(0, 1)
            ? self::COLOR_WHITE
            : self::COLOR_BLACK;
        $this->_age = rand(0, 1);
    }

    public function __construct(){
        $this->makeRandom();
    }

    public function sheer() {
        if($this->_age == 0) {
            $this->_sheered['This sheep is not ready to be sheared.'] = $this->_sheered['This sheep is not ready to be sheared.'] + 1;
        } else {
            if($this->_color == 'black') {
                $this->_sheered['black'] = $this->_sheered['black'] + 1;
            } else {
                $this->_sheered['white'] = $this->_sheered['white'] + 1;
            }
        }
    }

    public function results() {
        return print_r($this->_sheered,true);
    }
}

$batch = 20;
$sheep = new Sheep();

while($batch > 0) {
    $sheep->makeRandom();
    $sheep->sheer();
    $batch--;
}
echo $sheep->results()."\n";

?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.