Question
The shell of the Singleton Pattern code is provided. 2 of the below 4 classes need to be finished to work correctly according to the
The shell of the Singleton Pattern code is provided. 2 of the below 4 classes need to be finished to work correctly according to the instructions in the image above.
import java.util.*;
public class AssembleParty implements Runnable { //This is a necessary component of testing the code. //For simplicity, it has been provided to you in a complete state. public void run() { System.out.println("Beginning this thread... "); //Creates the only instance of SingletonDM, and prints the identity. SingletonDM singleton = SingletonDM.getInstance(); System.out.println("ID of current instance: " + System.identityHashCode(singleton));
//Gets and prints all of the character sheets. LinkedList
import java.util.*;
public class PlayerCharacter { //This class shall be used to represent Player Characters as data. //D&D aficionados will note the bare nature of this class and the paucity of given stats. //However, for our purposes, the given information will work just fine. //For consistency and ease of coding, this class was completed for you. //In the immortal words of the Bluth family, that was a freebie. //For our purposes, these values will represent all possible characters. private int[] baseStatScores; private int level; private String type; //I was gonna call these by their proper name, "classes", but that word is special in java. static String[] types = { "Barbarian", "Bard", "Cleric", "Druid", "Fighter", "Monk", "Paladin", "Ranger", "Rogue", "Sorcerer", "Warlock", "Wizard" }; //Constructs a PlayerCharacter of specific level and type. public PlayerCharacter(int lev, int ty) { baseStatScores = rollBaseStats(); level = lev; type = types[ty%12]; } //Constructs a PlayerCharacter of specific level and type. public PlayerCharacter(int lev, String ty) { baseStatScores = rollBaseStats(); level = lev; type = ty; } //Constructs an entirely random character. public PlayerCharacter() { baseStatScores = rollBaseStats(); level = (int)Math.ceil(Math.random() * 20); type = types[(int)Math.floor(Math.random() * 12)]; } //Gets this character's level. public int getLevel() { return level; } //Sets this character's level. public void setLevel(int lev) { level = lev; } //Gets this character's type. public String gettype() { return type; } //Sets this character's type. public void settype(int ty) { type = types[ty]; } //Sets this character's type. public void settype(String ty) { type = ty; }
//Gets this character's stats. public int[] getBaseStats() { return baseStatScores; } //Sets this character's stats. //To prevent creating broken characters, you cannot directly set the stats. public void setBaseStats() { baseStatScores = rollBaseStats(); } //Randomizes the character's level. public void randomizeLevel() { level = (int)Math.ceil(Math.random() * 20); } //Randomizes the character's type. public void randomizeType() { type = types[(int)Math.floor(Math.random() * 12)]; } //Rolls character stats. private int[] rollBaseStats() { int stats[] = new int[6]; for(int i = 0; i
for(int j = 0; j
sum -= min; stats[i] = sum; } return stats; } //Gives an easy way to print the character. //This way, you can just call System.out.println(x), where x is a PlayerCharacter object. public String toString() { return "Level " + level + " " + type; } }
import java.util.*;
public class SingletonDM { //Here, put the specific private static variable that makes this class follow the singleton pattern. //The array of randomized character sheets. //Feel free to hardcode a few of these for your testing. private PlayerCharacter sheets[] = { new PlayerCharacter(), new PlayerCharacter(), new PlayerCharacter(), new PlayerCharacter(), new PlayerCharacter(), new PlayerCharacter(), new PlayerCharacter(), new PlayerCharacter(), new PlayerCharacter(), new PlayerCharacter(), new PlayerCharacter(), new PlayerCharacter(), new PlayerCharacter(), new PlayerCharacter(), new PlayerCharacter(), new PlayerCharacter(), new PlayerCharacter(), new PlayerCharacter(), new PlayerCharacter(), new PlayerCharacter(), new PlayerCharacter() }; //The sheets array remade as a list for convenience. private LinkedList
import java.util.*;
public class TestSingletonDM { public static void main(String args[]) { //This main function will be used to test your program. //Feel free to use it to test your code, but leave this blank when you turn your program in. //Some things you might want to test: //Creating two or more instances of AssembleParty, and starting their threads one after the other //Creating two or more instances of SingletonDM and comparing their IDs with System.identityHashCode() //Creating two or more instances of SingletonDM and comparing their sheet lists //Creating two or more instances of SingletonDM and getting sheets of only a certain type or level }
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started