Java

 

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:

  1. Install Java Development Kit (JDK) if not already installed.
  2. Create a new file named VirtualPet.java in your preferred Java development environment.
  3. Copy and paste the code into this file.
  4. Compile the program by running javac VirtualPet.java in the terminal/command prompt.
  5. Run the program by executing java VirtualPet.
  6. Follow the on-screen instructions to interact with your virtual pet!
© 2025 Learn Programming. All rights reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)