「Amazing Pets」という非常に単純な Java アプリケーションを作成しています。これには、人間とそのペット (猫または犬) が含まれます。この場合、私たちは犬を扱っています。人間用のインスタンス メソッド (makeDogMakeNoise と呼ばれる) を作成するにはどうすればよいですか? このメソッドは、Dog で makeNoise を呼び出し、ランダムな整数をパラメーターとして渡します。makeNoise メソッドは、ランダムなノイズ文字列をコンソールに出力します。たとえば、「ゴーストの鳴き声」、「ゴーストの鳴き声」、「ゴーストの鳴き声」などです。オンラインで信頼できるリソースが見つからないので、この件について誰か助けてもらえますか? よろしくお願いします。
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;
}
}
Dogs.java
public class Dogs {
private final String mDogName;
public Dogs(String dogName){
mDogName = dogName;
}
public String getDogName(){
return mDogName;
}
}