Virtual Pet OOP Project in Java
Starter Code (Template)
File: Pet.java
public class Pet {
// Attributes (fields)
private String name;
private int hunger;
private int happiness;
private int energy;
// Constructor
public Pet(String name) {
this.name = name;
this.hunger = 50;
this.happiness = 50;
this.energy = 50;
}
// Methods (behaviors)
public void feed() {
hunger -= 10;
energy += 5;
System.out.println(name + " has been fed.");
}
public void play() {
happiness += 10;
energy -= 10;
hunger += 5;
System.out.println(name + " is playing.");
}
public void sleep() {
energy += 20;
hunger += 10;
System.out.println(name + " is sleeping.");
}
public void showStatus() {
System.out.println("\nStatus of " + name + ":");
System.out.println("Hunger: " + hunger);
System.out.println("Happiness: " + happiness);
System.out.println("Energy: " + energy);
}
}
File: PetGame.java
import java.util.Scanner;
public class PetGame {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Name your pet: ");
String petName = scanner.nextLine();
Pet myPet = new Pet(petName);
while (true) {
System.out.println("\nWhat do you want to do?");
System.out.println("1. Feed");
System.out.println("2. Play");
System.out.println("3. Sleep");
System.out.println("4. Show Status");
System.out.println("5. Exit");
int choice = scanner.nextInt();
switch (choice) {
case 1: myPet.feed(); break;
case 2: myPet.play(); break;
case 3: myPet.sleep(); break;
case 4: myPet.showStatus(); break;
case 5: System.out.println("Goodbye!"); return;
default: System.out.println("Invalid choice.");
}
}
}
}
Student Instructions:
1. Study the sample code.
2. Customize it:
- Add new actions (e.g., bath(), train()).
- Add more attributes (e.g., health, age, mood).
- Add basic rules (e.g., if hunger > 100, pet "gets sick").
3. Submit your modified code and a short write-up (1 paragraph) explaining what you
added or changed.
Assessment Rubric (20 points total)
Criteria Points
Correct use of classes/objects 5
Functional methods and logic 5
Creative enhancements/customizations 5
Code readability and comments 3
Short write-up / explanation 2