We are going to create a constructor and some methods for our Dog class, so that Baxter, Lucy, and Max can play together. Here’s what we need:
A constructor that takes in a name (String), age (int) and a “tired” value (boolean). The order matters here!
A play() method. play() should print that the dog is playing and then make the dog tired. If the dog is already tired, it cannot play (print this out to tell the user)
A nap() method. nap() should print that the dog is napping and then make the dog not tired. If the dog is already not tired, it cannot nap (print this out to tell the user)
A birthday() method. birthday() should print out a happy birthday message for the dog and increase the dog’s age by 1.
A toString() method. toString() should return a String that looks like this. This example is for a Dog named Spot who is 9 years old and is not tired:
“Dog: Spot age: 9 tired: false”
Testing
After you think you are done, run the dog simulation by executing java Dog in your cloud shell. Follow the code in the main method and make sure it is doing what you’d expect! (i.e. Lucy is too tired to play, and Baxter is too excited to nap (this happens twice). Baxter should also celebrate his 4th birthday.)
Already registered? Login
Not Account? Sign up
Enter your email address to reset your password
Back to Login? Click here