Java Reference
In-Depth Information
< Day Day Up >
Puzzle 79: It's a Dog's Life
This class models the life of a house pet. The main method creates a Pet instance representing a dog
named Fido and lets it run. Although most dogs run in the backyard, this one runs in the
background. What does the program print?
public class Pet {
public final String name;
public final String food;
public final String sound;
public Pet(String name, String food, String sound) {
this.name = name;
this.food = food;
this.sound = sound;
}
public void eat() {
System.out.println(name + ": Mmmmm, " + food);
}
public void play() {
System.out.println(name + ": " + sound + " " + sound);
}
public void sleep() {
System.out.println(name + ": Zzzzzzz...");
}
 
 
Search WWH ::




Custom Search