Question

In: Computer Science

Implement a Factory Design Pattern for the code below: MAIN: import java.util.ArrayList; import java.util.List; import java.util.Random;...

Implement a Factory Design Pattern for the code below:

MAIN:

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Main {
public static void main(String[] args) {
Character char1 = new Orc("Grumlin");
Character char2 = new Elf("Therae");

int damageDealt = char1.attackEnemy();
System.out.println(char1.name + " has attacked an enemy " +
"and dealt " + damageDealt + " damage");

char1.hasCastSpellSkill = true;

damageDealt = char1.attackEnemy();
System.out.println(char1.name + " has attacked an enemy " +
"and dealt " + damageDealt + " damage");

int damageTaken = char2.takeHit();
System.out.println(char2.name + " has taken a hit and " +
"been dealt " + damageTaken + " damage");

char2.hasDodgeAttackSkill = true;

damageTaken = char2.takeHit();
System.out.println(char2.name + " has taken a hit and " +
"been dealt " + damageTaken + " damage");
}
}

CHARACTER:

import java.util.Random;

public abstract class Character {
protected String name;
protected int strength;
protected int resilience;
protected boolean hasCastSpellSkill;
protected boolean hasDodgeAttackSkill;

public int attackEnemy() {
Random random = new Random();

int damageDealt;
if (hasCastSpellSkill) {
int spellDamage = random.nextInt(5);
damageDealt = this.strength + spellDamage;
} else {
damageDealt = strength;
}
return damageDealt;
}

public int takeHit() {
Random random = new Random();

int damageDealt = random.nextInt(15);
int damageTaken;
if (hasDodgeAttackSkill) {
double chanceToDodge = random.nextDouble();

if (chanceToDodge > 0.50) {
damageTaken = 0;
} else {
damageTaken = damageDealt - resilience;
}
} else {
damageTaken = damageDealt - resilience;
}

if (damageTaken < 0) {
damageTaken = 0;
}
return damageTaken;
}
}

ELF:

public class Elf extends Character {
public Elf(String name) {
this.name = name;
this.strength = 4;
this.resilience = 2;
}
}

ORC:

public class Orc extends Character {
public Orc(String name) {
this.name = name;
this.strength = 10;
this.resilience = 9;
}
}

Solutions

Expert Solution

import java.util.ArrayList;
import java.util.List;
import java.util.Random;


class CharacterFactory {
    
    public static Character createOrc() {
        return new Orc("Grumlin");
    }
    public static Character createElf() {
        return new Elf("Therae");
    }
}


public class Main {
    public static void main(String[] args) {
        Character char1 = CharacterFactory.createOrc();
        Character char2 = CharacterFactory.createElf();


        int damageDealt = char1.attackEnemy();
        System.out.println(char1.name + " has attacked an enemy " +
        "and dealt " + damageDealt + " damage");

        char1.hasCastSpellSkill = true;

        damageDealt = char1.attackEnemy();
        System.out.println(char1.name + " has attacked an enemy " +
        "and dealt " + damageDealt + " damage");

        int damageTaken = char2.takeHit();
        System.out.println(char2.name + " has taken a hit and " +
        "been dealt " + damageTaken + " damage");

        char2.hasDodgeAttackSkill = true;

        damageTaken = char2.takeHit();
        System.out.println(char2.name + " has taken a hit and " +
        "been dealt " + damageTaken + " damage");
    }
}

**************************************************

I have tried to answer your question to best of my efforts. However, if you still face any issues in the answer, please let me know via the comment section. Your feedback will imporve as well as encourage me to keep up the good work.

If i was able to help you, then please provide me an upvote(thumbs up). Thanks!


Related Solutions

