2

I am working on an class lab and ran into some trouble. We have been asked to write a method with the following instructions:

"Dog getRandomDog() - randomly selects a dog, returns it, and removes it from the kennel. Returns null if there are no dogs."

This is the method that I wrote (which doesn't work):

public Dog getRandomDog(){
    if(dogs.size() >= 0){
        Random random = new Random();
        int index = random.nextInt(dogs.size());
        return dogs.get(index);
        dogs.remove(index);
   }
    else {
    return null;
   }
}

I do understand that you cant have an executable statement after a return, but then how the heck to do you around this? Thanks in advance.

8 Answers 8

3

I come from an old school of thought One entry, one exit

public Dog getRandomDog(){
    Dog dog = null;
    if(dogs.size() >= 0){
        Random random = new Random();
        int index = random.nextInt(dogs.size());
        dog = dogs.remove(index);
    }
    return dog;
}

This basically means that you allow one point of entry for your method (a little mute in Java) and one exit (or one return). This makes the method easier to understand as you don't need to worry about the method exiting half way through. Really a pain when the method can run several screens lengths...

Sign up to request clarification or add additional context in comments.

Comments

1

After return the code will not run

return dogs.get(index);
dogs.remove(index);  // <--- This will not execute after return

Try to return the result of remove:

return dogs.remove(index);

Comments

1

dogs.remove(index) itself will remove the Dog object at index and return it as well.

return dogs.remove(index)

Comments

0

You need to remove before the return statement

 public Dog getRandomDog(){
    Dog d = null;
    if(dogs.size() >= 0){
        Random random = new Random();
        int index = random.nextInt(dogs.size());
        d = dogs.remove(index);
    }
    return d;
    }

Comments

0

If the collection that dogs is a type of returns the object itself on calling remove, you can simply do:

int index = random.nextInt(dogs.size()); return dogs.remove(index);

Comments

0

The most compact version would be:

Dog d = dogs.remove(getRandomDog());  

public Dog getRandomDog()  {  
    return dogs.size() > 0 ?  dogs.get((int)(Math.random()*dogs.size())) : null;  
}  

Comments

0

This will also work, apart from most of the answers above

public Dog getRandomDog(){
if(!dogs.isEmpty()){
    Random random = new Random();
    int index = random.nextInt(dogs.size());
    try{
    return dogs.get(index);
    }finally{
    dogs.remove(index);
   }
}
 else {
  return null;
 }
}

Comments

0

I voted up @MadProgrammer.

public Dog getRandomDog() {
    return dogs.isEmpty() ?
        null : dogs.remove(new Random().nextInt(dogs.size()));
}

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.