One event occuring more than the other

For my exam on Java (i'm almost there), I got a simulation with Foxes & Rabbits. I've added Hares to this. Foxes eat the rabbits and hares, but the rabbits and hares breed faster again. It's going fine so far, the only thing that I need to implement is that a Fox eats rabbits more often than hares, and I have no clue on how to implement it. What I've thought of is to make an integer for both the rabbits and the hares, with a sort of attractiveness-rating, but then I got stuck on how to implement it.
Anyway, here are the codes:
import java.util.List;
import java.util.Random;
public class Rabbit extends PreyAnimal
    // Characteristics shared by all rabbits (static fields).
    // The age at which a rabbit can start to breed.
    // private static final int BREEDING_AGE = 5;
    // The age to which a rabbit can live.
    // private static final int MAX_AGE = 50;
    // The likelihood of a rabbit breeding.
    // private static final double BREEDING_PROBABILITY = 0.15;
    // The maximum number of births.
    private static final int MAX_LITTER_SIZE = 5;
    // A shared random number generator to control breeding.
     private static final Random rand = new Random();
     public static final int PREFERENCE = 6;
    // Individual characteristics (instance fields).
     * Create a new rabbit. A rabbit may be created with age
     * zero (a new born) or with a random age.
     * @param randomAge If true, the rabbit will have a random age.
    public Rabbit(boolean randomAge)
        super.incrementAge();
        if(randomAge) {
            setAge(rand.nextInt(MAX_AGE));
     * This is what the rabbit does most of the time - it runs
     * around. Sometimes it will breed or die of old age.
     * @param currentField The field currently occupied.
     * @param updatedField The field to transfer to.
     * @param newAnimals A list to add newly born rabbits to.
    public void act(Field currentField, Field updatedField, List newAnimals)
        super.incrementAge();
        if(isAlive()) {
            int births = breed();
            for(int b = 0; b < births; b++) {
                Rabbit newRabbit = new Rabbit(false);
                newAnimals.add(newRabbit);
                newRabbit.setLocation(
                        updatedField.randomAdjacentLocation(getLocation()));
                updatedField.place(newRabbit);
            Location newLocation = updatedField.freeAdjacentLocation(getLocation());
            // Only transfer to the updated field if there was a free location
            if(newLocation != null) {
                setLocation(newLocation);
                updatedField.place(this);
            else {
                // can neither move nor stay - overcrowding - all locations taken
                setDead();
     * Generate a number representing the number of births,
     * if it can breed.
     * @return The number of births (may be zero).
    private int breed()
        int births = 0;
        if(canBreed() && rand.nextDouble() <= BREEDING_PROBABILITY) {
            births = rand.nextInt(MAX_LITTER_SIZE) + 1;
        return births;
    public int getPreference()
        return PREFERENCE;
     * @return A string representation of the rabbit.
    public String toString()
        return "Rabbit, age " + getAge();
}The class Hare is nearly the same ofcourse.
Here's the fox, on where I got to implement the preference:
import java.util.List;
import java.util.Iterator;
import java.util.Random;
public class Fox extends Animal
    // Characteristics shared by all foxes (static fields).
    // The age at which a fox can start to breed.
    private static final int BREEDING_AGE = 10;
    // The age to which a fox can live.
    private static final int MAX_AGE = 150;
    // The likelihood of a fox breeding.
    private static final double BREEDING_PROBABILITY = 0.09;
    // The maximum number of births.
    private static final int MAX_LITTER_SIZE = 3;
    // The food value of a single rabbit. In effect, this is the
    // number of steps a fox can go before it has to eat again.
    private static final int RABBIT_FOOD_VALUE = 6;
    private static final int HARE_FOOD_VALUE = 4;
    // A shared random number generator to control breeding.
    private static final Random rand = new Random();
    // Individual characteristics (instance fields).
    // The fox's food level, which is increased by eating rabbits.
    private int foodLevel;
     * Create a fox. A fox can be created as a new born (age zero
     * and not hungry) or with random age.
     * @param randomAge If true, the fox will have random age and hunger level.
    public Fox(boolean randomAge)
        super();
        if(randomAge) {
            setAge(rand.nextInt(MAX_AGE));
            foodLevel = rand.nextInt(RABBIT_FOOD_VALUE);
        else {
            // leave age at 0
            foodLevel = RABBIT_FOOD_VALUE;   
     * This is what the fox does most of the time: it hunts for
     * rabbits. In the process, it might breed, die of hunger,
     * or die of old age.
     * @param currentField The field currently occupied.
     * @param updatedField The field to transfer to.
     * @param newAnimals A list to add newly born foxes to.
    public void act(Field currentField, Field updatedField, List newAnimals)
        incrementAge();
        incrementHunger();
        if(isAlive()) {
            // New foxes are born into adjacent locations.
            int births = breed();
            for(int b = 0; b < births; b++) {
                Fox newFox = new Fox(false);
                newAnimals.add(newFox);
                newFox.setLocation(
                        updatedField.randomAdjacentLocation(getLocation()));
                updatedField.place(newFox);
            // Move towards the source of food if found.
            Location newLocation = findFood(currentField, getLocation());
            if(newLocation == null) {  // no food found - move randomly
                newLocation = updatedField.freeAdjacentLocation(getLocation());
            if(newLocation != null) {
                setLocation(newLocation);
                updatedField.place(this);  // sets location
            else {
                // can neither move nor stay - overcrowding - all locations taken
                setDead();
     * Increase the age. This could result in the fox's death.
    private void incrementAge()
        setAge(getAge() + 1);
        if(getAge() > MAX_AGE) {
            setDead();
     * Make this fox more hungry. This could result in the fox's death.
    private void incrementHunger()
        foodLevel--;
        if(foodLevel <= 0) {
            setDead();
     * Tell the fox to look for rabbits adjacent to its current location.
     * @param field The field in which it must look.
     * @param location Where in the field it is located.
     * @return Where food was found, or null if it wasn't.
    private Location findFood(Field field, Location location)
        Iterator adjacentLocations =
                          field.adjacentLocations(location);
        while(adjacentLocations.hasNext()) {
            Location where = (Location) adjacentLocations.next();
            Object animal = field.getObjectAt(where);
            if(animal instanceof Rabbit) {
                Rabbit rabbit = (Rabbit) animal;
                if(rabbit.isAlive()) {
                    rabbit.setDead();
                    foodLevel = RABBIT_FOOD_VALUE;
                    return where;
            else if(animal instanceof Hare) {
            Hare hare = (Hare) animal;
            if(hare.isAlive()) {
                hare.setDead();
                foodLevel = HARE_FOOD_VALUE;
                return where;
    return null;
     * Generate a number representing the number of births,
     * if it can breed.
     * @return The number of births (may be zero).
    private int breed()
        int births = 0;
        if(canBreed() && rand.nextDouble() <= BREEDING_PROBABILITY) {
            births = rand.nextInt(MAX_LITTER_SIZE) + 1;
        return births;
     * @return A string representation of the fox.
    public String toString()
        return "Fox, age " + getAge();
     * A fox can breed if it has reached the breeding age.
    private boolean canBreed()
        return getAge() >= BREEDING_AGE;
}Is there anybody out there with a clue on how to do it? Or maybe with another solution than adding an integer? Would be much appreciated :)
Btw, the full code with all classes is availble @ http://members.home.nl/firehawk/S200.rar

dubwai offers a simple solution for you...this would be implemented in the findFood() method, i believe.
i would offer a suggest. implements to an interface...this will get rid of the instanceof checking.
example
public interface Animal {  // common to all animal (in your application)
    public void setDead();
    public boolean isAlive();
public abstract class Prey implements Animal{  // common to all animal that are a prey
    private boolean alive = true;
    private int foodValue = 0;
    public void setDead(){  alive = false; }  // probably better to name it die() instead of setDead()
    public boolean isAlive(){ return alive; }
    public abstract int getFoodLevel();
public class Rabbit extends Prey {
    private static final RABBIT_FOOD_LEVEL = 3;
    public int getFoodLevel(){ return RABBIT_FOD_LEVEL; }
private Location findFood(Field field, Location location){
    Iterator adjacentLocations = field.adjacentLocations(location);
    while(adjacentLocations.hasNext()) {
        Location where = (Location) adjacentLocations.next();
        Animal animal = (Animal) field.getObjectAt(where);
        if (animal.isAlive()){
            animal.setDead();
            foodLevel = animal.getFoodLevel();
            return where;
        return false;
}i would probably makes the Prey an interface instead of an abstract class.

Similar Messages

  • How to proportionally scale one side of a photo more than the other?

    Does anyone have any suggestions as to how to scale one side of an image more than the other? I've tried Content-Aware scale but it always yields ugly results. Any help is greatly appreciated.
    Peter

    Hi Peter
    I thik the main problem is the amount of the side of the column showing is making the right column appear larger.
    I would therefore suggest moving part of the right side of the image slightly to the left.
    You need to be careful in distorting the image as it will show up in the perspective on the floor.

  • MBP Caps Lock key sticks out more than the others!

    Just wondered if this is normal! The caps lock seems to stick out of the keyboard more than the others! Can anyone help?

    I just received my new MacBook Pro 17" this afternoon. My tab, caps lock, and shift keys are all raised on the left hand side. If you look at the keys from the left hand side of the keyboard you can see how they are raised up higher than the tilde and fn key. Also, the control keys kind of sticks when it is pressed down all the way. I like the machine, but I've very disappointed in the keyboard. It doesn't seem as nice as my PowerBook keyboard. I have them here side by side. The PowerBook keyboard is perfect and has a better feel when typing. I hope I get used to this new one.
    -Karen

  • Macbook came with an AC cable and an AC plug, is one better for charging than the other?

    Macbook came with an AC cable and an AC plug, is one better than the other for charging?

    The 3-prong AC cable provides for proper gorunding, and will alleviate any "vibration/electrical" sensation on the casework.

  • Audio audible in one Encore timline, but not the other?

    Hello,
    I'm trying to troubleshoot an issue in an Encore timeline for Blu-ray with an MPEG-2 and AC3 file.  It's odd because with the exact same file specs for both video and audio, I'm able to hear the audio from test project timeline (shorter in duration), but not the full length project timeline (roughly an hour and half in duration).
    I'm in the process of burning a disk to verfiy that the audio is simply not there, rather than a playback issue with the longer project, but can anyone suggest what might be failing here?  Again, the two projects appear to be identical in settings and file specs for video and audio, only the duration of one (working) is shorter than the other (not working).
    The audio file has been validated outside of Encore as playing back audio.
    Thanks for anything thoughts anyone might have here.  Btw, the project has no menu screens.  It is for a "play only" Blu-ray disk.
    Mtbakerstu

    Stan, thanks for the questions / feedback.
    As it turns out the file specs were not identical !  No fault of Adobe- an ingest / encode application from another company mysteriously swapped out channels 1/2 of audio to 5/6, and were not readable in Encore.   This information was not readiliy accessible within Encore, but was discovered in bringing the audio into Final Cut Pro (where it could be heard).
    Thanks again for your thoughts, as always.
    Mtbakerstu

  • Hi i  insttalled the free trial 3O days MacScan on OsX10.5 is it normal that for full scaning it takes so long time more even than one day?! on the other hand this application hasn't any uninstaller on image disc ,so how can i uninstal it from my hard?

    Hi i  insttalled the free trial 3O days MacScan on OsX10.5 is it normal that for full scaning it takes so long time more even than one day?! on the other hand this application hasn't any uninstaller on image disc ,so how can i uninstal it from my hard?Thanks

    Get rid of the tracking cookies. They are used to profile and track your browsing history. While they are privacy invading, by calling them spyware, MacScan is being a little dramatic in trying to sell you its crap. And in the future, for whatever browser you use, don't allow third-party cookies.
    To prevent tracking, get Ghostery. In addition to having Ghostery and forbidding third-party cookies, I clear out all cookies from one browsing session to another. If you always do that, you won't have any tracking cookies to worry about, so you won't need MacScan to find them for you. Btw, MacScan finds the tracking cookies in the first few minutes of scanning; if you want to use it for that, then that's all the time you need to run it for. But, as I said, you won't have any tracking cookies around if you just remove all cookies and don't allow third-party cookies. As soon as you visit a site that needs them, you'll just get new ones. No problem.
    Read all about cookies here.
    http://en.wikipedia.org/wiki/HTTP_cookie

  • Hello!  I need Apple (!!!) headphones for Ipod Shuffle 3! (only apple earphones with remote and mic!!!) Tell me, what distinguishes the model are presented in black and white boxes? They may differ in features? Can one model is more modern than the other?

    Hello!
    I need Apple (!!!) headphones for Ipod Shuffle 3! (only apple earphones with remote and mic!!!)
    Tell me, what distinguishes the model are presented in black and white boxes? They may differ in features? Can one model is more modern than the other?
    Thanks in advance!

    Hi Fencer1986,
    I apologize, I'm a bit unclear on your request. If you have questions about headphone compatibility for your iPod shuffle 3rd Gen, you may find the following article helpful:
    iPod shuffle (3rd generation): About headphone compatibility
    http://support.apple.com/kb/ht3472
    Regards,
    - Brenden

  • When printing a list in Address Book, how can I select more than the default Attributes and keep them selected when I print again? I want to print ALL information for contacts so I have email address, notes, phone, company, title, etc all on one page.

    When printing a list in Address Book, how can I select more than the default Attributes and keep them selected when I print again? I want to print ALL information for contacts so I have email address, notes, phone, company, title, etc all on one page. I don't want to have to check off an additional 5 or 6 attributes each time I print out contact information. Is there a way to change the default setting for printing lists, so it is not just "phone," "photo," and "job title?"

    I have a user who wants to do this same thing. I did not find any way either to default the attributes to anything other than what you see the first time. Seems like such a trivial thing, hard to believe they do not allow it. I did find a program for this called iDress but I can't seem to download it from any links on the Internet. Not sure if it is free or not, but it was recommended by a link on the Mac support site.

  • Hi from few days onwards i found one abnormality in my iphone4s .That is if i observe my phone settings screen from right side of the device top navigation appearing rightside height is more than the left side. In reverse manner from the left side.

    Hi from few days onwards i found one abnormality in my iphone4s .That is if i observe my phone settings screen from right side of the device top navigation bar appearing rightside height is more than the left side. In reverse manner from the left side. I dnt know whether it is default feature of iphone or not but i am really disappointed due to this. So please anyone help me in this issue and let me clear my doubt and make me happy. Thanks in advance.

    I believe what you are seeing is an optical illusion when viewing the screen from the side. Try to align one of the one of the options in Settings, like General, with the navigation bar you'll see it does the same thing.
    Hope that helps

  • Syncing photos from more than the last 20 iPhoto events?

    How can I sync more than the last 20 iPhoto events with my iPhone?
    When I select folders instead of events I can get more photos (ex: last 12 months) to my iPhone but at the same time I no longer have access to the "events" view which I use to organize my photos.
    Is there a solution to sync/copy more than 20 events to my iPhone?

    For whatever the reason, syncing iPhoto Events is limited.
    I make use of iPhoto albums in addition to Events - primarily for transferring photos to my iPhone.

  • I want to transfer my iPhoto from my old MacBook Pro to my new MacBook Pro. I have a firewire or I could also do it from my TimeCapsule. Would one be a better method than the other?

    I want to transfer my iPhoto from my old MacBook Pro to my new MacBook Pro. I have a firewire or I could also do it from my TimeCapsule. Would one be a better method than the other?

    Hi brotherbrown,
    A direct FireWire transfer (especially if it's 800 to 800) is going to be the fastest method. TimeCapsule would work, if you connect to it via Ethernet, via wireless it would be quite slow (especially if you have a large library).

  • Why does my iPhone plays louder music in one side of the earphone than the other?

    Hello Apple Community,
    So I own this iPhone 4S which is about half a year old, and from this Monday onwards I kept feeling that my iPhone plays music louder in one side of the earphone than the other. I don't think it's my earphones because I just got them two weeks ago, and I always took good care of it. What can I do to solve this frustrating problem?
    Thanks!
    P.S. My earphones are the Klipsch x10. Costs about 300 bucks.

    The only setting on an iPhone that controls balance is here: Settings>General>Accessibility>Hearing.

  • Qosmio X300 - one Geforce 9800 GTS runs hotter than the other

    Hi.....Does anyone have a clue why 1 of my 2 gfx cards seems to be running alot hotter than the other? I would assume they would both be roughly the same temp. This is an example...Nvidia Defaul @ 1680x1050
    512MB GeForce 9800M GTS (Toshiba) 63 C
    ForceWare version 186.42
    512MB GeForce 9800M GTS (Toshiba) 49 C
    ForceWare version 186.42
    256MB GeForce 9400M G (Toshiba) 56 C
    ForceWare version 186.42
    SLI Disabled
    When i exited playing F1 2010 yesterday, one of the cars was 90C and the other about 58C......Im sure that isnt right. !!!!
    Also i see that it says SLI disabled....again i dont understand this, as i thought my rig automatically switched to SLI mode when needed.
    These temp readings was taken from the program speccy.
    My laptop is a Toshiba Qosmio X300-15U....running windows 7 ultimate 64 bit. 4gb ram. Q6600@2ghz.
    Any help would be really apprieciated.
    Thanks

    Well i'm sorry you've had such clueless replies to your question.....if i had a cent for everytime a "regular" toshiba contributor says "use compressed air" or "only use Toshiba display drivers blah..blah`.....i'd be a very rich man!
    The simple reason why only 1 of your 9800m gts cards is hot is because it is the only card being utilised in most games especially recent ones like F1 2010.
    You see the toshiba graphics driver you have installed on your Qosmio is nearly 2 years old-and it doesn't contain any SLI profiles for games that have been released after this driver was created (mid 2009).
    Just because you have 2 Nvidia GPUs in the laptop it does not mean that they are being utilised everytime-in fact it is totally dependent on having an SLI profile for specific program/game within the driver itself.
    If you open the Nvidia control panel and go to advance settings/manage programs you can scroll down a list of sli profiles for programs installed on you machine and also view a complete list of all profiles within the driver.
    It is possible to add you own sli profiles on this screen by browsing for a games executable and adding it to the list of preinstalled profiles.
    However in my experience this rarely,if ever, produces much of a performance increase and can sometimes cause instability-and is no substitute for Nvidias own SLI profiles.
    So.. the only way you can gain an SLI boost in games is by having the latest driver with the latest profiles....but wait i hear you say..
    Toshiba haven't updated the driver for our laptop since 2009!!!
    Yes thats the massive downside of owning a gaming laptop especially one as unique as Qosmio x300-just see my post in the Gaming forum-`Toshiba display drivers and what they don't want you to know' and i'm not gonna go into all that again.
    All i say is that you can get some great advice on using modified Nvidia drivers on Laptopvideo2GO.com and if you go to the forums and look under Nautis1100 Hybrid and optimus drivers you'll find a new thread especially created for owners of qosmio X300s.
    Good luck!

  • Report to select customer ID occuring more than once

    My report requirement is to capture transaction detail when a customer ID occurs more than once. Is there a way to do a sum count on the customer id in order to display the transaction detail. My current solution is to run a report where the customer count is greater than one, and then do a subquery against that data to display the transaction detail. I would like to be able to eliminate the subquery.

    Hi,
    You can create an analytic function to count the customer_ID per every transaction:
    count(customer_id) over (partition by ..<enter the fields of the report>.....)
    This will give for every transaction the number of transaction per same customer_id
    Now that you have it you can create a condition on it so
    that you will get only transaction with count customer_id > 1

  • Can I have more than one ipod with more than  music library on one computer

    I was just wondering if i could have 2 different ipods (one a 5th generation and the other ipod touch) with 2 different music libraries on the same computer and itunes? because right now im using my parents computer and my fiance is using my laptop and we're moving so we'll only have one computer in the next few weeks. So if anyone could try to give me an answer or have a solution that would be fantastic lol!

    See these articles about using more than one iPod on one computer, and with either one or more iTunes libraries.
    Multiple iPod installations.
    How to use multiple iPods with one computer.
    If you have the latest iTunes, an easy way to create a new library is to hold down the 'alt' key whilst opening iTunes (for Macs), or hold down the 'shift' key (for Windows).
    This will give you the option of either using the current iTunes library or creating a new one.
    There are various methods of transferring different songs to different iPods with a single iTunes library.
    One is to manage the songs manually.
    With this setting, you can drag and drop songs/playlists etc onto the iPod from iTunes, and also delete and/or edit songs which are currently on the iPod without affecting the iTunes library.
    If you wish to keep the convenience of automatic sync, then you could set the iPods to sync with "selected playlists". This setting can be found in the iPod summary screen under the 'music' tab. With this setting, different playlists can be transferred and later deleted from each iPod without affecting any playlists stored in iTunes or the playlists on the other iPods.
    Also, on the 'summary' main page you will see the option to "only sync checked items". With this setting selected, if you remove the check marks from any songs in iTunes, they will not be transferred to the iPod. You can restore the check mark if you want to put the songs back on the iPod at a later date.
    Be aware that when playing songs in iTunes, any songs that do not have a check mark against them will be skipped over and will not be played.

Maybe you are looking for