1) import [Link].
ArrayList;
import [Link];
import [Link];
public class VirtualPet {
private final String petId;
private String petName;
private String species;
private int age;
private int happiness;
private int health;
private int evolutionStage; // index in EVOLUTION_STAGES
private boolean isGhost;
private static final String[] EVOLUTION_STAGES = {"Egg", "Baby", "Child", "Teen", "Adult", "Elder"};
private static int totalPetsCreated = 0;
// Full constructor
public VirtualPet(String petName, String species, int age, int happiness, int health, int
evolutionStage) {
[Link] = generatePetId();
[Link] = petName;
[Link] = species;
[Link] = age;
[Link] = happiness;
[Link] = health;
[Link] = evolutionStage;
[Link] = false;
totalPetsCreated++;
}
// Constructor with name and species
public VirtualPet(String petName, String species) {
this(petName, species, 1, 50, 50, 2); // Start as Child
// Constructor with name only
public VirtualPet(String petName) {
this(petName, randomSpecies(), 0, 50, 50, 1); // Start as Baby
// Default constructor - mysterious egg
public VirtualPet() {
this("Mystery Egg", randomSpecies(), 0, 50, 50, 0);
// Generate unique pet ID
public static String generatePetId() {
return [Link]().toString();
private static String randomSpecies() {
String[] speciesList = {"Dragon", "Cat", "Dog", "Phoenix", "Unicorn"};
Random rand = new Random();
return speciesList[[Link]([Link])];
public void feedPet() {
if (isGhost) return;
happiness += 10;
health += 5;
[Link](petName + " was fed.");
}
public void playWithPet() {
if (isGhost) return;
happiness += 15;
health -= 5;
[Link](petName + " played and is happier.");
public void healPet() {
if (isGhost) return;
health += 20;
[Link](petName + " was healed.");
public void simulateDay() {
if (isGhost) return;
age++;
happiness -= new Random().nextInt(10);
health -= new Random().nextInt(10);
if (health <= 0) {
health = 0;
isGhost = true;
species = "Ghost";
[Link](petName + " has turned into a Ghost!");
} else {
evolvePet();
}
public void evolvePet() {
if (isGhost) return;
int targetStage = age / 3; // Simple logic: every 3 days advance a stage
if (targetStage >= EVOLUTION_STAGES.length) {
targetStage = EVOLUTION_STAGES.length - 1;
if (evolutionStage < targetStage) {
evolutionStage = targetStage;
[Link](petName + " has evolved to " + EVOLUTION_STAGES[evolutionStage] + "
stage!");
public String getPetStatus() {
return [Link](
"ID: %s | Name: %s | Species: %s | Age: %d | Happiness: %d | Health: %d | Stage: %s",
petId, petName, species, age, happiness, health,
isGhost ? "Ghost" : EVOLUTION_STAGES[evolutionStage]);
public static int getTotalPetsCreated() {
return totalPetsCreated;
// Main method - simulate pet daycare
public static void main(String[] args) {
ArrayList<VirtualPet> daycare = new ArrayList<>();
// Create multiple pets
[Link](new VirtualPet());
[Link](new VirtualPet("Fluffy"));
[Link](new VirtualPet("Spike", "Dog"));
[Link](new VirtualPet("Mystic", "Unicorn", 2, 70, 80, 3));
// Simulate 10 days
for (int day = 1; day <= 10; day++) {
[Link]("\n--- Day " + day + " ---");
for (VirtualPet pet : daycare) {
[Link]();
[Link]();
[Link]();
[Link]();
[Link]([Link]());
[Link]("\nTotal pets created: " + [Link]());
OUTPUT –
2) import [Link];
import [Link];
abstract class MagicalStructure {
protected String structureName;
protected int magicPower;
protected String location;
protected boolean isActive;
public MagicalStructure(String structureName) {
this(structureName, 100, "Unknown", true);
public MagicalStructure(String structureName, int magicPower, String location, boolean isActive) {
[Link] = structureName;
[Link] = magicPower;
[Link] = location;
[Link] = isActive;
public abstract void castMagicSpell();
class WizardTower extends MagicalStructure {
private int spellCapacity;
private String[] knownSpells;
public WizardTower(String structureName) {
this(structureName, 100, new String[]{"Basic Spell"});
public WizardTower(String structureName, String[] knownSpells) {
this(structureName, 100, knownSpells);
public WizardTower(String structureName, int spellCapacity, String[] knownSpells) {
super(structureName, 200, "Tower Location", true);
[Link] = spellCapacity;
[Link] = knownSpells;
}
@Override
public void castMagicSpell() {
[Link](structureName + " casts powerful magic using " +
[Link](knownSpells));
public int getSpellCapacity() {
return spellCapacity;
public void doubleSpellCapacity() {
spellCapacity *= 2;
class EnchantedCastle extends MagicalStructure {
private int defenseRating;
private boolean hasDrawbridge;
public EnchantedCastle(String structureName) {
this(structureName, 150, false);
public EnchantedCastle(String structureName, int defenseRating, boolean hasDrawbridge) {
super(structureName, 150, "Castle Grounds", true);
[Link] = defenseRating;
[Link] = hasDrawbridge;
}
@Override
public void castMagicSpell() {
[Link](structureName + " reinforces defenses magically.");
public int getDefenseRating() {
return defenseRating;
public void tripleDefense() {
defenseRating *= 3;
class MysticLibrary extends MagicalStructure {
private int bookCount;
private String ancientLanguage;
public MysticLibrary(String structureName) {
this(structureName, 50, "Ancient Tongue");
public MysticLibrary(String structureName, int bookCount, String ancientLanguage) {
super(structureName, 80, "Library Location", true);
[Link] = bookCount;
[Link] = ancientLanguage;
@Override
public void castMagicSpell() {
[Link](structureName + " reveals ancient knowledge in " + ancientLanguage);
}
class DragonLair extends MagicalStructure {
private String dragonType;
private int treasureValue;
public DragonLair(String structureName, String dragonType, int treasureValue) {
super(structureName, 300, "Cave Location", true);
[Link] = dragonType;
[Link] = treasureValue;
@Override
public void castMagicSpell() {
[Link](structureName + " unleashes dragon fire of type " + dragonType);
class KingdomManager {
public static boolean canStructuresInteract(MagicalStructure s1, MagicalStructure s2) {
return !(s1 instanceof DragonLair && s2 instanceof DragonLair); // DragonLairs do not interact
with each other
public static String performMagicBattle(MagicalStructure attacker, MagicalStructure defender) {
int attackerPower = [Link];
int defenderPower = [Link];
if (attacker instanceof WizardTower && defender instanceof MysticLibrary) {
attackerPower += 50; // WizardTower advantage
return attackerPower > defenderPower ? [Link] + " wins!" :
[Link] + " withstands the attack!";
public static int calculateKingdomMagicPower(MagicalStructure[] structures) {
int totalPower = 0;
boolean hasTower = false;
boolean hasLibrary = false;
for (MagicalStructure s : structures) {
totalPower += [Link];
if (s instanceof WizardTower) hasTower = true;
if (s instanceof MysticLibrary) hasLibrary = true;
if (hasTower && hasLibrary) {
totalPower *= 1.5; // Knowledge boost effect
return totalPower;
public static void applySpecialEffects(MagicalStructure[] structures) {
for (MagicalStructure s : structures) {
if (s instanceof WizardTower) {
for (MagicalStructure other : structures) {
if (other instanceof WizardTower && other != s) {
((WizardTower) s).doubleSpellCapacity();
if (s instanceof EnchantedCastle) {
for (MagicalStructure other : structures) {
if (other instanceof DragonLair) {
((EnchantedCastle) s).tripleDefense();
public static void categorizeStructures(MagicalStructure[] structures) {
[Link]("\n--- Kingdom Structure Summary ---");
int towers = 0, castles = 0, libraries = 0, lairs = 0;
for (MagicalStructure s : structures) {
if (s instanceof WizardTower) towers++;
else if (s instanceof EnchantedCastle) castles++;
else if (s instanceof MysticLibrary) libraries++;
else if (s instanceof DragonLair) lairs++;
[Link]("Wizard Towers: " + towers);
[Link]("Enchanted Castles: " + castles);
[Link]("Mystic Libraries: " + libraries);
[Link]("Dragon Lairs: " + lairs);
}
public static void determineKingdomSpecialization(MagicalStructure[] structures) {
int magicPower = 0, defensePower = 0;
for (MagicalStructure s : structures) {
magicPower += [Link];
if (s instanceof EnchantedCastle) defensePower += ((EnchantedCastle) s).getDefenseRating();
if (magicPower > defensePower) {
[Link]("Kingdom Specialization: Magic-focused");
} else {
[Link]("Kingdom Specialization: Defense-focused");
public class MedievalKingdomSimulator {
public static void main(String[] args) {
MagicalStructure[] kingdomStructures = {
new WizardTower("Arcane Tower", 5, new String[]{"Fireball", "Teleport"}),
new EnchantedCastle("Iron Keep", 300, true),
new MysticLibrary("Grand Archive", 5000, "Elder Script"),
new DragonLair("Red Dragon Lair", "Fire Drake", 10000)
};
[Link](kingdomStructures);
[Link](kingdomStructures);
[Link]("\nTotal Magic Power: " +
[Link](kingdomStructures));
[Link](kingdomStructures);
[Link]("\nPerforming a Magic Battle:");
[Link]([Link](kingdomStructures[0],
kingdomStructures[1]));
OUTPUT –