I'm having a difficult time trying to understand an assignment I was given as a part of a OOP Introduction project in Java. All names are changed, not exactly translated. I'm asked to complete the following method:

public boolean insertHorse(Horse horse) {
return true;
}

The method signature can't be changed, I'm supposed to add the horse on the array of horses of the class the method is in, and to return true if it was inserted with success, or false otherwise. So this is what I've done:

    public boolean insertHorse(Horse horse) {

    //Create a temp array with length equal to h
    Horse[] temp = new Horse[this.horses.length+1];

    //copy the horses to the temp array
    for (int i = 0; i < horses.length; i++){
        temp[i] = horses[i];
    }

    //add the horse on the temp array
    temp[temp.length-1] = horse;

    //change the reference of the old array to the temp one
    veiculos = temp;
    return true;

My problem is, how and why would this ever give false? I do have a limitation to the number of horses of a farm, however I can (and was planning) on checking that before calling the method. Is this a bad practice of me?

Highest regards, João Silva

share|improve this question
You can "correctly" implement the function by simply returning "false" and doing nothing else. – Hot Licks 19 hours ago
@HotLicks Then I would lose the return true possibility, I guess in the context of the assignment I have to check if I'm at the max of allowed Horses in the array, and if I am -> return false – Kirag 19 hours ago
It would still satisfy the "contract" -- the invariant would be preserved. – Hot Licks 18 hours ago
feedback

2 Answers

up vote 1 down vote accepted

I can't think of any reason this should return false, except for the limitation of number of horses. Although in theory you could run out of heap space if there were too many horses, that condition would probably cause your program to crash anyway.

It would be good practice to put the number of horses check in this function instead of outside of it. That way, it wouldn't be possible to accidentally insert more horses than allowed.

share|improve this answer
I can work the check inside, aside from running OutOfMemory it's the only "reason" I can think to return false. But I would believe checking them before call would make more sense, since I have a few more things to check not related to the horses but their "company" (names were changed but the idea is the same) – Kirag 19 hours ago
feedback

You're assuming that there will always be enough memory available to allocate an array with room for one more Horse.

One situation in which that would not be true is if an OutOfMemoryError was thrown.

share|improve this answer
1  
Makes sense, but with that in mind, if an 'OutOfMemoryError' is thrown, doesn't it crash instead of returning false? – Kirag 19 hours ago
Not right away, or at least, not if you catch it. It would probably be a good idea to shut down the program after perhaps logging the error or presenting it to the user, however. – Danny 19 hours ago
been searching about that, even if I catch it like you said, the program would have to end, and that's not considered on the assignment. I believe this is more interpretation than the rest, as the question is really, when should it return false. Nonetheless thank you for your answer ;) – Kirag 19 hours ago
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.