-2

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 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 Beepath.\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 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;
    }
}
SaeHyun Kim
  • 475
  • 2
  • 8
  • 26
theenigma
  • 431
  • 1
  • 6
  • 6

3 Answers3

2

A random int can be obtained using java.lang.Math.random() or java.util.Random.

Heres how I would get a random int between 0 and size (another int) using Math.random():

    int randomInd = (int)((size+1)*Math.random()) //needs casting to int as a double is returned

And now using java.util.Random:

    Random r = new Random();
    int randomInd = r.nextInt(size+1);

You should realize that your design can be greatly improved based on the comments above. Think on how you would answer the questions below:

  1. Can a Human have more then 1 pet? Can a Human have a Cat and a Dog as well?
  2. Should the Human class contain makeDogNoise() and makeCatNoise() methods or will just makePetNoise() do?
  3. Human, Dog, Cat better describe the objects than plurals such as Humans
Azee
  • 63
  • 4
0

Try this:


    public class Humans {
    private 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;
    }

    public void makeDogMakeNoise(Dogs d){
        d.makeNoise(Math.random());
    }
    }

    public class Dogs {
    private final String mDogName;
    private String[] strs= {"barks", "woofs", "whimpers"};

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

    public void makeNoise(int n){
        System.out.println("Ghost "+strs[(int)(n*(strs.length-1))]);
    }
    }

    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 Beepath.\n____________________________\n");

        Dogs firstDog = new Dogs("Ghost");
        Humans firstName = new Humans("Alex");
        firstName.makeDogMakeNoise(firstDog);

        Dogs secondDog = new Dogs("Paperbag");
        Humans secondName = new Humans("Michael");
        secondName.makeDogMakeNoise(secondDog);

        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");
        }
    }    

Carlos Rojas
  • 95
  • 2
  • 6
  • What would you suggest being the most suitable approach to implement this in AmazingPets.java to print it onto the console? – theenigma May 25 '15 at 08:43
0

Edit: misread the "random number" part of your question.

In order for a Human to tell one of his/her Dogs to "make a noise", he/she needs to have a reference to the Dog in the first place.

In order to do this, you can define the makeDogMakeNoise method inside your Humans class so that it accepts a parameter (i.e. a reference to a Dog):

public void makeDogMakeNoise(Dogs dog){
    // Tell the specified Dog to make one of n random noises

    // Generate a random integer between 0 and 9
    int n = (int)(Math.random() * 10);

    // Now you can tell the dog to make a noise
    dog.makeNoise(n);
}

You will need to define the makeNoise method inside your Dogs class:

public void makeNoise(int n){
    /*
     * Do what you need to for this Dog to "make a noise"
     */
}

You can now do something like this:

Humans johnSnow = new Humans("John Snow");
Dogs ghost = new Dogs("Ghost");

// Tell the dog to make a noise
johnSnow.makeDogMakeNoise(ghost);

Also, consider changing your class names from being plural (use Human, Dog, Cat instead).

Hope that helps.

D. Pereira
  • 116
  • 1
  • 5