Objective:
Create an app that behaves like a pet. You have to feed it, keep it entertained, make sure it sleeps, and so on.
Requirements:
1) Define a class such as “Pet” or “Critter”
Right click the folder containing your MainActivity Java code and click “New” and then “Java Class.”
Define some class attributes. Example: a boolean called hungry.
Create getters and setters for your attributes. This can be done quickly using “generate” (right click or “Alt+Insert”).
Create a constructor for your class.
Create an instance of this class. That is, create an object of the type that you just defined. Declare it as a class variable in MainActivity, and then create it in the onCreate() method.
2) User Interface for your Pet
Create several buttons in your Main Activity to represent things like feeding, playing, and sleeping.Create one or more TextViews to display information about the status of your pet.Make the buttons interact with your pet object.
3) Program Behaviors
Now you can write functions that make your buttons do something with your pet.
- The functions should change the status of your pet in memory (change object attributes using setter methods)
- The TextView (or other View) objects should be updated so your user can see that latest status of the pet. Use setText() to change View text and use getter methods to retrieve the status of your pet.
- Examples: Make your pet eat, but only if it’s not hungry. Make the pet tired after it plays. Make it sleep only if it’s tired. Make it not tired anymore after it sleeps.