virtual_pet
This is an old revision of the document!
Virtual Pet
- VirtualPet.java
public class VirtualPet { /* * Activity 3 * @author (your name) * @version (a version number or a date) * * enter code for instance variables, constructor, * getName, and toString methods * **/ public VirtualPet(String str) { } public String getName(){ return ""; } }
- VirtualPetRunner.java
import java.util.Scanner; //Activity 3 /* remove comment for part C import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; */ public class VirtualPetRunner { // Prints menu and returns user choice public static int getChoice(Scanner input) { int selection = 0; while (selection < 1 || selection > 4) { System.out.println("------Virtual Pet Menu------"); System.out.println("1. Get Pet Information"); System.out.println("2. Feed Pet" ); System.out.println("3. Play with Pet" ); System.out.println("4. Quit" ); System.out.print("Enter your choice ..... "); selection = input.nextInt(); } return selection; } // Displays a picture of the pet public static void printPet(String emo) { System.out.println(" /\\_/\\"); System.out.println("( o.o )"); System.out.println(" > " + emo + " <"); } public static void main(String[] args) { // CHANGE THIS VARIABLE VALUE TO TEST AT A DIFFERENT SPEED final int INTERVAL_IN_SECONDS = 10; // Sets up Scanner for user input Scanner input = new Scanner(System.in); VirtualPet myPet = new VirtualPet("Coco"); /* Remove comment for part C // Sets up a ScheduledExecutorService object that will call updateStatus // every 1 minute. ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); scheduler.scheduleAtFixedRate(() -> { myPet.updateStatus(); }, INTERVAL_IN_SECONDS, INTERVAL_IN_SECONDS, TimeUnit.SECONDS); */ System.out.println(myPet); printPet("ᵔ"); int choice = getChoice(input); while (choice != 4) { if (choice == 1) { System.out.println(myPet); } else if (choice == 2) { /* remove comment for part B myPet.feed(); System.out.println("\n\nYou have fed " + myPet.getName()); */ } else if (choice == 3) { /* remove comment for part C myPet.play(); System.out.println("\n\nYou have played with " + myPet.getName()); */ } /* remove comment for part B if (myPet.getEnergyLevel() >= 5 && myPet.getHappinessLevel() >= 5) { printPet("ᵕ"); } else { printPet("ᵔ"); } */ System.out.println(myPet.getName().toUpperCase()); choice = getChoice(input); } /* remove comment for part C scheduler.shutdown(); */ } }
Activity 4
- Game.java
/** * A Game object is created with parameters that define the name of the game, * the increase in happiness level, and the amount of weight lost * when the game is played. * * The Game class should have a constructor with the following header: * public Game(String name, int happinessIncr, int weightDecr). * * The Game class also has accessor methods for all instance variables * and a method boolean isWinner() that has a 50% chance of returning true * and a 50% chance of returning false. * Write the complete Game class, including the constructor * and any required instance variables and their accessor method and the method isWinner(). Your implementation should meet all the specifications outlined above. */ public class Game { }
virtual_pet.1772130954.txt.gz · Last modified: by frchris
