Introduction
Welcome to the Virtual Pet Game! This simple Java program allows you to take care of a virtual pet. You can feed your pet, play with it, and keep track of its mood and hunger levels. The pet’s happiness depends on how well you take care of it. This project is a great way to practice Java programming and object-oriented principles such as classes, methods, and variables.
Objective
The objective of this program is to simulate the experience of taking care of a pet. You will interact with the pet by feeding it and playing with it, and you’ll be able to track its health, hunger, and happiness levels. The goal is to keep your pet happy and healthy through regular care. This will provide hands-on experience with basic Java programming and console input/output.
Code
public class VirtualPet {
private String name;
private int hungerLevel;
private int happinessLevel;
public VirtualPet(String name) {
this.name = name;
this.hungerLevel = 5;
this.happinessLevel = 5;
}
public void feed() {
if (hungerLevel > 0) {
hungerLevel--;
System.out.println(name + " has been fed!");
} else {
System.out.println(name + " is not hungry right now.");
}
}
public void play() {
if (happinessLevel < 10) {
happinessLevel++;
System.out.println(name + " is playing and getting happier!");
} else {
System.out.println(name + " is already very happy!");
}
}
public void status() {
System.out.println("\nPet Status:");
System.out.println("Name: " + name);
System.out.println("Hunger Level: " + hungerLevel);
System.out.println("Happiness Level: " + happinessLevel);
}
public static void main(String[] args) {
VirtualPet pet = new VirtualPet("Buddy");
java.util.Scanner scanner = new java.util.Scanner(System.in);
int choice;
do {
System.out.println("\nWhat would you like to do?");
System.out.println("1. Feed the pet");
System.out.println("2. Play with the pet");
System.out.println("3. Check pet status");
System.out.println("4. Exit");
choice = scanner.nextInt();
switch (choice) {
case 1:
pet.feed();
break;
case 2:
pet.play();
break;
case 3:
pet.status();
break;
case 4:
System.out.println("Thanks for taking care of " + pet.name + "!");
break;
default:
System.out.println("Invalid choice. Please try again.");
}
} while (choice != 4);
scanner.close();
}
}
Program Structure and Explanation
This Java program is structured around a class called VirtualPet. The main components of the program are:
- VirtualPet Class: This class holds the pet’s name, hunger level, and happiness level. It also provides methods to feed, play, and check the pet’s status.
- feed() Method: Decreases the pet’s hunger level by 1 when called. If the pet isn’t hungry, it will let you know.
- play() Method: Increases the pet’s happiness level by 1 when called. It checks if the pet is already happy before allowing another play session.
- status() Method: Displays the current status of the pet, including its name, hunger level, and happiness level.
- Main Method: This method runs a loop to continuously ask the user for input, allowing them to interact with the pet by choosing different options (feed, play, check status, or exit).
How to Run the Program:
- Install Java Development Kit (JDK) if not already installed.
- Create a new file named
VirtualPet.java
in your preferred Java development environment. - Copy and paste the code into this file.
- Compile the program by running
javac VirtualPet.java
in the terminal/command prompt. - Run the program by executing
java VirtualPet
. - Follow the on-screen instructions to interact with your virtual pet!