Skip to Content
CoursesCSC102ระบบสวนสัตว์เวทมนตร์

ระบบสวนสัตว์เวทมนตร์

โจทย์

In the enchanted world of Java, there exists a magical zoo where every creature is unique, possessing distinct attributes and abilities. The zoo’s caretaker, Master Encapsulator, has requested your help to manage and calculate the powers of these mystical beings. Your task is to design a system that leverages the principles of Object-Oriented Programming, specifically focusing on encapsulation and polymorphism.

The zoo houses three types of creatures: Dragons, Phoenixes, and Unicorns. Each creature type has unique attributes that determine its power, which the caretaker needs to calculate efficiently.

You are required to implement a Java program that models these creatures with strict encapsulation of attributes, and well-defined methods to interact with those attributes. The program should also demonstrate the use of polymorphism by calculating the total power of all creatures in the zoo.

Class Descriptions:

  • Create a base class called “Creature” class that contains:
    • Attributes
      • A private String attribute named name, representing the name of the creature.
      • A private integer attribute named age, representing the age of the creature.
    • Method
      • calculatePower() that returns the power of the creature. This method should be overridden by subclasses to provide specific implementations.
  • Create a subclass called “Dragon” class that contains:
    • Attributes
      • A private integer attribute named numberOfWings, representing the number of wings of Dragon has.
      • A private integer attribute named flameIntensity, representing the intensity of the Dragon’s fire-breathing ability.
    • Encapsulation
      • All attributes should be private.
      • Provide public getters and setters for each attribute to allow controlled access and modification.
    • Method
      • calculatePower() that calculates the power as numberOfWings * flameIntensity.
  • Create a subclass called “Phoenix” class that contains:
    • Attributes
      • A private integer attribute named rebirthCount, representing the number of times the Phoenix has risen from the ashes.
    • Encapsulation
      • All attributes should be private.
      • Provide public getters and setters for each attribute to allow controlled access and modification.
    • Method
      • calculatePower() that calculates the power as rebirthCount * 100.
  • Create a subclass called “Unicorn” class that contains:
    • Attributes
      • A private integer attribute named hornLength, representing the length of the Unicorn’s horn.
      • A private integer attribute named healingPower, representing the effectiveness of the Unicorn’s healing ability.
    • Encapsulation
      • All attributes should be private.
      • Provide public getters and setters for each attribute to allow controlled access and modification.
    • Method
      • calculatePower() that calculates the power as hornLength * healingPower.

โค้ดเริ่มต้น

