java - Object Oriented Programming, Method Call Not Working as Expected, Subclasses -
i having trouble call method makenoise in pets class. call makenoise method through class, humans, has makepetmakenoise method:
public void makepetmakenoise() { int randnum = (int)(math.random() *5); pet.makenoise(randnum); }
and set pets' canmakenoise boolean when create it: cat = new cat("critter", "meow", true);
when call humans' makepetmakenoise method, printout so: critter remains silent instead of: meow critter. why this, , how fix it? thanks.
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==true) { for(int i=0; i<number; i++) { system.out.println(noise + " " + name); } } else if(canmakenoise==false) { system.out.println(name + " *remains silent*"); } } public void eat() { system.out.println(name + " eating..."); }
}
it looks assignment "canmakenoise" in constructor reversed, i.e. assign canmakenoise = pcanmakenoise
.
Comments
Post a Comment