[go: up one dir, main page]

0% found this document useful (0 votes)
104 views3 pages

Virtual Pet OOP Project Java

The document outlines a Java-based virtual pet project, including starter code for a Pet class and a PetGame class that allows users to interact with their pet. Users can feed, play, sleep, and check the pet's status, with a loop for continuous interaction. Students are instructed to customize the code by adding new actions, attributes, and rules, and to submit their modified code along with a brief write-up explaining their changes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
104 views3 pages

Virtual Pet OOP Project Java

The document outlines a Java-based virtual pet project, including starter code for a Pet class and a PetGame class that allows users to interact with their pet. Users can feed, play, sleep, and check the pet's status, with a loop for continuous interaction. Students are instructed to customize the code by adding new actions, attributes, and rules, and to submit their modified code along with a brief write-up explaining their changes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

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

You might also like