public class CreatureDriver { public static void main(String[] args) { // Creating Dragon objects Scanner sc = new Scanner(System.in); // Get input for Dragon String dragonName = sc.next(); int dragonAge = sc.nextInt(); int dragonWing = sc.nextInt(); int dragonPower = sc.nextInt(); Dragon dragon1 = new Dragon(dragonName, dragonAge, dragonWing, dragonPower); // Get input for Phoenix String phoenixName = sc.next(); int phoenixAge = sc.nextInt(); int phoenixPower = sc.nextInt(); Phoenix phoenix1 = new Phoenix(phoenixName, phoenixAge, phoenixPower); // Get input for Unicorn String unicornName = sc.next(); int unicornAge = sc.nextInt(); int unicornHorn = sc.nextInt(); int unicornPower = sc.nextInt(); Unicorn unicorn1 = new Unicorn(unicornName, unicornAge, unicornHorn, unicornPower); // Displaying each creature's details dragon1.displayDetails(); phoenix1.displayDetails(); unicorn1.displayDetails(); // Calculating and displaying the total power of all creatures int totalPower = dragon1.calculatePower() + phoenix1.calculatePower() + unicorn1.calculatePower(); System.out.println("Total Power of the Zoo: " + totalPower); } } class Creature{ // Write your code here } class Dragon{ // Write your code here } class Phoenix{ // Write your code here } class Unicorn{ // Write your code here }

โค้ด

import java.util.Scanner; public class CreatureDriver { public static void main(String[] args) { // Creating Dragon objects Scanner sc = new Scanner(System.in); // Get input for Dragon String dragonName = sc.next(); int dragonAge = sc.nextInt(); int dragonWing = sc.nextInt(); int dragonPower = sc.nextInt(); Dragon dragon1 = new Dragon(dragonName, dragonAge, dragonWing, dragonPower); // Get input for Phoenix String phoenixName = sc.next(); int phoenixAge = sc.nextInt(); int phoenixPower = sc.nextInt(); Phoenix phoenix1 = new Phoenix(phoenixName, phoenixAge, phoenixPower); // Get input for Unicorn String unicornName = sc.next(); int unicornAge = sc.nextInt(); int unicornHorn = sc.nextInt(); int unicornPower = sc.nextInt(); Unicorn unicorn1 = new Unicorn(unicornName, unicornAge, unicornHorn, unicornPower); // Displaying each creature's details dragon1.displayDetails(); phoenix1.displayDetails(); unicorn1.displayDetails(); // Calculating and displaying the total power of all creatures int totalPower = dragon1.calculatePower() + phoenix1.calculatePower() + unicorn1.calculatePower(); System.out.println("Total Power of the Zoo: " + totalPower); } } class Creature { private String name; private int age; public Creature(String name, int age) { this.name = name; this.age = age; } public String getName() { return this.name; } public int getAge() { return this.age; } public void setName(String name) { this.name = name; } public void setAge(int age) { this.age = age; } int calculatePower() { return 0; } public void displayDetails() { System.out.printf("Name: %s, Age: %d, Power: %d\n", this.name, this.age, this.calculatePower()); } } class Dragon extends Creature { private int numberOfWings; private int flameIntensity; public Dragon(String name, int age, int numberOfWings, int flameIntensity) { super(name, age); this.numberOfWings = numberOfWings; this.flameIntensity = flameIntensity; } public int getNumberOfWings() { return this.numberOfWings; } public int getFlameIntensity() { return this.flameIntensity; } public void setNumberOfWings(int numberOfWings) { this.numberOfWings = numberOfWings; } public void setFlameIntensity(int flameIntensity) { this.flameIntensity = flameIntensity; } public int calculatePower() { return this.getNumberOfWings() * this.getFlameIntensity(); } } class Phoenix extends Creature { private int rebirthCount; public Phoenix(String name, int age, int rebirthCount) { super(name, age); this.rebirthCount = rebirthCount; } public int getRebirthCount() { return this.rebirthCount; } public void setRebirthCount(int rebirthCount) { this.rebirthCount = rebirthCount; } public int calculatePower() { return this.rebirthCount * 100; } } class Unicorn extends Creature { private int hornLength; private int healingPower; public Unicorn(String name, int age, int hornLength, int healingPower) { super(name, age); this.hornLength = hornLength; this.healingPower = healingPower; } public int getHornLength() { return this.hornLength; } public int getHealingPower() { return this.healingPower; } public void setHornLength(int hornLength) { this.hornLength = hornLength; } public void setHealingPower(int healingPower) { this.healingPower = healingPower; } public int calculatePower() { return this.hornLength * this.healingPower; } }

คำอธิบาย

โครงสร้างของระบบ

คลาส Creature (คลาสแม่)

คลาสพื้นฐานสำหรับสิ่งมีชีวิตทุกชนิดในสวนสัตว์เวทมนตร์

class Creature { private String name; // ชื่อของสิ่งมีชีวิต private int age; // อายุ // Constructor public Creature(String name, int age) { this.name = name; this.age = age; } // แสดงรายละเอียด public void displayDetails() { System.out.printf("Name: %s, Age: %d, Power: %d\n", this.name, this.age, this.calculatePower()); } }

คลาส Dragon (มังกร)

class Dragon extends Creature { private int numberOfWings; // จำนวนปีก private int flameIntensity; // ความร้อนของไฟ // Constructor public Dragon(String name, int age, int numberOfWings, int flameIntensity) { super(name, age); this.numberOfWings = numberOfWings; this.flameIntensity = flameIntensity; } // คำนวณพลัง = จำนวนปีก × ความร้อนของไฟ public int calculatePower() { return this.numberOfWings * this.flameIntensity; } }

การคำนวณพลังของมังกร:

  • สูตร: จำนวนปีก × ความร้อนของไฟ
  • ตัวอย่าง: มังกรมี 4 ปีก และความร้อนไฟ 50
  • พลัง = 4 × 50 = 200 หน่วย

คลาส Phoenix (ฟีนิกซ์)

class Phoenix extends Creature { private int rebirthCount; // จำนวนครั้งที่เกิดใหม่ // Constructor public Phoenix(String name, int age, int rebirthCount) { super(name, age); this.rebirthCount = rebirthCount; } // คำนวณพลัง = จำนวนครั้งที่เกิดใหม่ × 100 public int calculatePower() { return this.rebirthCount * 100; } }

การคำนวณพลังของฟีนิกซ์:

  • สูตร: จำนวนครั้งที่เกิดใหม่ × 100
  • ตัวอย่าง: ฟีนิกซ์เกิดใหม่ 3 ครั้ง
  • พลัง = 3 × 100 = 300 หน่วย

คลาส Unicorn (ยูนิคอร์น)

class Unicorn extends Creature { private int hornLength; // ความยาวเขา private int healingPower; // พลังการรักษา // Constructor public Unicorn(String name, int age, int hornLength, int healingPower) { super(name, age); this.hornLength = hornLength; this.healingPower = healingPower; } // คำนวณพลัง = ความยาวเขา × พลังการรักษา public int calculatePower() { return this.hornLength * this.healingPower; } }

การคำนวณพลังของยูนิคอร์น:

  • สูตร: ความยาวเขา × พลังการรักษา
  • ตัวอย่าง: ยูนิคอร์นมีเขายาว 10 หน่วย และพลังรักษา 25
  • พลัง = 10 × 25 = 250 หน่วย

ตัวอย่างการทำงาน

การใช้งานระบบผ่าน CreatureDriver:

1. การสร้างสิ่งมีชีวิต

// สร้างมังกร Dragon dragon1 = new Dragon("Draco", 100, 4, 50); // สร้างฟีนิกซ์ Phoenix phoenix1 = new Phoenix("Fawkes", 200, 3); // สร้างยูนิคอร์น Unicorn unicorn1 = new Unicorn("Luna", 50, 10, 25);

2. การแสดงข้อมูลและคำนวณพลังรวม

// แสดงรายละเอียดของแต่ละสิ่งมีชีวิต dragon1.displayDetails(); phoenix1.displayDetails(); unicorn1.displayDetails(); // คำนวณพลังรวมทั้งหมด int totalPower = dragon1.calculatePower() + phoenix1.calculatePower() + unicorn1.calculatePower();
ปรับปรุงล่าสุด