I am trying to write a program (Driver) that will create Humans and their Pets and specific types as seen below.
I have run into multiple errors: in the Driver program, the error is when I try to do Humans b = new Humans(); and I get:
constructor Humans in class Humans cannot be applied to given types.
Additionally, if I try to write out Humans b = new Humans("Jane", Pet/Dog...) I am unsure of how to specify the type of Pet for the person.
Any help and direction would be appreciated.
public class Humans {
String name;
Pets pet;
int popcount;
public Humans(String hname, Pets hpet) {
name = hname;
pet = hpet;
}
public int populationCount() {
return popcount;
}
public void makePetMakeNoise() {
int randnum = (int) (Math.random() * 10);
pet.makeNoise(randnum);
}
public void feedPet() {
pet.eat();
}
}
public class Pets {
String name;
String noise;
boolean canMakeNoise;
public Pets(String pname, String pnoise, boolean pcanmakenoise) {
name = pname;
noise = pnoise;
pcanmakenoise = canMakeNoise;
}
public void makeNoise(int number) {
if (canMakeNoise != false) {
for (int i = 0; i < number; i++) {
System.out.println(noise + name);
}
} else {
System.out.println(name + " *remains silent*");
}
}
public void eat() {
System.out.println(name + " is eating...");
}
class Dog extends Pets {
public Dog(String pname, String pnoise, boolean pcanmakenoise) {
super(pname, pnoise, pcanmakenoise);
}
public void eat() {
System.out.println(name + " is eating...");
}
}
class Cat extends Pets {
public Cat(String pname, String pnoise, boolean pcanmakenoise) {
super(pname, pnoise, pcanmakenoise);
}
public void eat() {
super.eat();
System.out.println("I'm still hungry, meow");
}
}
}
public class Driver {
public Driver() {
Humans b = new Humans();
b.name = "Jane";
Pets bb = new Pets(Cat);
b.pet = bb;
bb.canMakeNoise = true;
bb.name = "Bertha";
ArrayList<String> list = new ArrayList<String>();
list.add(b);
ListIterator<String> itr = list.listIterator();
while (itr.hasNext()) {
String str = itr.next();
str.makePetMakeNoise();
str.feedPet();
}
System.out.println(Humans.populationCount());
}
}
The first issue you're seeing is that you're trying to pass no arguments to the Humans constructor but you didn't define a constructor that takes no arguments. The easy fix for this specific error is to add a default constructor to Humans:
public Humans() {}
Probably more inline with what you're trying to do would be to call the constructor you have. To do this you need to pass a name and pet to Humans(String, Pets), as you seem to have figured out.
Creating a Pets object is straightforward, but you don't specify the type of pet like you're trying: each subclass of Pets is itself a class, so you would call new Pets.Cat(...) rather than trying to invoke something on Pets.
This gets us to:
Pets bb = new Pets.Cat("Bertha", "meow", true);
Humans b = new Humans("Jane", bb);
This will lead to more compile errors, but hopefully that helps with the construction questions you asked.
Comments about naming conventions/structure:
Humans class represents a single human, not multiple humans (currently at least, not sure where you're going with population), so it would be more appropriately named Human.Dog and Cat more typically would be placed outside the Pets class: the inheritance of extends Pets informs the user/compiler of the relationship with Pets, the location of the class definition doesn't need to.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With