我正在制作一个名为“Amazing Pets”的非常简单的 Java 应用程序。它涉及人类及其宠物(猫或狗)。在这种情况下,我们正在处理 Dogs。如何为人类创建一个实例方法(称为 makeDogMakeNoise),该方法调用 Dog 上的 makeNoise 并将随机整数作为参数传递?makeNoise 方法将随机噪声字符串打印到控制台。例如“Ghost barks ”、“Ghost woofs”、“Ghost whimpers ” 。任何人都可以帮忙解决这个问题,因为我似乎无法在网上找到任何可靠的资源?提前谢谢你。
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");
}
}
人类.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;
}
}
狗.java
public class Dogs {
private final String mDogName;
public Dogs(String dogName){
mDogName = dogName;
}
public String getDogName(){
return mDogName;
}
}