/* * File: CyberPet.java<2> * Author: Java, Java, Java * Description: This class represents a CyberPet that can * eat and sleep on command. This version incorporates * constructor methods to initialize the pet's state. * Constructors have the same name as the class itself and * cannot return a value. They are used to create instances * (objects) of the class. */ public class CyberPet { // Data private boolean isEating = true; // CyberPet's state private boolean isSleeping = false; private String name = "no name"; // CyberPet's name // Methods public CyberPet() {} // Explicit default constructor public CyberPet(String str) // Constructor method { name = str; } public void setName(String str) // Access method { name = str; } // setName() public String getName() // Access method { return name; // Return CyberPet's name } // getName() public void eat() // Start eating { isEating = true; // Change the state isSleeping = false; System.out.println(name + " is eating"); return; } // eat() public void sleep() // Start sleeping { isSleeping = true; // Change the state isEating = false; System.out.println(name + " is sleeping"); return; } // sleep() } // CyberPet