Question

Problem 5: Monster Factory (10 points) (Game Dev) Create a Monster class that maintains a count...

Problem 5: Monster Factory (10 points) (Game Dev) Create a Monster class that maintains a count of all monsters instantiated and includes a static method that generates a new random monster object. In software engineering, a method that generates new instances of classes based on configuration information is called the Factory pattern. UML Class Diagram: Monster - name: String - health: int - strength: int - xp: int + spawn(type:String): Monster + constructor (name: String, health: int, strength: int, xp: int) + getName(): String + getHealth(): int + getStrength(): int + getXP(): int + takeDamage(damage: int) + attack(hero: Player) + toString(): String Monster Constructor Summary: Constructor Description Monster(String name, int health, int strength, int xp) Creates Monster object with given name, health, strength. and xp. Monster Method API: Modifier and Type Method and Description void attack(Player hero) Monster attacks player, where player takes damage equal to monster strength. Display message: "%s attacks player for %d damage", name, strength int getHealth() Returns this monster's health String getName() Returns this monster's name int getStrength() Returns this monster's strength int getXP() Returns this monster's experience static Monster spawn(String type) Returns a monster object of given type for "goblin", "orc", or "troll" void takeDamage(int damage) health is decreased by given damage, but can't be a negative String toString() Returns a text representation of this account, formatted as: "[%s] HP: %d, STR: %d", name, health, strength Facts ● Monster types that may be spawned with following attribute values: ○ goblin, name="goblin", health=60, strength=8, xp=1 ○ orc, name="orc", health=100, strength=12, xp=3 ○ troll, name="troll", health=150, strength=15, xp=5 ● monster's attack() should invoke player's takeDamage() method. ● Consider using String.format() in your toString() Software Architecture: The Monster class is designed to be instantiated by Game class which is used to make Monster objects and Player objects. See the UML object diagram below: UML Object Diagram Tester Files: Use the MonsterTester.java file to test your implementation. Use the Game.java app to play a simple game using your Monster class. Sample Method Calls Sample Method Results Monster goblin = Monster.spawn("goblin"); Monster orc = Monster.spawn("orc"); Monster troll = Monster.spawn("troll"); System.out.println(goblin); System.out.println(orc); System.out.println(troll); [goblin] HP:60, STR:8 [orc] HP:100, STR:12 [troll] HP:150, STR:15

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solution to above problem is given below in their respective java files.

The implementation logic of the problem is provided in code comments of the program. In case of any problem regarding the implementation of question, write into comment section.

Monster.java

public class Monster { /* Instance variables / private String name; private int health; private int strength; private int xp; /* * @param name * @param health * @param strength * @param xp / public Monster(String name, int health, int strength, int xp) { super(); this.name = name; this.health = health; this.strength = strength; this.xp = xp; } / return monster object of given monsterName / public static Monster spawn(String monsterName) { if(monsterName == "goblin") { return new Monster("goblin", 60, 8, 1); } if(monsterName == "orc") { return new Monster("orc", 100, 12, 3); } return new Monster("troll", 150, 15, 5); } / getter to return name of the monster / public String getName() { return name; } / getter to return health of the monster / public int getHealth() { return health; } / getter to return strength of the monster / public int getStrength() { return strength; } / getter to return the XP of the monster / public int getXp() { return xp; } / this method decreases the health by given damage points, this method also * checks that damage should not be negative / public void takeDamage(int damage) { if(damage > 0){ health = health - damage; } } @Override public String toString() { return String.format("[%s] HP: %d. STR: %d",name, health, strength); } / this method implements the functionality of attack(int damage) method, * monster attacks player, where player takes damage equal to monster strength */ public void attack(Player hero) { hero.takeDamage(this.strength); System.out.println(String.format("%s attacks player for %d damage", name, strength)); } }
Player.java

/* * Player class is implemented on user understanding of the scenario, because * there is no information provided how player class should be implemented. * This class needs to be implemented to ensure the working of the attack(int: damage) method present in monster Class * / public class Player{ / Instance variables */ private String playerName; private int playerHealth; public String getPlayerName() { return playerName; } public void setPlayerName(String playerName) { this.playerName = playerName; } public int getPlayerHealth() { return playerHealth; } public void setPlayerHealth(int playerHealth) { this.playerHealth = playerHealth; } public void takeDamage(int damage) { playerHealth = playerHealth - damage; } }
MonsterTester.java

public class MonsterTester { public static void main(String[] args) { Monster goblin = Monster.spawn("goblin"); Monster orc = Monster.spawn("orc"); Monster troll = Monster.spawn("troll"); System.out.println(goblin); System.out.println(orc); System.out.println(troll); } }
Output:

HA T I - - - 1 Monstertva yergans Monstercatorja: 1 package cheee_apr_22; XXX 22.23 $5126 AM Gutie 0 22. bitan Console X cami

Add a comment
Know the answer?
Add Answer to:
Problem 5: Monster Factory (10 points) (Game Dev) Create a Monster class that maintains a count...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT