1) import java.util.
ArrayList;
import java.util.Random;
import java.util.UUID;
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) {
this.petId = generatePetId();
this.petName = petName;
this.species = species;
this.age = age;
this.happiness = happiness;
this.health = health;
this.evolutionStage = evolutionStage;
this.isGhost = 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 UUID.randomUUID().toString();
private static String randomSpecies() {
String[] speciesList = {"Dragon", "Cat", "Dog", "Phoenix", "Unicorn"};
Random rand = new Random();
return speciesList[rand.nextInt(speciesList.length)];
public void feedPet() {
if (isGhost) return;
happiness += 10;
health += 5;
System.out.println(petName + " was fed.");
}
public void playWithPet() {
if (isGhost) return;
happiness += 15;
health -= 5;
System.out.println(petName + " played and is happier.");
public void healPet() {
if (isGhost) return;
health += 20;
System.out.println(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";
System.out.println(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;
System.out.println(petName + " has evolved to " + EVOLUTION_STAGES[evolutionStage] + "
stage!");
public String getPetStatus() {
return String.format(
"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
daycare.add(new VirtualPet());
daycare.add(new VirtualPet("Fluffy"));
daycare.add(new VirtualPet("Spike", "Dog"));
daycare.add(new VirtualPet("Mystic", "Unicorn", 2, 70, 80, 3));
// Simulate 10 days
for (int day = 1; day <= 10; day++) {
System.out.println("\n--- Day " + day + " ---");
for (VirtualPet pet : daycare) {
pet.simulateDay();
pet.feedPet();
pet.playWithPet();
pet.healPet();
System.out.println(pet.getPetStatus());
System.out.println("\nTotal pets created: " + VirtualPet.getTotalPetsCreated());
OUTPUT –
2) import java.util.ArrayList;
import java.util.Arrays;
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) {
this.structureName = structureName;
this.magicPower = magicPower;
this.location = location;
this.isActive = 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);
this.spellCapacity = spellCapacity;
this.knownSpells = knownSpells;
}
@Override
public void castMagicSpell() {
System.out.println(structureName + " casts powerful magic using " +
Arrays.toString(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);
this.defenseRating = defenseRating;
this.hasDrawbridge = hasDrawbridge;
}
@Override
public void castMagicSpell() {
System.out.println(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);
this.bookCount = bookCount;
this.ancientLanguage = ancientLanguage;
@Override
public void castMagicSpell() {
System.out.println(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);
this.dragonType = dragonType;
this.treasureValue = treasureValue;
@Override
public void castMagicSpell() {
System.out.println(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 = attacker.magicPower;
int defenderPower = defender.magicPower;
if (attacker instanceof WizardTower && defender instanceof MysticLibrary) {
attackerPower += 50; // WizardTower advantage
return attackerPower > defenderPower ? attacker.structureName + " wins!" :
defender.structureName + " withstands the attack!";
public static int calculateKingdomMagicPower(MagicalStructure[] structures) {
int totalPower = 0;
boolean hasTower = false;
boolean hasLibrary = false;
for (MagicalStructure s : structures) {
totalPower += s.magicPower;
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) {
System.out.println("\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++;
System.out.println("Wizard Towers: " + towers);
System.out.println("Enchanted Castles: " + castles);
System.out.println("Mystic Libraries: " + libraries);
System.out.println("Dragon Lairs: " + lairs);
}
public static void determineKingdomSpecialization(MagicalStructure[] structures) {
int magicPower = 0, defensePower = 0;
for (MagicalStructure s : structures) {
magicPower += s.magicPower;
if (s instanceof EnchantedCastle) defensePower += ((EnchantedCastle) s).getDefenseRating();
if (magicPower > defensePower) {
System.out.println("Kingdom Specialization: Magic-focused");
} else {
System.out.println("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)
};
KingdomManager.applySpecialEffects(kingdomStructures);
KingdomManager.categorizeStructures(kingdomStructures);
System.out.println("\nTotal Magic Power: " +
KingdomManager.calculateKingdomMagicPower(kingdomStructures));
KingdomManager.determineKingdomSpecialization(kingdomStructures);
System.out.println("\nPerforming a Magic Battle:");
System.out.println(KingdomManager.performMagicBattle(kingdomStructures[0],
kingdomStructures[1]));
OUTPUT –