Solution: Simulate with Objects
This Java program is a virtual pet simulator where the user interacts with a pet named Milo. The pet has two key attributes: hunger and happiness, both starting at a value of 5.
The Pet class defines how the pet behaves:
The
feed()method lowers the hunger level (so the pet becomes less hungry) and shows the current hunger and happiness.The
play()method raises both hunger and happiness (playing makes the pet happy but also hungrier).The
checkMood()method checks if hunger is too high. If it is, the pet loses happiness and a warning message is displayed.
In the main() method, the program uses a menu system that repeatedly asks the user to:
Feed the pet
Play with the pet
Exit the program
Each choice updates Milo’s status accordingly until the user chooses to exit.
Overall, the program demonstrates object-oriented programming, method interaction, and user input handling through a simple, interactive console-based game.
Solution: Simulate with Objects
This Java program is a virtual pet simulator where the user interacts with a pet named Milo. The pet has two key attributes: hunger and happiness, both starting at a value of 5.
The Pet class defines how the pet behaves:
The
feed()method lowers the hunger level (so the pet becomes less hungry) and shows the current hunger and happiness.The
play()method raises both hunger and happiness (playing makes the pet happy but also hungrier).The
checkMood()method checks if hunger is too high. If it is, the pet loses happiness and a warning message is displayed.
In the main() method, the program uses a menu system that repeatedly asks the user to:
Feed the pet
Play with the pet
Exit the program
Each choice updates Milo’s status accordingly until the user chooses to exit.
Overall, the program demonstrates object-oriented programming, method interaction, and user input handling through a simple, interactive console-based game.