Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

Random Int as Parameter Java

I am making a very simple Java application called "Amazing Pets." It involves Humans and their Pets (Cats or Dogs). In this case, we are dealing with the Dogs. How do I create an instance method for Humans (called makeDogMakeNoise) which calls the makeNoise on the Dog and passes a random integer as a parameter? The makeNoise method prints three (3) random noise strings to the console. For Example "Ghost barks", "Ghost woofs, "Ghost whimpers". Can anyone please assist on this matter as I cannot seem to find any reliable resources online? Thank you in advance.

AmazingPets.java:

 public class AmazingPets {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
System.out.println("Welcome to Pets and Humans! Created By Marc B.\n____________________________\n");

Dogs firstDog = new Dogs("Ghost");
Humans firstName = new Humans("Alex");
Dogs secondDog = new Dogs("Paperbag");
Humans secondName = new Humans("Michael");
Cats firstCat = new Cats("Tom");
Cats secondCat = new Cats("Mr Furball");
Humans thirdName = new Humans("Bryan");
Humans fourthName = new Humans("Julie");

System.out.printf("%s's dog's name is %s.\n", firstName.getHumanName(), firstDog.getDogName()); 
System.out.printf("%s's dog's name is %s.\n", secondName.getHumanName(), secondDog.getDogName());
System.out.printf("%s's cat's name is %s.\n", thirdName.getHumanName(), firstCat.getCatName());
System.out.printf("%s's cat's name is %s.\n", fourthName.getHumanName(), secondCat.getCatName());

System.out.printf("\n\nHow many Humans have been created? To get your answer type in the console 'population'. ");
Scanner scan = new Scanner(System.in);
String myLine = scan.nextLine();
String pop = "population";
if (myLine.equalsIgnoreCase(pop)){
    System.out.printf("There are %s Humans.\n", Humans.populationCount());
}
else {
    System.out.printf("There was an error getting the Population.\n");
}   

}    
}

Humans.java:

 public class Humans {
private final String mHumanName;

private static int humanCount = 0;
public Humans(String humanName){
    mHumanName = humanName;
    humanCount++;
}
public String getHumanName(){
    return mHumanName;
}

 public static int populationCount() {
    return humanCount;
}

}

Dogs.java:

 public class Dogs {
private final String mDogName;

public Dogs(String dogName){
    mDogName = dogName;
}
public String getDogName(){
    return mDogName;
}

}

This question is a bit tricky because of the way your program is built. Currently, the Human class is completely separate from the Dogs class. What this means is that if the Human class needed to call a method on an instance of the Dog class, the Human would need to receive a reference to this instance as a parameter. Like this :

public void makeDogMakeNoise(Dog myDog){

}

The other workaround is to make the instance of dog public, but this is a big no no in OOP.

While this method IS possible, I would recommend restructuring your program a little bit.
My reasoning behind this is the relationship between dogs and owners. Every owner HAS A dog. Since it is a has a relationship, it makes sense for the Dog to be a field for each Human. This way, each human is able to call methods on their own pets without having to be provided with the parameter each time. This would also require NO modification of the Dog class, only minor adjustments to the Human class, and some changes to your AmazingPets.java

Afterwards it would look something like this:

 public class Humans {
private final String mHumanName;
private Dog mMyDog;
private static int humanCount = 0;

public Humans(String humanName, String dogName){
    mHumanName = humanName;
    humanCount++;
    mMyDog = new Dog(dogName);
}
public String getHumanName(){
    return mHumanName;
}

 public static int populationCount() {
    return humanCount;
}

}

You would also want to add getter and setter methods for the dogs info to the Human class, but hopefully you get the picture.

Now on to the actual question! To do this you would use the Java Random class. Imported from java.util.Random In case you are unfamiliar with it, I will quickly go over its use.

//Initializing an instance of the Random class
Random rand = new Random();

//Generate a random number using the nextInt() method

int randomNumber = rand.nextInt(3);

When calling the nextInt() method on a Random object, it will return a random number between 0 and one lower than the argument provided. For example rand.nextInt(2) would return either 0 or 1;

Putting it all together now, the method would look something like this:

public void makeDogMakeNoise(){
  mMyDog.makeNoise(new Random().nextInt(3));
}

Or if that is confusing

public void makeDogMakeNoise(){
  Random rand = new Random();
  mMyDog.makeNoise(rand.nextInt(3));
}

I am not entirely sure what you need the makeNoise method to do so if you have any questions with that feel free to reply. Sorry for the short novel!

1 Answer

Hey Jonathan, great answer!!!!

Another question, shouldn´t Dog be called Dogs, because of the class name "Gogs" ?

Dogs dog = new Dogs();

instead of

Dog dog = new Dog();

I am pretty sure it is convention to name classes singular.
My guess to the reasoning behind this is they when you are creating an instances of the class, each instance represents a single "Dog", not multiple dogs.
Though I am unsure what you are referring too when you say "Gogs", it may be some course material that I skipped over so take my answer with a grain of salt!