Question: By using BlueJ For this exercise, you will be given a project file digital_pet containing two (2) classes, the DigiPet class and the DigiPetTester class.

By using BlueJ

For this exercise, you will be given a project file digital_pet containing two (2) classes, the DigiPet class and the DigiPetTester class. Every DigiPet has five fields: - name - lifespan (in cycles) max age - age (in cycles) current age - mood (2 = joyful, 1 = happy, 0 = neutral, -1 = sad, -2 = angry) - size current size of pet (size 1) A DigiPet has a default constructor, overloaded constructor, and some various methods which allow one to interact with it or modify its state. Poke or pet increases or decreases its mood. Feed or exercise increases or decreases its size by a specified amount. Every time you interact with the digipet (poke, pet, feed, exercise), its age increases. When the age reaches the lifespan, it dies (or becomes a zombie). You need to modify and customize the DigiPet class as follows: - Implement the overloaded constructor: Initialize all five fields. Use the new values (passed as parameters) along with the same default values that the default constructor uses. - Implement poke(): Decrement mood by 1. Check and make sure mood stays larger than -2. Call the makeOlder() method to make the pet older by one cycle. (Note that the makeOlder() method is already implemented for you; you simply need to call it.) - Implement pet(): Increment mood by 1. Check and make sure mood stays smaller than 2. Call the makeOlder() method to make the pet older by one cycle. - Implement feed(int amount): Increases the pets size by the amount (passed as a parameter to the method). Call the makeOlder() method to make the pet older by one cycle. - Implement exercise(int amount): Decreases the pets size by the amount. If the resulting size is smaller than 1, print a message "No more energy!" and set the size to 1. Call the makeOlder() method to make the pet older by one cycle. - Modify showPet(): Customize it any way you like but with the following requirements: It must show the name, age, and lifespan. It must depict the size and mood in some way (be creative). Optional: Feel free to tweak the behavior or add behaviors as long as they make sense and you let me know. To let me know what you changed or added, write comments for the methods and also display for the user the appropriate instructions if you make changes or additions. You will need to add cases to the switch statement and modify the DigiPetTester main method to support additional behaviors.

import java.util.Scanner;

public class DigiPetTester { static Scanner input = new Scanner(System.in); public static void main(String[] args) { System.out.print("Enter DigiPet name: "); String petName = input.next(); System.out.print("Enter lifespan: "); int petLifespan = input.nextInt(); DigiPet myPet = new DigiPet(petName, petLifespan); boolean keepGoing = true; while (myPet.getAge() <= myPet.getLifespan() && keepGoing) { System.out.println(); myPet.showPet(); System.out.print(" (P)et, po(K)e, (F)eed, (E)xercise, (Q)uit: "); String userCommand = input.next(); char command = userCommand.charAt(0); switch (command) { case 'P': case 'p': myPet.pet(); break; case 'K': case 'k': myPet.poke(); break; case 'F': case 'f': System.out.println("Feed how much? "); int amtFood = input.nextInt(); myPet.feed(amtFood); break; case 'E': case 'e': System.out.println("Exercise how much? "); int amtExercise = input.nextInt(); myPet.exercise(amtExercise); break; case 'Q': case 'q': keepGoing = false; break; default: System.out.println("Command not recognized."); } } System.out.println("Your pet expired :("); } }

public class DigiPet { // instance variables (representing the attributes/state of a DigiPet) private String name; private int lifespan; // max age (in cycles) private int age; // current age (in cycles, each action performed causes a cycle to pass) private int mood; // (2 = joyful, 1 = happy, 0 = neutral, -1 = sad, -2 = angry) private int size;

// Default constructor public DigiPet() { name = "(noname)"; lifespan = 12; age = 0; size = 1; mood = 0; }

// Overloaded constructor public DigiPet(String newName, int newLifespan) { // Initialize all five fields as appropriate: // Your code here: }

// Makes a pet one cylcle older and checks if the pet is at the end of its life private void makeOlder() { age++; if (age >= lifespan) { name = "Zombie " + name; } } // Accessor method for age public int getAge() { return age; } // Accessor method for lifespan public int getLifespan() { return lifespan; } public void poke() { // Your code here }

public void pet() { // Your code here } public void feed(int amount) { // Your code here }

public void exercise(int amount) { // Your code here }

// Modify this method to customize your pet's "appearance" public void showPet() { System.out.println(name + " (" + age + "/" + lifespan + ")"); System.out.print("~"); // tail for (int i = 0; i < size; i++) { System.out.print("#"); // body segment } System.out.print("@"); // head System.out.print(" --(i'm so "); switch (mood) { case 2: System.out.print("joyful!"); break; case 1: System.out.print("happy"); break; case -1: System.out.print("sad"); break; case -2: System.out.print("angry!"); break; default: System.out.print("meh"); } System.out.println(")"); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!