Implement a Composite Design Pattern for the code below that creates a family tree MAIN: public...
Implement a Composite Design Pattern for the code below that creates a family tree MAIN: public class Main { public static void main(String[] args) { /* Let's create a family tree (for instance like one used on genealogy sites). For the sake of simplicity, assume an individual can have at most two children. If an individual has 1-2 children, they are considered a "tree". If an individual does not have children, they are considered a "person". With that in mind,...
This is the code that needs to be completed... import java.util.ArrayList; import java.util.Collections; /** * Write...
This is the code that needs to be completed... import java.util.ArrayList; import java.util.Collections; /** * Write a description of class SpellChecker here. * * @author (your name) * @version (a version number or a date) */ public class SpellChecker { private ArrayList words; private DictReader reader; /** * Constructor for objects of class SpellChecker */ public SpellChecker() { reader = new DictReader("words.txt"); words = reader.getDictionary(); } /** * This method returns the number of words in the dictionary. * Change...
My code: import java.util.Random; import java.util.Scanner; public class RollDice { public static void main(String[] args) {...
My code: import java.util.Random; import java.util.Scanner; public class RollDice { public static void main(String[] args) { int N; Scanner keybd = new Scanner(System.in); int[] counts = new int[12];    System.out.print("Enter the number of trials: "); N = keybd.nextInt();    Random die1 = new Random(); Random die2 = new Random(); int value1, value2, sum; for(int i = 1; i <= N; i++) { value1 = die1.nextInt(6) + 1; value2 = die2.nextInt(6) + 1; sum = value1 + value2; counts[sum-1]++; }   ...
import java.util.ArrayList; import java.util.Collections; public class BirthdayList { /** * This is the main process for...
import java.util.ArrayList; import java.util.Collections; public class BirthdayList { /** * This is the main process for class BirthdayList */ public static void main() { System.out.println("\n\tBegin Birthday List Program\n");    System.out.println("Declare an ArrayList for Birthday objects"); // 1. TO DO: Declare an ArrayList to store Birthday objects (3 Pts)       // 2. TO DO: Replace the xxx.xxxx() with the appropriate method (2 Pts) System.out.printf("\nBirthday List is empty: %s\n", xxx.xxxx() ? "True" : "False");    System.out.println("Add at least five Birthdays to...
This is my code I need to complete it? //The code package arraylists; import java.util.ArrayList; import...
This is my code I need to complete it? //The code package arraylists; import java.util.ArrayList; import java.util.Scanner; /** * * */ public class SoftOpening {    public static void main(String[] args) {       Scanner input = new Scanner(System.in);    ArrayList foodList = generateMenu();    User user = generateUser(input); user.introduce();    userBuyFood(foodList, user, input); user.introduce(); } public static ArrayList generateMenu(){    ArrayList foodList = new ArrayList<>();    Food pizza1 =new Food(1,"pizza","Seafood",11,12); Food pizza2 =new Food(2,"pizza","Beef",9,10); Food Friedrice =new Food(3,"fried rice","Seafood",5,12);...
import java.util.ArrayList; import java.util.Collections; import java.lang.Exception; public class ItemList { /** This is the main process...
import java.util.ArrayList; import java.util.Collections; import java.lang.Exception; public class ItemList { /** This is the main process for the project */ public static void main () { System.out.println("\n\tBegin Item List Demo\n"); System.out.println("Declare an ArrayList to hold Item objects"); ArrayList<Item> list = new ArrayList<Item>(); try { System.out.println("\n Add several Items to the list"); list.add(new Item(123, "Statue")); list.add(new Item(332, "Painting")); list.add(new Item(241, "Figurine")); list.add(new Item(126, "Chair")); list.add(new Item(411, "Model")); list.add(new Item(55, "Watch")); System.out.println("\nDisplay original Items list:"); listItems(list); int result = -1; // 1....
Convert this pseudo code program into sentences. import java.util.ArrayList; import java.util.Collections; class Bulgarian {    public...
Convert this pseudo code program into sentences. import java.util.ArrayList; import java.util.Collections; class Bulgarian {    public static void main(String[] args)    {        max_cards=45;        arr->new ArraryList        col=1;        card=0;        left=max_cards;        do{            col->random number            row->new ArrayList;            for i=0 to i<col            {                card++                add card into row            }   ...
With the code that is being tested is: import java.util.Random; public class GVdate { private int...
With the code that is being tested is: import java.util.Random; public class GVdate { private int month; private int day; private int year; private final int MONTH = 1; private final int DAY = 9; private static Random rand = new Random(); /** * Constructor for objects of class GVDate */ public GVdate() { this.month = rand.nextInt ( MONTH) + 1; this.day = rand.nextInt ( DAY );    } public int getMonth() {return this.month; } public int getDay() {return this.day;...
create code for deletestudentbyID Number for choice == 4 package courseecom616; import java.util.Scanner; import java.util.ArrayList; public...
create code for deletestudentbyID Number for choice == 4 package courseecom616; import java.util.Scanner; import java.util.ArrayList; public class CourseeCOM616 {        private String courseName;     private String[] studentsName = new String[1];     private String studentId;        private int numberOfStudents;        public CourseeCOM616(String courseName) {         this.courseName = courseName;     }     public String[] getStudentsName() {         return studentsName;     }     public int getNumberOfStudents() {         return numberOfStudents;     }     public String getStudentId() {         return studentId;    ...
NEed UML diagram for this java code: import java.util.ArrayList; import java.util.Scanner; class ToDoList { private ArrayList<Task>...
NEed UML diagram for this java code: import java.util.ArrayList; import java.util.Scanner; class ToDoList { private ArrayList<Task> list;//make private array public ToDoList() { //this keyword refers to the current object in a method or constructor this.list = new ArrayList<>(); } public Task[] getSortedList() { Task[] sortedList = new Task[this.list.size()];//.size: gives he number of elements contained in the array //fills array with given values by using a for loop for (int i = 0; i < this.list.size(); i++) { sortedList[i] = this.list.get(i);...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT